Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

xslt - Including an XML file in an XML/XSL file

So currently I'm doing some XML-> XSLT-> (HTML5/CSS3) work. Right now I have a menu.xml file, and I'd like to include it in either the XSL file or the XML page. I've done lots of searching, but I'm unable to find a straightforward answer.

So, how do I include an XML file in to another XML file or in to a XSL file?

Edit: By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I. Here is how any XML document or fragment can be embedded in an XSLT stylesheet and used during the transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <my:menu>
   <menu>
     <choice>A</choice>
     <choice>B</choice>
     <choice>C</choice>
   </menu>
 </my:menu>

 <xsl:template match="/">
  <xsl:copy-of select="document('')/*/my:menu/*"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on any XML document (not used in this example), the wanted result (just copying the XML) is produced:

<menu xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">
   <choice>A</choice>
   <choice>B</choice>
   <choice>C</choice>
</menu>

Remember: Any XML can be embedded into an XSLT stylesheet, provided it is wrapped into a namespaced element (the namespace not the XSLT namespace) and this wrapping element is at the global level (a child of the <xsl:stylesheet> (top) element).

II. Accessing the XML menu file that resides in a separate XML file:

To do this we have to change only slightly the previous example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>


 <xsl:template match="/">
  <xsl:copy-of select="document('menu.XML')/*"/>
 </xsl:template>
</xsl:stylesheet>

If the menu XML file is in the 'menu.XML' file (in the same directory as the XSLT stylesheet file, then this transformation produces exactly the same result as the previous:

<menu>
   <choice>A</choice>
   <choice>B</choice>
   <choice>C</choice>
</menu>

Do note: In both cases we are using the standard XSLT function document()

Typically, one defines a global-level variable, whose value is the result of calling the document() function. Then this variable and its contents is accessed via XPath expressions during the transformation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...