XSLT - 在保留现有属性的同时向一组元素添加编号的id属性

时间:2016-02-18 19:57:18

标签: xslt

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

<text>
...
<p>"<char key="Utterson">I</char> saw <char key="Hyde">Mr. Hyde</char> go in by the old dissecting-room door, <char key="Poole">Poole</char>," <char key="Utterson">he</char> said. "Is that right, when <char key="Jekyll">Dr. Jekyll</char> is from home?"</p>
<p>"Quite right, <char key="Utterson">Mr. Utterson, sir</char>," replied <char key="Poole">the servant</char>. "<char key="Hyde">Mr. Hyde</char> has a key."</p>
<p>"<char key="Jekyll">Your master</char> seems to repose a great deal of trust in <char key="Hyde">that young man</char>, <char key="Poole">Poole</char>," resumed <char key="Utterson">the other</char> musingly.</p>
<p>"Yes, <char key="Utterson">sir</char>, <char key="Jekyll">he</char> do indeed," said <char key="Poole">Poole</char>. "<char key="servants">We</char> have all orders to obey <char key="Hyde">him</char>."</p>
<p>"<char key="Utterson">I</char> do not think <char key="Utterson">I</char> ever met <char key="">Mr. Hyde</char>?" asked <char key="Utterson">Utterson</char>.</p>
...

我想生成一个副本,为每个char添加一系列编号的ID,例如:

<text>
...
<p>"<char key="Utterson" id="Utterson-1">I</char> saw <char key="Hyde" id="Hyde-1">Mr. Hyde</char> go in by the old dissecting-room door, <char key="Poole" id="Poole-1">Poole</char>," <char key="Utterson" id="Utterson-2">he</char> said. "Is that right, when <char key="Jekyll" id="Jekyll-1">Dr. Jekyll</char> is from home?"</p>
<p>"Quite right, <char key="Utterson" id="Utterson-3">Mr. Utterson, sir</char>," replied <char key="Poole" id="Poole-2">the servant</char>. "<char key="Hyde" id="Hyde-2">Mr. Hyde</char> has a key."</p>
<p>"<char key="Jekyll" id="Jekyll-2">Your master</char> seems to repose a great deal of trust in <char key="Hyde" id="Hyde-3">that young man</char>, <char key="Poole" id="Poole-3">Poole</char>," resumed <char key="Utterson" id="Utterson-4">the other</char> musingly.</p>
<p>"Yes, <char key="Utterson" id="Utterson-5">sir</char>, <char key="Jekyll" id="Jekyll3-">he</char> do indeed," said <char key="Poole" id="Poole-4">Poole</char>. "<char key="servants" id="servants-1">We</char> have all orders to obey <char key="Hyde" id="Hyde-4">him</char>."</p>
<p>"<char key="Utterson" id="Utterson-6">I</char> do not think <char key="Utterson" id="Utterson-7">I</char> ever met <char key="" id="-5">Mr. Hyde</char>?" asked <char key="Utterson" id="Utterson-8">Utterson</char>.</p>
...

不会丢失任何先前的属性,而是使用相反的键属性来为每个id编号。这样的事情可能吗?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您应该从身份模板开始,在所有现有节点和属性的副本

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

然后您只需要一个模板来匹配char元素,您可以在其中添加新属性。您可以使用xsl:number

执行此操作
<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>

试试这个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>