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

时间:2017-08-21 09:08:29

标签: xml xslt

我是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?

1 个答案:

答案 0 :(得分:2)

使用<item> <layer-list> <item> <shape> <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#ffffff" android:type="linear" /> <stroke android:width="0.33dp" android:color="#0fb1fa" /> <corners android:radius="0dp" /> <padding android:bottom="3dp" android:left="3dp" android:right="3dp" android:top="3dp" /> </shape> </item> <item android:right="5dp"> <bitmap android:gravity="center_vertical|right" android:src="@drawable/arrow_down_gray" /> </item> </layer-list> </item> 可以通过在共享XSL中进行小调整来实现输出。 XSLT 2.0值可以添加到变量中,用于格式化所需的输出。

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