Link to home
Start Free TrialLog in
Avatar of MatthewNicoll
MatthewNicoll

asked on

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

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.)
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

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
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MatthewNicoll
MatthewNicoll

ASKER

Thank you Gertone, that gives me the courage to keep experimenting.