Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
RSS Feed Parsing for Images 5 59
Scraping specific data within an XML document 19 54
Image decoding from Camera 3 72
microsoft access - xml 10 58
The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now