如何在xslt中添加节点?

时间:2017-04-25 02:39:23

标签: xml xslt xslt-1.0

如何在每个节点的xslt中添加新节点。这是我的代码 http://xsltransform.net/93dEHFw/4输入

<test>
    <video graph="1" potime="1811">
        <sec>
            <secid>3812907</secid>
            <pick>
                <vdsecname>A</vdsecname>
                <mediadate>24 Apr 2017, 7:45PM IST</mediadate>
                <v>240420171945</v>

            </pick>
            <pick>
                <vdsecname>D</vdsecname>
                <mediadate>20 Apr 2017, 4:30PM IST</mediadate>
                <v>200420171630</v>
            </pick>
            <pick>
                <vdsecname>E</vdsecname>
                <mediadate>20 Apr 2017, 3:30PM IST</mediadate>
                 <v>200420171530</v>
            </pick>

        </sec>
    </video>
    <video graph="1" potime="94">
        <sec>
            <secid>20970242</secid>
            <pick>
                <vdsecname>B</vdsecname>
                <mediadate>23 Apr 2017, 7:30PM IST</mediadate>
                 <v>230420171930</v>
            </pick>
            <pick>
                <vdsecname>C</vdsecname>
                <mediadate>22 Apr 2017, 5:33PM IST</mediadate>
                 <v>220420171733</v>
            </pick>
        </sec>
    </video>
</test>

输出

<videos>
        <pick>

            <vdsecname>A</vdsecname>

            <mediadate>24 Apr 2017, 7:45PM IST</mediadate>

            <v>240420171945</v>

            <domain>ddd</domain>
        </pick>
        <pick>

            <vdsecname>D</vdsecname>

            <mediadate>20 Apr 2017, 4:30PM IST</mediadate>

            <v>200420171630</v>
            <domain>ddd</domain>
        </pick>
        <pick>

            <vdsecname>E</vdsecname>

            <mediadate>20 Apr 2017, 3:30PM IST</mediadate>

            <v>200420171530</v>
            <domain>ddd</domain>
        </pick>

        <pick>

            <vdsecname>B</vdsecname>

            <mediadate>23 Apr 2017, 7:30PM IST</mediadate>

            <v>230420171930</v>
            <domain>ddd</domain>
        </pick>
        <pick>

            <vdsecname>C</vdsecname>

            <mediadate>22 Apr 2017, 5:33PM IST</mediadate>

            <v>220420171733</v>
            <domain>ddd</domain>
        </pick>

    </videos>

我喜欢这个

  <xsl:variable
name="variableA">
<videos>

<xsl:for-each select="test/video">
    <xsl:copy-of select="sec/pick"/>
    <domain>ddd</domain>
    </xsl:for-each>
    </videos>
</xsl:variable>
<xsl:copy-of select="$variableA"/>

1 个答案:

答案 0 :(得分:1)

以下内容应该有效。第一个模板与根元素匹配,添加<videos>元素并将模板应用于所有pick元素。第二个模板匹配pick元素,复制它及其内容,并添加domain元素。

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />

    <xsl:template match="/*">
        <videos>
            <xsl:apply-templates select="video/sec/pick" />
        </videos>
    </xsl:template>

    <xsl:template match="pick">
        <xsl:copy>
            <xsl:copy-of select="@* | node()" />
            <domain>ddd</domain>
        </xsl:copy>
    </xsl:template>

</xsl:transform>

http://xsltransform.net/93dEHFw/6