XSLT:需要帮助如何将新属性添加到将包含增量值的现有元素

XSLT: need help how to add a new attribute to an existing element that will contain incremental values

XSLT:需要有关如何将新属性 (id) 添加到将包含增量值的现有元素(行)的帮助。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<Personal>
    <description />
    <details />
    <information>
        <Table>
            <Location>
                <location>town</location>
                <location>city</location>
                <location>country</location>
            </Location>
            <Contact>
                <row>
                    <values>
                        <ref id="0" value="name" />
                        <ref id="1" value="address" />
                    </values>
                </row>
                <row>
                    <values>
                        <ref id="0" value="name" />
                        <ref id="1" value="" />
                        <ref id="2" value="" />
                    </values>
                </row>
            </Contact>
        </Table>
    </information>
</Personal>

期望的输出:

在行元素中有一个 id 属性,其中值将从 0 开始递增

<?xml version="1.0" encoding="UTF-8"?>
<Personal>
    <description />
    <details />
    <information>
        <Table>
            <Location>
                <location>town</location>
                <location>city</location>
                <location>country</location>
            </Location>
            <Contact>
                <row id="0">
                    <values>
                        <ref id="0" value="name" />
                        <ref id="1" value="address" />
                    </values>
                </row>
                <row id="1">
                    <values>
                        <ref id="0" value="name" />
                        <ref id="1" value="" />
                        <ref id="2" value="" />
                    </values>
                </row>
            </Contact>
        </Table>
    </information>
</Personal>

我是 XSLT 的新手,我尝试的一切似乎都不起作用

该转换看起来很简单,您只需使用 position()...

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:mode on-no-match="shallow-copy"/>
    
    <xsl:template match="row">
        <row id="{position()-1}">
            <xsl:apply-templates select="@*|node()"/>
        </row>
    </xsl:template>
    
</xsl:stylesheet>

如果您无法使用 XSLT 3.0,只需将 xsl:mode 替换为 identity transform

工作fiddle:http://xsltfiddle.liberty-development.net/3MP42Nt