Advertisements

Monday, October 23, 2017

XSLT how to handle XML and node set content in variables

It is very helpful to persist a xml node in a variable that we can query later using xpath, we can accomplish this easily, however you have to make sure the XML transformer (I use Xalan) you are using has to be compatible.

Having the following xml document to play around:

 
 1
 Article1
 This is article 1
 
 
 2
 Article2
 This is article 2
 
 
 3
 Article3
 This is article 3
 

1.- How to use a XSLT variable that holds a XPATH node set so we can use it as a variable in our XSLT.
1.1.- Build the variable using xpath and only get the node set that is required using a predicate
1.2.- Use the variable same way we use xpath expression Here is the xslt:
Here is the output:
Name: Article1
Desc: This is article 1




Notes: You might need to include this in the namespace depending on which transformer you are using:
extension-element-prefixes="exsl"

This is an extension that allows to manipulate xml documents within a variable.