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.
I was unable to get the code to work that people provided. However I did get a solution after trying several things. Name an xml file clGroup.xml (first two letters don't matter, but I am finding "Group" is required. This xml file will reference the xml you want to parse.
clGroup xml:
Now name a file caGroup.xml and put in the following code:
Now here is my XSLT file calendarGroup.xslt
The benefit of setting up my code this way is I can easily create the xml from an excel file and not half to put in a declaration statement in the newly created file. You do need to have the format "xxGroup" for this to work together. You can add more groups to the clGroup file and they will parse in the XSLT allowing you to include more xml in the same XSLT parser. Make sure they are all in the same directory.
If simply embedding the file inside the XML file is not a solution then adding a url field that the interpreting program reads should be added- you're basically asking for the equivalent of an include(menu.xml) or require(xml), but in an XML file.
So since you are writing the program that is intepretting the file you can add an
<externalMenuTagThatYouDecideToAddToTheBaseXmlFile>
tag or whatever you want to call it that you will read and insert menu.xml's root note the place of the<externalMenuTagThatYouDecideToAddToTheBaseXmlFile>
.Good luck and may your programs always compile.
I. Here is how any XML document or fragment can be embedded in an XSLT stylesheet and used during the transformation:
When this transformation is applied on any XML document (not used in this example), the wanted result (just copying the XML) is produced:
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:
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: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.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 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:
and could then reference it like you would any other entity:
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.
I have a blog post on this. You use document() to get the other XML and pass around the content using parameters.