XSL转换新手需要帮助展平XML文件

时间:2010-08-13 17:31:40

标签: xml xslt

我正在尝试转换此文件:

<?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>

到这个文件:

<?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>

我有以下xsl文件已关闭,但它留下了“错误”和“警告”标签,我无法弄清楚原因!任何人都可以帮助我吗?

这是我当前的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>

谢谢!劳里

3 个答案:

答案 0 :(得分:2)

此样式表:

<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>

输出:

<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>

编辑1 :更好的解释。 注意:“身份转换”(模板[@ name ='idenity'])只是按原样复制输入源。绕过某些元素(不复制但将模板应用于子项):errorswarningsissuepath元素使用空模板进行条带化。内联映射:document('')计算样式表文档根目录;处理器会忽略除XSLT名称空间之外的其他名称空间中的顶级元素,但是我们可以选择它们(在这种情况下,那些具有本地名称等于{grand}父亲本地名称的message的那些元素,然后是那些那些字符串值等于message字符串值,最后是type属性

答案 1 :(得分:0)

我认为你在开始时'询问'所有节点。你会得到很多原创的东西。只需在XSLT中删除它:

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

我可以得到一个输出:

<?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>

答案 2 :(得分:0)

小建议:)。如果有任何带有它自己的XSL的XML,你可以使用XEP检查它们是否相互对应,否则它会显示你的错误...... 有关XEP的更多信息,请访问论坛 http://cooltools.renderx.com/viewtopic.php?f=14&t=4