Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在节点XSLT中插入新节点_Xml_Xslt - Fatal编程技术网

Xml 在节点XSLT中插入新节点

Xml 在节点XSLT中插入新节点,xml,xslt,Xml,Xslt,我有一个XML,如下所示。这些值都是为了测试而模拟的 <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <TAG1> <placeholder>ghgh</placeholder> <placeholder>ghg</placeholder> <placeholder>ghgh</placeholder> <p

我有一个XML,如下所示。这些值都是为了测试而模拟的

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TAG1>

  <placeholder>ghgh</placeholder>

  <placeholder>ghg</placeholder>

  <placeholder>ghgh</placeholder>

  <placeholder>ghg</placeholder>

  <placeholder>ghgh</placeholder>

  <placeholder>ghg</placeholder>

  <Information>

    <InformationBody>

      <Test>EE</Test>

      <TestTwo>QU1RIENUVEIxICAgICAgIBI3f1QK6wEs</TestTwo>

      <TestThree>20150119.141224508</TestThree>

    </InformationBody>

  </Information>

</TAG1>

ghgh
温室气体
ghgh
温室气体
ghgh
温室气体
EE
QU1REINUVEIXICAGIBIB3F1QK6WES
20150119.141224508
我需要在InformationBody标记后附加一个新节点,并附加一些额外的数据。我该如何做呢?我是XSLT新手,并提出了上述内容,但我不确定它是否在正确的轨道上

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- Identity template, copies everything as is -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!-- Override for target element -->
  <xsl:template match="TAG1">
    <!-- Copy the element -->
    <xsl:copy>
      <!-- And everything inside it -->
      <xsl:apply-templates select="@* | *"/> 
      <!-- Add new node -->
      <xsl:element name="newNode"/>
    </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

最终的结果是

<Information>
<InformationBody>

      <Test>EE</Test>

      <TestTwo>QU1RIENUVEIxICAgICAgIBI3f1QK6wEs</TestTwo>

      <TestThree>20150119.141224508</TestThree>

    </InformationBody>

<newTag></newTag>
</Information>

EE
QU1REINUVEIXICAGIBIB3F1QK6WES
20150119.141224508

您可以匹配
InformationBody
元素,按原样复制它,然后在复制后添加元素。详情如下:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Identity template, copies everything as is -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <!-- Override for target element -->
    <xsl:template match="InformationBody">
        <!-- Copy the element -->
        <xsl:copy>
            <!-- And everything inside it -->
            <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
        <!-- Add new node -->
        <xsl:element name="newNode"/>
    </xsl:template>
</xsl:stylesheet>

如果
InformationBody
之后没有任何元素,那么您的方法也会起同样的作用。如果有,将在
TAG1
的所有子节点之后添加
newNode