Solved

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

Posted on 2011-03-08
4
458 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you Gertone, that gives me the courage to keep experimenting.
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
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. 
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

8 Experts available now in Live!

Get 1:1 Help Now