XSLT:如何获取所有子节点中特定节点的位置索引

Sur*_*ran 3 xml xslt xpath

考虑以下XML:

<root>
    <a>
        <pos>
            <c val="abc"></c>
            <c val="def"></c>
            <c val="ghi"></c>
            <c val="jkl"></c>
            <c val="mno"></c>
        </pos>
        <b>
            <c></c>
        </b>
        <b>
            <c></c>
            <d>here</d>
        </b>
        <b>
            <e>and</e>
            <c></c>
            <d>for</d>
        </b>
        <b>
            <c></c>
            <c></c>
            <d>also</d>
        </b>
    </a>
    <a>
        <pos>
            <c val="pqr"></c>
            <c val="stu"></c>
            <c val="vwx"></c>
            <c val="yz"></c>
        </pos>
        <b>
            <c></c>
            <d>what</d>
        </b>
        <b>
            <c></c>
        </b>
        <b>
            <d>how</d>
        </b>
        <b>
            <c></c>
            <d>where</d>
            <c></c>
        </b>
    </a>
</root>
Run Code Online (Sandbox Code Playgroud)

现在在我的输出中,当我遇到一个<c></c>内部a时b,我需要从节点val中输入a 的属性的相应值.通过对应的值,我的意思是内部节点的索引应该与所有节点组合的索引相同.<c></c><pos></pos>cposcb

所需的输出是:

<start>
    <level>
        abc
    </level>
    <level>
        def
        here
    </level>
    <level>
        and
        ghi
        for
    </level>
    <level>
        jkl
        mno
        also
    </level>
<start>


<start>
    <level>
        pqr
        what
    </level>
    <level>
        stu
    </level>
    <level>
        how
    </level>
    <level>
        vwx
        where
        yz
    </level>
 </start>
Run Code Online (Sandbox Code Playgroud)

我尝试使用以下XSL:

<xsl:template match="root">
    <star>
        <xsl:apply-templates select="a"/>
    </start>
</xsl:template>
<xsl:template match="a">
    <xsl:apply-templates select="b"/>
</xsl:template>
<xsl:template match="b">
    <level>
      <xsl:for-each select="*">
          <xsl:choose>
            <xsl:when test="name() = 'd'">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:when test="name() = 'e'">
                <xsl:value-of select="."/>
            </xsl:when>
            <xsl:when test="name() = 'c'">
                <xsl:variable name="posCount">
                    <!-- I don't know what to do here -->
                </xsl:valiable>
                <xsl:for-each select="ancestor::a[1]/pos">
                    <xsl:for-each select="c[position() = $posCount]">
                          <xsl:value-of select="@val"/>
                    </xsl:for-each>
                </xsl:for-each>

            </xsl:when>
          </xsl:choose>
      </xsl:for-each>
    </level>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

我需要做的是以某种方式获取c所有bs 内部的位置计数,然后使用从内部val相应定位的属性值.cpos

我该怎么办呢?

注意:我使用的是XSLT 1.0

Thnx提前!!

mic*_*57k 6

使用该xsl:number元素可以非常轻松地获得您要查找的索引号.例如:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="root">
    <root>
        <xsl:apply-templates select="a"/>
    </root>
</xsl:template>

<xsl:template match="a">
    <start>
        <xsl:apply-templates select="b"/>
    </start>
</xsl:template>

<xsl:template match="b">
    <level>
        <xsl:text>&#10;</xsl:text>
        <xsl:apply-templates select="*"/>
    </level>
</xsl:template>

<xsl:template match="d|e">
    <xsl:value-of select="."/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="b/c">
<xsl:variable name="i">
    <xsl:number count="b/c" from="a" level="any" />
</xsl:variable>
    <xsl:value-of select="ancestor::a/pos/c[number($i)]/@val"/>
    <xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)

注意:
1.我root在输出中添加了一个元素,使其成为有效的XML;
2.最好使用密钥从中获取相应的值pos/c.