Solved

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

Posted on 2011-03-08
4
464 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mongodb hierarchy 2 59
Merge 2 XML Files together 5 53
Transform XML to Excel using XSL 1 33
XSLT XML 4 28
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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