tags:

views:

46

answers:

3

I'm trying convert this file:

<?xml version="1.0" encoding="utf-8"?> 
<x12errors>  
  <header>  
    <errors>  
      <issue>  
        <message>Wrong value</message>  
        <path>path to bad value</path>  
      </issue>  
      <issue>  
        <message>Missing value</message>  
        <path>path where missing value should be</path>  
      </issue>  
    </errors>  
    <warnings>  
      <issue>  
        <message>Value too small</message>  
        <path>path to value</path>  
      </issue>  
    </warnings>  
  </header>  
  <boxes>  
    <box>  
      <boxName>cardboard</boxName>  
      <boxStyleNum>12345</boxStyleNum>  
      <errors>  
        <issue>  
          <message>Box too small</message>  
          <path>path to box size</path>  
        </issue>  
      </errors>  
      <warnings>  
        <issue>  
          <message>Box flaps off center</message>  
          <path>path to box measurements</path>  
        </issue>  
      </warnings>  
      <wrappings>  
        <wrapping>  
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <errors>
            <issue>
              <message>Wrong material</message>
              <path>path</path>
            </issue>
          </errors>
          <warnings>
            <issue>
              <message>Prefer different color</message>
              <path>path to value</path>
            </issue>
          </warnings>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>

to this file:

<?xml version="1.0" encoding="utf-8"?>
<x12errors>
  <header>
    <headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg>
    <headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg>
    <headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg>
  </header>
  <boxes>
    <box>
      <boxName>cardboard</boxName>
      <boxStyleNum>12345</boxStyleNum>
      <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg>
      <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg>
      <wrappings>
        <wrapping>
          <material>bubble wrap</material>
          <dimensions>9x12</dimensions>
          <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg>
          <wrappingMsg><type>E</type><msgText>Prefer different color</msgText></wrappingMsg>
        </wrapping>
      </wrappings>
    </box>
  </boxes>
</x12errors>

I have the following xsl file that's close, but it's leaving the "errors" and "warnings" tags in there and I can't figure out why! Can anyone help me?

Here's my current xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

  <xsl:output method="xml" indent="no"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="header//errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="header//errors/issue/message">
    <headerMsg>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </headerMsg>
  </xsl:template>

  <xsl:template match="header/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="header//warnings/issue/message">
    <headerMsg>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </headerMsg>
  </xsl:template>

  <xsl:template match="boxs">
    <xsl:apply-templates select="box"/>
  </xsl:template>

  <xsl:template match="box/errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="box//errors/issue/message">
    <boxMsg>
      <xsl:apply-templates select="../../boxName"/>
      <xsl:apply-templates select="../../boxStyle"/>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </boxMsg>
  </xsl:template>

  <xsl:template match="box/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="box//warnings/issue/message">
    <boxMsg>
      <xsl:apply-templates select="../../boxName"/>
      <xsl:apply-templates select="../../boxStyle"/>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </boxMsg>
  </xsl:template>

  <xsl:template match="wrappings">
    <xsl:apply-templates select="wrapping"/>
  </xsl:template>

  <xsl:template match="wrapping/errors/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="wrapping//errors/issue/message">
    <wrappingMsg>
      <xsl:apply-templates select="../../material"/>
      <xsl:apply-templates select="../../dimensions"/>
      <type>E</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </wrappingMsg>
  </xsl:template>

  <xsl:template match="wrapping/warnings/issue">
    <xsl:apply-templates select="message"/>
  </xsl:template>

  <xsl:template match="wrapping//warnings/issue/message">
    <wrappingMsg>
      <xsl:apply-templates select="../../material"/>
      <xsl:apply-templates select="../../dimensions"/>
      <type>W</type>
      <xsl:element name="msgText">
        <xsl:apply-templates />
      </xsl:element>
    </wrappingMsg>
  </xsl:template>


</xsl:stylesheet>

Thanks! Laurie

A: 

I think you are 'asking' for all the nodes in the beginning. You are going to get a lot of original stuff. Just by removing this in your XSLT:

<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

I could get an output:

<?xml version="1.0" encoding="utf-8"?>  
<headerMsg><type>E</type><msgText>Wrong value</msgText></headerMsg>  
<headerMsg><type>E</type><msgText>Missing value</msgText></headerMsg>  
<headerMsg><type>W</type><msgText>Value too small</msgText></headerMsg>  
        cardboard  
        12345  
        <boxMsg><type>E</type><msgText>Box too small</msgText></boxMsg>  
        <boxMsg><type>W</type><msgText>Box flaps off center</msgText></boxMsg>  
        bubble wrap
        9x12
        <wrappingMsg><type>E</type><msgText>Wrong material</msgText></wrappingMsg>
        <wrappingMsg><type>W</type><msgText>Prefer different color</msgText></wrappingMsg>
AJ
I tried that too - but I don't know how to ask for just the nodes I want. I know very little about xsl and have figured out what I have thus far almost purely by trial and error.
Laurie
+2  A: 

This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="message" exclude-result-prefixes="m">
    <m:header>
        <m:message type="E">Wrong value</m:message>
        <m:message type="E">Missing value</m:message>
        <m:message type="W">Value too small</m:message>
    </m:header>
    <m:box>
        <m:message type="E">Box too small</m:message>
        <m:message type="W">Box flaps off center</m:message>
    </m:box>
    <m:wrapping>
        <m:message type="E">Wrong material</m:message>
        <m:message type="E">Prefer different color</m:message>
    </m:wrapping>
    <xsl:template match="@*|node()" name="identity">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="path"/>
    <xsl:template match="errors|warnings|issue">
        <xsl:apply-templates/>
    </xsl:template>
    <xsl:template match="message">
        <xsl:element name="{local-name(../../..)}Msg">
            <type>
                <xsl:value-of select="document('')/*/m:*[local-name()=local-name(current()/../../..)]/*[.=current()]/@type"/>
            </type>
            <msgText>
                <xsl:value-of select="."/>
            </msgText>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

Output:

<x12errors>
    <header>
        <headerMsg>
            <type>E</type>
            <msgText>Wrong value</msgText>
        </headerMsg>
        <headerMsg>
            <type>E</type>
            <msgText>Missing value</msgText>
        </headerMsg>
        <headerMsg>
            <type>W</type>
            <msgText>Value too small</msgText>
        </headerMsg>
    </header>
    <boxes>
        <box>
            <boxName>cardboard</boxName>
            <boxStyleNum>12345</boxStyleNum>
            <boxMsg>
                <type>E</type>
                <msgText>Box too small</msgText>
            </boxMsg>
            <boxMsg>
                <type>W</type>
                <msgText>Box flaps off center</msgText>
            </boxMsg>
            <wrappings>
                <wrapping>
                    <material>bubble wrap</material>
                    <dimensions>9x12</dimensions>
                    <wrappingMsg>
                        <type>E</type>
                        <msgText>Wrong material</msgText>
                    </wrappingMsg>
                    <wrappingMsg>
                        <type>E</type>
                        <msgText>Prefer different color</msgText>
                    </wrappingMsg>
                </wrapping>
            </wrappings>
        </box>
    </boxes>
</x12errors>

EDIT 1: Better explanation. Note: The "identity transform" ( template[@name='idenity'] ) just copy the input source as-is. Some element are bypassed (not copy but apply templates to childs): errors, warnings and issue. path element is striped with empty template. The inline map: document('') evaluate to stylesheet document root; top level elements in other namespace than XSLT namespace are ignore by the processor, but we can select them (in this case, those that have a local name equal to message grand grand father's local name, then the childs from those that have a string value equal to message string value, and finaly their type atribute)

Alejandro
Thank you!!!!!!!!!
Laurie
Wow! that is a slick solution. Can you please enlighten this by walking through it? I will be really glad. You have my +1 already.
AJ
@Alejandro : Thanks a lot
AJ
@AJ: You are wellcome.
Alejandro
A: 

Little advice :). If have any XML with it's own XSL, you can use XEP to check if they correspond to each other, otherwise it will show you your errors... See more about XEP you can visit the forum http://cooltools.renderx.com/viewtopic.php?f=14&amp;t=4

Vardges