XSLT–;将编号id属性添加到一组元素中,同时保留现有属性

XSLT–;将编号id属性添加到一组元素中,同时保留现有属性,xslt,Xslt,对于像这样的文件(具有潜在的附加属性): 在不丢失之前的任何属性的情况下,而是相反地使用key属性对每个id进行编号。这样做可能吗?提前感谢。您应该从此处的标识模板开始,在该模板中,跨所有现有节点和属性进行复制 <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </

对于像这样的文件(具有潜在的附加属性):


在不丢失之前的任何属性的情况下,而是相反地使用key属性对每个id进行编号。这样做可能吗?提前感谢。

您应该从此处的标识模板开始,在该模板中,跨所有现有节点和属性进行复制

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

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

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

    <xsl:template match="char">
        <xsl:copy>
            <xsl:attribute name="id">
                <xsl:value-of select="@key" />
                <xsl:text>-</xsl:text>
                <xsl:variable name="key" select="@key" />
                <xsl:number level="any" count="char[@key = $key]"></xsl:number>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

-
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>
<xsl:attribute name="id">
    <xsl:value-of select="@key" />
    <xsl:text>-</xsl:text>
    <xsl:variable name="key" select="@key" />
    <xsl:number level="any" count="char[@key = $key]"></xsl:number>
 </xsl:attribute>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" indent="yes" />

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

    <xsl:template match="char">
        <xsl:copy>
            <xsl:attribute name="id">
                <xsl:value-of select="@key" />
                <xsl:text>-</xsl:text>
                <xsl:variable name="key" select="@key" />
                <xsl:number level="any" count="char[@key = $key]"></xsl:number>
            </xsl:attribute>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>