首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -在保留现有属性的同时向一组元素添加编号id属性

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

Stack Overflow用户
提问于 2016-02-18 19:57:17
回答 1查看 711关注 0票数 1

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

代码语言:javascript
复制
<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添加一系列编号ids,例如:

代码语言:javascript
复制
<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进行编号。这样的事情有可能吗?提前谢谢你。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-18 20:25:32

您应该从这里的标识模板开始,在那里复制所有现有的节点和属性。

代码语言:javascript
复制
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

然后,您只需要一个模板来匹配char元素,在那里您可以添加新属性。您可以通过使用xsl:number来实现这一点。

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35491213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档