Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including an XML file in an XML/XSL file

Tags:

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.

like image 893
Jookia Avatar asked Jan 16 '11 01:01

Jookia


People also ask

How do you reference XSL in XML?

Correct Style Sheet Declaration The xmlns:xsl="http://www.w3.org/1999/XSL/Transform" points to the official W3C XSLT namespace. If you use this namespace, you must also include the attribute version="1.0".

How does XSL work with XML?

XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.

How do I convert one XML to another in XSLT?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.


2 Answers

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.

like image 142
Dimitre Novatchev Avatar answered Oct 23 '22 10:10

Dimitre Novatchev


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

You can use an external entity to reference the menu.xml file and include the content into either an XML file or the XSLT (or both).

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

By using external entities, you can reference/load the menu.xml content from external files and do not have to duplicate the XML content.

For instance, if you wanted the menu.xml content included in your XSLT, you would declare the external entity in your XSLT like this:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE stylesheet [      <!ENTITY menu SYSTEM "./menu.xml"> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

and could then reference it like you would any other entity:

&menu; 

When the XSLT parsed, the entity reference will be expanded and the XML content of the menu.xml will be included as part of the XSLT document as if you had copy/pasted into the spot where the entity reference was.

like image 38
Mads Hansen Avatar answered Oct 23 '22 11:10

Mads Hansen