Below is my XML file, which is used to stored the data –
<Locations>
<location>
<place>Newyork</place>
<dt>01-Dec-2011</dt>
</location>
<location>
<place>Berlin</place>
<dt>02-Dec-2011</dt>
</location>
<location>
<place>Tokyo</place>
<dt>04-Dec-2011</dt>
</location>
</Location>
What I want to achieve is –
I want to replace the <dt> tags date value, if the visit is re-scheduled. For example-
If the visit date for Berlin is changed, stored in <dt> tags, then how to edit/replace the same
in the XML file using XSLT..? Thanks in advance – John
This transformation shows how to use a global parameter (modelled here with an inline element) to specify (possibly multiple) updates:
When applied on the provided XML document (corrected to make it well-formed):
the wanted, correct result is produced:
Explanation:
The identity rule copies every node “as-is“.
There is just one overriding template — matching the text-node child of any
dtwhoseplacesibling’s string value has a correspondingmy:updates/updateelement. In this template we output the value of thedtattribute of this correspondingmy:updates/updateelement.Do note: In a realworld transformation the inline
my:updateselement will be better replaced by an external, global parameter. Read your XSLT processor’s documentation how to pass an external parameter to the transformation — this is implementation-dependent.UPDATE: As the OP has found it difficult to convert this solution to one using global, externally passed
xsl:param, here is this converted solution:When this transformation is applied on the same XML document (above), the same correct and wanted result is produced:
Do note: In this solution the
xsl:paramstill has its value hardcoded and this is the only reason we are using theext:node-set()extension function. If the parameter is really passed from outside, then this convertion from RTF to a regular tree isn’t necessary and the parameter should be referenced directly.Also, in XSLT 1.0 we have to match more inexactly and to use comparisons (the
xsl:choose) inside the body of the template. This is so because in XSLT 1.0 it isn’t allowed to reference variables/parameters inside the match-pattern.In XSLT 2.0 this limitation has been eliminated, so we can just have a much simpler transformation: