[英]XSLT: Get previous node value within text tokenized template
我是XSLT的新手,所以我希望这里能有所帮助。
我正在尝试转换以下XML
<?xml version="1.0"?><?xml-stylesheet type="text/xsl"?>
<OrderLineItems>
<OrderLineItem>
<SKU>60</SKU>
<Meta>Topic: one, Topic: two, Topic: three, Topic: four</Meta>
</OrderLineItem>
<OrderLineItem>
<SKU>70</SKU>
<Meta>Topic: one, Topic: two, Topic: three, Topic: four</Meta>
</OrderLineItem>
</OrderLineItems>
至
<ArticleNo>60.1</ArticleNo>
<ArticleNo>60.2</ArticleNo>
<ArticleNo>60.3</ArticleNo>
<ArticleNo>60.4</ArticleNo>
<ArticleNo>70.1</ArticleNo>
<ArticleNo>70.2</ArticleNo>
<ArticleNo>70.3</ArticleNo>
<ArticleNo>70.4</ArticleNo>
与以下xslt不起作用
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Meta" name="tokenize">
<xsl:param name="separator" select="', '" />
<xsl:for-each select="tokenize(.,$separator)">
<ArticleNo><xsl:value-of select="../SKU"/>.<xsl:value-of select="position()" /></ArticleNo>
</xsl:for-each>
</xsl:template>
<xsl:template match="SKU" />
</xsl:stylesheet>
如何正确访问SKU?
使用XSLT 2.0
,可以通过对共享XSL进行小的调整来实现输出。 可以将<SKU>
值添加到变量中,并用于格式化所需的输出。
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Meta" name="tokenize">
<xsl:param name="separator" select="', '" />
<xsl:variable name="skuValue" select="../SKU" />
<xsl:for-each select="tokenize(.,$separator)">
<ArticleNo>
<xsl:value-of select="$skuValue" />
.
<xsl:value-of select="position()" />
</ArticleNo>
</xsl:for-each>
</xsl:template>
<xsl:template match="SKU" />
</xsl:stylesheet>
输出量
<ArticleNo>60.1</ArticleNo>
<ArticleNo>60.2</ArticleNo>
<ArticleNo>60.3</ArticleNo>
<ArticleNo>60.4</ArticleNo>
<ArticleNo>70.1</ArticleNo>
<ArticleNo>70.2</ArticleNo>
<ArticleNo>70.3</ArticleNo>
<ArticleNo>70.4</ArticleNo>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.