user1731504
user1731504

Reputation: 159

How to manipulate the variable containing xml using xsl

I have requirement with xsl variable containing xml.

<xsl:variable name="file" select="document('abc.xml')"/>

abc.xml is nothing but some sample xml <a>1<b>2</b>3</a>

Now I have to modify/add elements of the variable $file and assign the result to another variable..

My input will be

<Service name="CBI" detailedLog="false">
    <PolicyRules type="default">
        <EndPoints>
            <EndPoint source="Src" target="ET" serviceoperation="AV01">
                <Url>http://firstbackend.com</Url>
            </EndPoint>
            <EndPoint source="Src" target="ET" serviceoperation="PV01">
                <Url>http://secondbackend</Url>
            </EndPoint>
        </EndPoints>
    </PolicyRules>
</Service>

I have to fetch the tags along with $file..I need the following output..

<a>1<b>2</b>
<Url>http://firstbackend.com</Url>
<Url>http://secondbackend</Url>
3</a>

Could anyone please help me

Upvotes: 0

Views: 1485

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167446

Store the main input document into a global variable e.g.

<xsl:variable name="main-doc" select="/"/>

then write a template for the element you want to transform e.g.

<xsl:template match="a">
  <xsl:copy>
    <xsl:copy-of select="b | b/preceding-sibling::node()"/>
    <xsl:copy-of select="$main-doc//Url"/>
    <xsl:copy-of select="b/following-sibling::node()"/>
  </xsl:copy>
</xsl:template>

then apply the templates and store in a variable (if needed) e.g.

<xsl:variable name="rtf1">
  <xsl:apply-templates select="$file/node()"/>
</xsl:variable>

then use that variable to output the result e.g.

  <xsl:template match="/">
    <xsl:copy-of select="$rtf1"/>
  </xsl:template>

Upvotes: 1

Related Questions