XML文件的XSLT转换
我有这样的XML,我可以有带有区域和TT的模型,也可以只有TT的模型:XML文件的XSLT转换,xml,csv,xslt,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="MX10
<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>
我尝试了此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="'
'" />
<xsl:variable name="tab" select="'	'" />
<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?如果我正确理解您试图执行的操作(这一点不确定),您应该尝试: XSLT1.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="'
'" />
<xsl:variable name="tab" select="'	'" />
<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>
<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="'
'" />
<xsl:variable name="tab" select="'	'" />
<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>
编辑 我发现我有另一个例子,我可以用区域和 TT和仅含TT的模型 这是一个很大的区别,需要对方法进行重大改变: XSLT1.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="'
'" />
<xsl:variable name="tab" select="'	'" />
<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>
<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="'
'" />
<xsl:variable name="tab" select="'	'" />
<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>
你自己试过什么吗?你能发布你的XSLT吗?这让人困惑:你说你想要一个CSV文件,但你正在尝试构建一个选项卡分隔的文件,而你显示的输出两者都不是。非常感谢,我已经更正了代码,但我发现我有另一种情况,我可以使用带有区域和TT的模型,而只使用TT的模型,您的代码不显示第二种情况。@StefanoCalciati“您的代码不显示第二种情况。”。当然不是。我不是千里眼。这有很大的区别:我不认为有一种优雅的方法可以用相同的代码处理这两种情况。您需要在模型级别进行分支。@StefanoCalciati如果您的问题得到了回答,请通过接受答案来结束它。
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