2017-02-16 3 views
1

Ich habe ein XML wie unten zu sehen. Die Werte sind alle nur zu TestzweckenNeuen Knoten in Knoten XSLT einfügen

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

Ich brauche verspottet einen neuen Knoten nach dem InformationBody-Tag mit einigen mehr zusätzlichen Daten anzuhängen, wie würde ich über das tut dies gehen? Ich bin neu in XSLT und habe das oben genannte entwickelt, aber ich bin mir nicht sicher, ob es auf dem richtigen Weg ist.

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

Das Endergebnis würde so aussehen

<Information> 
<InformationBody> 

     <Test>EE</Test> 

     <TestTwo>QU1RIENUVEIxICAgICAgIBI3f1QK6wEs</TestTwo> 

     <TestThree>20150119.141224508</TestThree> 

    </InformationBody> 

<newTag></newTag> 
</Information> 

Antwort

2

Sie das Element InformationBody mithalten können, kopieren Sie sie wie sie ist, dann ein Element hinzufügen, nachdem es kopiert wird. Wie weiter unten:

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

funktioniert Ihr Ansatz das gleiche, wenn es keine Elemente nach InformationBody sind. Wenn dies der Fall ist, wird die newNode nach allen Kindern von TAG1 hinzugefügt.