有条件的同级XSLT位置

我需要了解如何在用XSL选择的子元素中获取具有相似值的并行元素的位置。我列出了带有某些参考的项目行作为子元素,然后有子项目行,应根据子元素值将其链接到项目行中。子项目行应指明参考编号相似的项目行的位置

我在方括号[]中尝试了几种不同条件的先行方法,但到目前为止还算不上运气。我只能使用xslt 1.0

我的xml具有这样的结构:

<goods>
    <item>
        <ref>a</ref>
    </item>
    <item>
        <ref>b</ref>
    </item>
    <item>
        <ref>c</ref>
    </item>
    <item>
        <ref>d</ref>
    </item>
    <subitem>
        <subref>c</subref>
    </subitem>
    <subitem>
        <subref>a</subref>
    </subitem>
</goods>

和我的xsl(1.0):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 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:output method="text" version="1.0" encoding="ISO-8859-1" indent="yes"/>
    <xsl:template match="/">
        <xsl:call-template name="Line"/>
    </xsl:template>
    <xsl:template name="Line">
        <xsl:for-each select="goods/item">
            <xsl:value-of select="position()"/>
            <xsl:text>;</xsl:text>
            <xsl:value-of select="ref"/>
            <xsl:text>&#xD;</xsl:text>
        </xsl:for-each>
        <xsl:for-each select="goods/subitem">
            <xsl:text>0;</xsl:text>
            <xsl:value-of select="subref"/>
            <xsl:text>;</xsl:text>
            here would be some kind of conditional preceeding select needed
            <xsl:text>&#xD;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

所需的输出将是:

1;a
2;b
3;c
4;d
0;c;3
0;a;1

最后2行是子项,最后一个数字应告诉我item-element的位置,相同的引用在哪里。在示例中,引用“ c”位于位置为3的项元素内部(第三个元素在子元素“ ref”中具有“ c”),因此在示例中,具有子引用值“ c”的子项应链接到项位置3。>

每个子项行都相同:所有subitem / subref = a的位置都应为1,所有'b'的位置应为2,依此类推。

iceman4019 回答:有条件的同级XSLT位置

这是一种查看方式:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>

<xsl:key name="item" match="item" use="ref" />

<xsl:template match="/goods">
    <xsl:for-each select="item">
        <xsl:value-of select="position()" />
        <xsl:text>;</xsl:text>
        <xsl:value-of select="ref"/>
        <xsl:text>&#xD;</xsl:text>
    </xsl:for-each>
    <xsl:for-each select="subitem">
        <xsl:text>0;</xsl:text>
        <xsl:value-of select="subref"/>
        <xsl:text>;</xsl:text>
        <xsl:value-of select="count(key('item',subref)/preceding-sibling::item) +1" />
        <xsl:text>&#xD;</xsl:text>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

请注意,这假设每个subref都有一个对应的item,且其值与ref相匹配。

本文链接:https://www.f2er.com/3160948.html

大家都在问