Solved

Can I use an XML fragment in an XSLT 2 variable?

Posted on 2011-03-08
4
460 Views
Last Modified: 2012-05-11
I am an experienced programmer, using XSLT for the first time.  
I find the documentation of XSLT variables quite stupefying!

I want to extract a fragment of an external xml file, using the document()
function and xpath, store that fragment somewhere (in an XSLT variable?),
then in subsquequent template statements select child nodes from that fragment.  

Is something like this possible:?


<xsl:template match="something">
    <!-- extract the mynode fragment from other.xml into variable frag -->
    <xsl:variable name="frag" select="document('other.xml')/long/xmlpath/expression/mynode" />
   
    <!-- Now extract a child of mynode -->
    <xsl:value-of select="$frag/childofmynode" />

</xsl:template>    


(I have successfully used the document function with xpath.)
0
Comment
Question by:MatthewNicoll
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35082071
yes, you can do that (you can even do that in XSLT1 if you want)
Simply copying a fragment in a variable and doing some XPath on it works in both XSLT1 and XSLT2 (and that is what you do)
Reconstructing a tree in a variable and doing XPath on it, only works in XSLT2, not in XSLT1
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 35082082
and maybe for your comfort, I do that a lot.
It is good design to load a document using document() and reuse that object all the time,
it beats reloading the document on every occasion
0
 

Author Closing Comment

by:MatthewNicoll
ID: 35088803
Thank you Gertone, that gives me the courage to keep experimenting.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 35088852
welcome
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question