XSLT比较XML文档不同分支的值
我需要XSLT方面的帮助。。。在输入xml中,我有:XSLT比较XML文档不同分支的值,xml,xslt,if-statement,compare,Xml,Xslt,If Statement,Compare,我需要XSLT方面的帮助。。。在输入xml中,我有: <shop> <categories> <category categoryId="63" parentCategoryId="239"> <name>Fruit</name> </category> <category categoryId="62" parentCategoryId="239"> <
<shop>
<categories>
<category categoryId="63" parentCategoryId="239">
<name>Fruit</name>
</category>
<category categoryId="62" parentCategoryId="239">
<name>Vegetable</name>
</category>
<category categoryId="60" parentCategoryId="221">
<name>Furniture</name>
</category>
...
<categories>
<products>
<product productId="3" productCode="1.05">
<name>Chair</name>
<categories>
<category>60</category>
</categories>
</product>
<product productId="21" productCode="1.59">
<name>Apple</name>
<categories>
<category>63</category>
</categories>
</product>
...
</products>
</shop>
果
蔬菜
家具
...
椅子
60
苹果
63
...
我需要创建产品列表,其中我有产品名称及其类别名称。我该怎么做?这对我不管用
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:template match="shop">
<export>
<xsl:apply-templates select="products" />
</export>
</xsl:template>
<xsl:template match="products">
<xsl:for-each select="product">
<polozka>
<product_name>
<xsl:value-of select="name" />
</product_name>
<xsl:for-each select="/shop/categories/category">
<xsl:if test="boolean(@categoryId = /shop/products/product/categories/category)">
<category_name>
<xsl:value-of select="name" />
</category_name>
</xsl:if>
</xsl:for-each>
</polozka>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果有人能帮助我,我会很高兴…您正在将每个类别的@CategoryId与所有产品进行比较:
@categoryId = /shop/products/product/categories/category
这永远是正确的,因为每个产品都与现有的类别相关
您必须将其与当前产品的类别进行比较。你不需要一个
。它可以用谓词来完成。将您的for each替换为以下内容:
<xsl:for-each select="/shop/categories/category[@categoryId = current()/categories/category]">
<category_name>
<xsl:value-of select="name" />
</category_name>
</xsl:for-each>
现在应该筛选出与当前产品类别不匹配的类别。
< P>如果要查找<强>类别< /强>,请根据其<强> @ SyryYID < /强>,然后考虑使用一个键进行查找< /P><xsl:key name="category" match="category" use="@categoryId"/>
然后,假设每个产品只属于一个类别,您可以像这样简单地查找产品的类别名称
<xsl:value-of select="key('category', categories/category)/name"/>
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="category" match="category" use="@categoryId"/>
<xsl:template match="shop">
<export>
<xsl:apply-templates select="products"/>
</export>
</xsl:template>
<xsl:template match="products">
<xsl:for-each select="product">
<polozka>
<product_name>
<xsl:value-of select="name"/>
</product_name>
<category_name>
<xsl:value-of select="key('category', categories/category)/name"/>
</category_name>
</polozka>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果产品元素可以有多个字符,那么您需要为每个循环使用
<xsl:for-each select="categories/category">
<category_name>
<xsl:value-of select="key('category', .)/name"/>
</category_name>
</xsl:for-each>
您是否可以添加一个示例,让您的输出看起来也一样?谢谢