首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XSLT -向给定位置添加新节点

XSLT -向给定位置添加新节点
EN

Stack Overflow用户
提问于 2015-07-24 07:52:05
回答 1查看 223关注 0票数 0

我有一个类似于以下xml的xml,

代码语言:javascript
复制
<doc>
<footnote>
    <p type="Foot">
        <link ref="http://www.facebook.com"/>
        <c>content</c>
        <d>cnotent</d>
    </p>
    <p type="Foot">
        <link ref="http://www.google.com"/>
        <c>content</c>
        <d>cnotent</d>
    </p>
</footnote>
</doc>

我的要求是,

1)向具有属性<p>type="Foot"节点添加动态id。

2)在<newNode>节点中添加名为<p>的新节点

3)向<newNode>添加动态id

所以输出应该是

代码语言:javascript
复制
<doc>
<footnote>
    <p id="ref-1" type="Foot">
        <newNode type="direct" refId="foot-1"/>
        <link ref="http://www.facebook.com"/>
        <c>content</c>
        <d>cntent</d>
    </p>
    <p id="ref-2" type="Foot">
        <newNode type="direct" refId="foot-2"/>
        <link ref="http://www.google.com"/>
        <c>content</c>
        <d>cotent</d>
    </p>
</footnote>
</doc>

我在xsl之后写了这样的文章

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

    <!-- add new dynamic attribute to <p> -->
    <xsl:template match="p[@type='Foot']">
        <xsl:copy>
            <xsl:attribute name="id">
                <xsl:value-of select="'ref-'"/> 
                <xsl:number count="p[@type='Foot']" level="any"/>
            </xsl:attribute>

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

            <!-- add new node with dynamic attribute -->
            <newNode type="direct">
                <xsl:attribute name="refId">
                    <xsl:value-of select="'foot-'"/>
                    <xsl:number count="p[@type='Foot']" level="any"></xsl:number>
                </xsl:attribute>
            </newNode>
        </xsl:copy>

    </xsl:template>

我的问题是在<p>节点中添加新节点和最后一个节点(如下面所示),我需要在<p>节点中添加作为第一个节点。

代码语言:javascript
复制
      <p id="ref-1" type="Foot">
            <link ref="http://www.facebook.com"/>
            <c>content</c>
            <d>cntent</d>
            <newNode type="direct" refId="foot-1"/>
        </p>

如何在<p>节点中放置第一个节点,如下所示?

代码语言:javascript
复制
    <p id="ref-1" type="Foot">
        <newNode type="direct" refId="foot-1"/>
        <link ref="http://www.facebook.com"/>
        <c>content</c>
        <d>cntent</d>
    </p>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-24 07:56:23

您需要确保在创建新节点后复制子元素:

代码语言:javascript
复制
<xsl:template match="p[@type='Foot']">
    <xsl:copy>
        <xsl:attribute name="id">
            <xsl:value-of select="'ref-'"/> 
            <xsl:number count="p[@type='Foot']" level="any"/>
        </xsl:attribute>

        <xsl:apply-templates select="@*" />

        <!-- add new node with dynamic attribute -->
        <newNode type="direct">
            <xsl:attribute name="refId">
                <xsl:value-of select="'foot-'"/>
                <xsl:number count="p[@type='Foot']" level="any"></xsl:number>
            </xsl:attribute>
        </newNode>

        <xsl:apply-templates/>
    </xsl:copy>

</xsl:template>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31604992

复制
相关文章

相似问题

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