如何根据“@rid”值设置正确的属性“@id”值

问题描述 投票:0回答:1

我尝试根据

@id
属性值在属性
@rid
中输入正确的值。
逻辑:如果元素
a
大于
1
并且元素
cu
等于'au'元素中的
1
,那么如何在属性值之后添加位置,例如
(<a id="a">...</a>, <a id="a">...</a>)
s/b
(<a id="a1">...</a>, <a id="a2">...</a>)

如何解决此问题模板仅匹配
a
元素。
请检查预期输出:

输入:

<root>
<t>Title</t>
<au>
    <cu>
        <x rid="a1 a2"/>
    </cu>
</au>
<a id="a">Centre</a>
<a id="a">Westmead</a>
<n>notes</n>
</root>

XSLT 代码:

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="a">
    <xsl:if test="count(../a) &gt; 1 and count(../au/cu[x]) = 1 and @id">
        <xsl:for-each-group select="self::node()" group-adjacent="self::a">
                <xsl:variable name="pos" select="position()"/>
                <xsl:element name="a">
                    <xsl:attribute name="id">
                        <xsl:value-of select="concat('a',$pos)"/>
                    </xsl:attribute>
                    <xsl:apply-templates/>
                </xsl:element>
            <!--</xsl:for-each>-->
        </xsl:for-each-group>
    </xsl:if>
</xsl:template>

期望输出:

<root>
<t>Title</t>
<au>
    <cu>
        <x rid="a1 a2"/>
    </cu>
</au>
<a id="a1">Centre</a>
<a id="a2">Westmead</a>
<n>notes</n>
xml xslt xslt-2.0
1个回答
0
投票

如果您想从模板中进行操作

a
使用

  <xsl:template match="root[a[2] and not(au/cu[x][2])]/a[@id]">
    <xsl:copy>
      <xsl:attribute name="id">a<xsl:number/></xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>
© www.soinside.com 2019 - 2024. All rights reserved.