2017-02-16 9 views
1

私は以下のXMLを持っています。値は、すべて私はこれをやって行くだろうか、いくつかのより多くの余分なデータを持つInformationBodyタグの後に新しいノードを追加する必要があるだけのテストの目的のためにノードXSLT内に新しいノードを挿入

<?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> 

を嘲笑していますか?私は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> 

最終的な結果は、コピーされた後に要素を追加し、InformationBody要素にマッチ-であるとして、それをコピーすることができ

<Information> 
<InformationBody> 

     <Test>EE</Test> 

     <TestTwo>QU1RIENUVEIxICAgICAgIBI3f1QK6wEs</TestTwo> 

     <TestThree>20150119.141224508</TestThree> 

    </InformationBody> 

<newTag></newTag> 
</Information> 

答えて

2

ようになります。下記のとおり: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> 

あなたのアプローチは、同じ動作します。存在する場合は、TAG1のすべての子の後にnewNodeが追加されます。

関連する問題