[英]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.