简体   繁体   English

XSLT:获取文本标记模板中的先前节点值

[英]XSLT: Get previous node value within text tokenized template

I'm new in XSLT so I hope I'll get some help here. 我是XSLT的新手,所以我希望这里能有所帮助。

I'm trying to transform following XML 我正在尝试转换以下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>

to

<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>

with following xslt which doesn't work 与以下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>

How can I access SKU correctly? 如何正确访问SKU?

Using XSLT 2.0 the output can be achieved by making a small tweak in the shared XSL. 使用XSLT 2.0 ,可以通过对共享XSL进行小的调整来实现输出。 The <SKU> value can be added to a variable and used to format the desired output. 可以将<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>

Output 输出量

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM