Having the following xml document to play around:
01 | < itemlist > |
02 | < itemelement > |
03 | < itemid >1</ itemid > |
04 | < itemname >Article1</ itemname > |
05 | < itemdesc >This is article 1</ itemdesc > |
06 | </ itemelement > |
07 | < itemelement > |
08 | < itemid >2</ itemid > |
09 | < itemname >Article2</ itemname > |
10 | < itemdesc >This is article 2</ itemdesc > |
11 | </ itemelement > |
12 | < itemelement > |
13 | < itemid >3</ itemid > |
14 | < itemname >Article3</ itemname > |
15 | < itemdesc >This is article 3</ itemdesc > |
16 | </ itemelement > |
17 | </ itemlist > |
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:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < xsl:stylesheet version = "1.0" |
03 | xmlns:xsl = "http://www.w3.org/1999/XSL/Transform" > |
04 |
05 | < xsl:variable name = "vItem" |
06 | select = "/itemList/itemElement[itemId=1]" /> |
07 |
08 | <!-- output goes to a text file instead of xml --> |
09 | < xsl:output method = "text" encoding = "UTF-8" /> |
10 |
11 | < xsl:template match = "/" > |
12 | < xsl:text >Name: </ xsl:text >< xsl:value-of select = "$vItem/itemName" /> |
13 | < xsl:text >
</ xsl:text > <!-- line break --> |
14 | < xsl:text >Desc: </ xsl:text >< xsl:value-of select = "$vItem/itemDesc" /> |
15 | </ xsl:template > |
16 |
17 | </ xsl:stylesheet > |
Name: Article1
Desc: This is article 1
Notes: You might need to include this in the namespace depending on which transformer you are using:
1 | extension-element-prefixes="exsl" |
This is an extension that allows to manipulate xml documents within a variable.