XSLT转换XML文件

时间:2015-07-23 15:33:36

标签: xml csv xslt

我有这样的XML,我可以使用带有区域的模型和仅带有TT的TT和模型:

<Brand id="1" desc="IH">
    <Type id="1" desc="Tractors">
        <Product id="1" desc="Tractors">
            <Series id="10496" desc="MXC">
                <Model id="10497" desc="MX100C" tsCode="048">
                    <Region id="4" name="APAC" desc="Asia Pacific" />
                    <Region id="1" name="NA" desc="North America" />
                    <TT code="696033805" desc="-MX MAXXUM MFD  " colCode="2" />
                    <TT code="696033808" desc="-MX MAXXUM 100C " colCode="2" />
                    <TT code="696044567" desc="-MX MAXXUM 300C " colCode="2" />
                </Model>
                <Model id="11597" desc="abc123" tsCode="765">
                    <TT code="123033805" desc="-AX XERCES ABCD  " colCode="1" />
                    <TT code="234033808" desc="-AX XERCES EDF   " colCode="3" />
                </Model>
            </Series>
        </Product>
    </Type>
</Brand>

我需要获得这样的csv:

brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =4;TTcode=696033805
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =4;TTcode=696033808
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =4;TTcode=696044567
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =1;TTcode=696033805
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =1;TTcode=696033808
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =1;TTcode=696044567
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=10497;Regionid =1;TTcode=696033808
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=11597;Regionid = ;TTcode=123033805
brandid=1;TypeID=1;Productid=1;SeriesID=1;Modelid=11597;Regionid = ;TTcode=234033808

我试过这个XSLT,但结果不正确:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space  elements="*"/>
<xsl:variable name="newline" select="'&#x0A;'" />
<xsl:variable name="tab" select="'&#x09;'" />
   <xsl:template match="/">
      <!-- <xsl:apply-templates select="ProductList/Brand" /> -->
      <xsl:apply-templates  />
   <xsl:template match="TT">
      <xsl:value-of select="concat(../../../../../@id,$tab,../../../../../@desc,$tab)"  /> <!-- Brand -->
      <xsl:value-of select="concat(../../../../@id,$tab,../../../../@desc,$tab)"  /> <!-- Type -->
      <xsl:value-of select="concat(../../../@id,$tab,../../../@desc,$tab)"  /> <!-- Product -->
      <xsl:value-of select="concat(../../@id,$tab,../../@desc,$tab)"  /> <!-- Series -->
      <xsl:value-of select="concat(../@id,$tab,../@desc,$tab)"     /> <!-- Model -->
      <xsl:value-of select="concat($tab, $tab, $tab, $tab, $tab,    $tab)" />  <!-- Region -->
      <xsl:value-of select="concat(@code, $tab, @desc, $tab, @colCode)" />
      <xsl:value-of select="$newline"/>
   </xsl:template>
</xsl:stylesheet>

它只显示TT但不显示区域。

如何更正此XSLT?

1 个答案:

答案 0 :(得分:0)

如果我理解你正在尝试做什么(这完全不确定),你应该尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space  elements="*"/>

<xsl:variable name="newline" select="'&#x0A;'" />
<xsl:variable name="tab" select="'&#x09;'" />

<xsl:template match="/">
    <xsl:for-each select="Brand/Type/Product/Series/Model/Region">
        <xsl:variable name="common" select="concat(ancestor::Brand/@id, $tab, ancestor::Type/@id, $tab, ancestor::Product/@id, $tab, ancestor::Series/@id, $tab, ancestor::Model/@id, $tab, @id)"/>
        <xsl:for-each select="../TT">
            <xsl:value-of select="concat($common, $tab, @code, $newline)" />
        </xsl:for-each>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

应用于您的输入示例(正确关闭Brand标记后!),结果将是:

1   1   1   10496   10497   4   696033805
1   1   1   10496   10497   4   696033808
1   1   1   10496   10497   4   696044567
1   1   1   10496   10497   1   696033805
1   1   1   10496   10497   1   696033808
1   1   1   10496   10497   1   696044567

修改

  

我发现我有另一个案例,我可以拥有带区域的模型和   TT和模型只有TT

这有很大的不同,需要对方法进行重大改变:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="ISO-8859-1" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space  elements="*"/>

<xsl:variable name="newline" select="'&#x0A;'" />
<xsl:variable name="tab" select="'&#x09;'" />

<xsl:template match="/">
    <xsl:for-each select="Brand/Type/Product/Series/Model">
        <xsl:variable name="common" select="concat(ancestor::Brand/@id, $tab, ancestor::Type/@id, $tab, ancestor::Product/@id, $tab, ancestor::Series/@id, $tab, ancestor::Model/@id, $tab, @id)"/>
        <xsl:choose>
            <xsl:when test="Region">
                <xsl:apply-templates select="Region">
                    <xsl:with-param name="common" select="$common"/>
                </xsl:apply-templates>
            </xsl:when>
            <xsl:otherwise>
                <xsl:apply-templates select="TT">
                    <xsl:with-param name="common" select="concat($common, $tab)" />
                </xsl:apply-templates>
            </xsl:otherwise>
        </xsl:choose>
     </xsl:for-each>
</xsl:template>

<xsl:template match="Region"> 
    <xsl:param name="common"/>
    <xsl:apply-templates select="../TT">
        <xsl:with-param name="common" select="concat($common, $tab, @id)" />
    </xsl:apply-templates>
</xsl:template> 

<xsl:template match="TT"> 
    <xsl:param name="common"/>
    <xsl:value-of select="concat($common, $tab, @code, $newline)" />
</xsl:template>

</xsl:stylesheet>

<强>结果

1   1   1   10496       10497   4   696033805
1   1   1   10496       10497   4   696033808
1   1   1   10496       10497   4   696044567
1   1   1   10496       10497   1   696033805
1   1   1   10496       10497   1   696033808
1   1   1   10496       10497   1   696044567
1   1   1   10496       11597       123033805
1   1   1   10496       11597       234033808