我有这个xml:
<products>
<product>
<name>ad</name>
<category>something</category>
<path>something</path>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
<imgs>
<img>pictures/large/7/8/87_2.jpg</img>
<img>pictures/large/7/8/87_1.jpg</img>
<img>pictures/large/7/8/87_4.jpg</img>
<img>pictures/large/7/8/87_5.jpg</img>
<img>pictures/large/7/8/87_3.jpg</img>
<img>pictures/large/7/8/87_6.jpg</img>
</imgs>google.com</url>
<price>7.98</price>
<brand>NIKE</brand>
<color>black</color>
<gender>Man</gender>
</product>
<product>
...
...
...
</product>
</products>
我需要什么:
<products>
<product>
<name>ad</name>
<category>something</category>
...
<variation>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
</variation>
<variation>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
</variation>
<variation>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
</variation>
<variation>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
</variation>
</product>
<product>
...
</product>
</products>
我有这个xsl:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/products/product">
<xsl:copy>
<xsl:copy-of select="description|id|name|category|path"/>
<xsl:for-each select="/products/product/size">
<variation>
<xsl:variable name="occur" select="position()"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="/products/product/color-code[$occur]"/>
<xsl:copy-of select="/products/product/size-code[$occur]"/>
<xsl:copy-of select="/products/product/size-qty[$occur]"/>
<xsl:copy-of select="/products/product/size-codeproducer[$occur]"/>
</variation>
</xsl:for-each>
<xsl:copy-of select="imgs|url|price|brand|color|gender"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
结果是复制的xml文件
每个产品节点中的size-codeproducer
,color-code
,size-code
和size-qty
。
任何帮助将不胜感激。
我尝试了xsl复制的各种变体-for-each循环和其他操作,但是大多数解析后都没有打印任何内容,没有打印相同的文档,或者还有其他问题。我猜问题出在我正在使用的路径(xpaths?)。
答案 0 :(得分:2)
首先,您的xsl:for-each
有问题...
<xsl:for-each select="/products/product/size">
通过以表示文档节点的/
开始select表达式,您实际上在进行绝对搜索,而忽略了当前位于的product
。您应该这样做,以获取当前产品的size
元素
<xsl:for-each select="size">
类似地,对于各种xsl:copy-of
语句,不要这样做...
<xsl:copy-of select="/products/product/color-code[$occur]"/>
您应该执行此操作(其中..
获取当前size
元素的父级)
<xsl:copy-of select="../color-code[$occur]"/>
尝试使用此模板
<xsl:template match="/products/product">
<xsl:copy>
<xsl:copy-of select="description|id|name|category|path"/>
<xsl:for-each select="size">
<variation>
<xsl:variable name="occur" select="position()"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="../color-code[$occur]"/>
<xsl:copy-of select="../size-code[$occur]"/>
<xsl:copy-of select="../size-qty[$occur]"/>
<xsl:copy-of select="../size-codeproducer[$occur]"/>
</variation>
</xsl:for-each>
<xsl:copy-of select="imgs|url|price|brand|color|gender"/>
</xsl:copy>
</xsl:template>
(请注意,您也可以在此处执行<xsl:copy-of select="following-sibling::color-code[1]"/>
,对于其他语句也是如此)。
答案 1 :(得分:1)
Just to show another approach to grouping adjacents or starting with something in XSLT 1.0, this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*" name="shallow-copy">
<xsl:copy>
<xsl:apply-templates select="node()[1]|@*"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="size">
<variation>
<xsl:call-template name="group-starting-with"/>
</variation>
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="color-code|size-code|size-qty|size-codeproducer">
<xsl:apply-templates select="following-sibling::node()[1]"/>
</xsl:template>
<xsl:template match="node()" mode="group-starting-with"
name="group-starting-with">
<xsl:copy>
<xsl:apply-templates select="node()[1]|@*"/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::node()[1]"
mode="group-starting-with"/>
</xsl:template>
<xsl:template match="size|imgs" mode="group-starting-with"/>
</xsl:stylesheet>
Output
<products>
<product>
<name>ad</name>
<category>something</category>
<path>something</path>
<variation>
<size>S</size>
<color-code>87-3</color-code>
<size-code>24294</size-code>
<size-qty>10</size-qty>
<size-codeproducer>5902228002604</size-codeproducer>
</variation>
<variation>
<size>M</size>
<color-code>87-4</color-code>
<size-code>24295</size-code>
<size-qty>64</size-qty>
<size-codeproducer>5902228002611</size-codeproducer>
</variation>
<variation>
<size>L</size>
<color-code>87-5</color-code>
<size-code>24296</size-code>
<size-qty>46</size-qty>
<size-codeproducer>5902228002628</size-codeproducer>
</variation>
<variation>
<size>XXL</size>
<color-code>87-7</color-code>
<size-code>24298</size-code>
<size-qty>0</size-qty>
<size-codeproducer>5902228002635</size-codeproducer>
</variation>
<imgs>
<img>pictures/large/7/8/87_2.jpg</img>
<img>pictures/large/7/8/87_1.jpg</img>
<img>pictures/large/7/8/87_4.jpg</img>
<img>pictures/large/7/8/87_5.jpg</img>
<img>pictures/large/7/8/87_3.jpg</img>
<img>pictures/large/7/8/87_6.jpg</img>
</imgs>
<url>google.com</url>
<price>7.98</price>
<brand>NIKE</brand>
<color>black</color>
<gender>Man</gender>
</product>
<product>... ... ...</product>
</products>
Do note: this is traversing in the following axe. It means that every rule is responsible of applying templates to first child and first following sibling. For grouping you need a rule with the matching criterion to start, empty rules to stop at not matching criterion, and bypass rules when the criterion is matched but you are not grouping.