【发布时间】:2014-06-18 08:30:55
【问题描述】:
我在处理 XSLT 工作表时遇到了一些麻烦。这是我的 XML 文档:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<products>
<product>
<id>1</id>
</product>
</products>
<stocks>
<stock>
<id>1</id>
<size>S</size>
<store>NYC</store>
</stock>
<stock>
<id>1</id>
<size>L</size>
<store>NYC</store>
</stock>
<stock>
<id>1</id>
<size>S</size>
<store>LA</store>
</stock>
</stocks>
</catalog>
我想要的是有这种输出 XML :
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<products>
<product>
<id>1</id>
<variants>
<variant>
<size>S</size>
<stocks>
<stock store-ref="NYC">
<stock store-ref="LA">
</stocks>
<variant>
<variant>
<size>L</size>
<stocks>
<stock store-ref="NYC">
</stocks>
<variant>
</variants>
</product>
</products>
</catalog>
今天,我正在使用这个 XSLT 来执行这个转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" exclude-result-prefixes="xs fn" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:key name="sizes" match="stock" use="id"/>
<xsl:key name="stocks" match="stock" use="fn:concat(id, '-', size)"/>
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<catalog>
<products>
<xsl:for-each select="/catalog/products/product">
<product>
<id><xsl:value-of select="id" /></id>
<variants>
<xsl:for-each select="key('sizes', id)">
<variant>
<size><xsl:value-of select="size" /></size>
<stocks>
<xsl:for-each select="key('stocks', fn:concat(id, '-', size))">
<stock store-ref="{store}" />
</xsl:for-each>
</stocks>
</variant>
</xsl:for-each>
</variants>
</product>
</xsl:for-each>
</products>
</catalog>
</xsl:template>
</xsl:stylesheet>
我得到了这个结果:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<products>
<product>
<id>1</id>
<variants>
<variant>
<size>S</size>
<stocks>
<stock store-ref="NYC"/>
<stock store-ref="LA"/>
</stocks>
</variant>
<variant>
<size>L</size>
<stocks>
<stock store-ref="NYC"/>
</stocks>
</variant>
<variant>
<size>S</size>
<stocks>
<stock store-ref="NYC"/>
<stock store-ref="LA"/>
</stocks>
</variant>
</variants>
</product>
</products>
</catalog>
所以我的问题是我想选择不同的尺寸值,但它似乎不起作用。我尝试使用 generate-id() 但我不太了解它是如何工作的,所以我没有得到很好的结果:( 知道如何解决这个问题吗? 谢谢!
【问题讨论】:
-
您是否尝试过使用
<xsl:for-each-group>?看看:stackoverflow.com/questions/19115109/… -
我的“产品”标签中没有任何“尺寸”信息,如果我必须遍历每个产品的每个“库存”,这将需要很长时间(实际上,我有5K 产品和大约 100 万个库存):(
-
我尝试将我的 for-each 替换为
<xsl:for-each select="stock[generate-id() = generate-id(key('sizes', id)[1])]">,但它不起作用:变体列表为空 -
好的,我明白你的意思。我试图用
<xsl:for-each-group select="key('sizes', id)" group-by="size">替换我的foreach,现在它可以工作了!谢谢!