Page 1 of 1

How do I override a xml value in xslt?

Posted: Thu Oct 09, 2014 4:24 pm
by winkimjr2
In short whenever my xml element has the following DC19DAKHN or DC19D0000 , in xslt output I want to override that value with MN019015J .

How do I do this in XSLT?

My XML Code

Code: Select all


<ValueID>
<MyID>[B]DC19DAKHN[/B]</MyID>
</ValueID>

My xslt output is

Code: Select all

<myID>[B]DC19D0000[/B]</myID>
I want this to look like this instead

Code: Select all

<myID>[B]MN019015J[/B]</myID>

Do I use If Choose?

Re: How do I override a xml value in xslt?

Posted: Sun Oct 12, 2014 5:53 pm
by Exselt
There are many ways in XSLT you can do this. My preference would be to use a modified identity template, where you match upon the text nodes and replace the values you want replaced.

Code: Select all

<!-- XSLT 3.0: copies everything, unless matched -->
<xsl:mode on-no-match="shallow-copy" />

<!-- only match text nodes, because that's what we want to change here -->
<xsl:template match="text()">
<xsl:value-of select="replace(., 'DC19DAKHN|DC19D0000', 'MN019015J')" />
</xsl:template>
The above code works with any XSLT 3.0 processor, like Saxon or Exselt. If you can't use an XSLT 3.0 processor and are stuck with 2.0, replace the xsl:mode declaration with an identity template.

HTH,
Abel Braaksma
http://Exselt.net, a streaming XSLT 3.0 processor