John O
John O

Reputation: 5453

Can a chunk of xml be included from another file within an XSL template element?

I'm trying to use XSLT (xsltproc.exe) to turn a pure data xml file into an svg diagram. Many of the symbols in the resulting svg are somewhat complicated, and it makes the xsl difficult to read by including the svg code directly in the main xsl file. It would be much more convenient if I could put that in a separate file and simply include it at the proper place in the xsl:template.

However, <xsl:include/> complains it is any other place than in the xsl:stylesheet element itself.

Is there a method to include a big chunk of xml code within a template?

Upvotes: 0

Views: 127

Answers (2)

Jason Coleman
Jason Coleman

Reputation: 60

<xsl:include> is only used to merge another XSLT Stylesheet into the current one (sim. functionality to <xsl:import>, but with different priority set) and, as you noted, must be a child of the root element of your stylesheet.

What you want to do is use the XPath function document(), which takes the URI of any XML markup file as the first (or only) argument. You can then use that in other templates, variables, etc.

For example, if you wants to get the title of an SVG file, you could use:

<xsl:value-of select="document(your.svg)/svg/title"/>

That might be the least interesting thing you could do with an SVG file, but you can see how to navigate the XML structure of the outside “document” there.

If you have a flat file structure, the URI is simple enough. It can get a bit more complex if your using a file elsewhere, but it can be used to read and use data from essentially any XML format.

Upvotes: 1

Martin Honnen
Martin Honnen

Reputation: 167696

Well, there is the document function which you can use to read in secondary XML documents, which your SVG seems to be. Wherever you need that you can then use <xsl:copy-of select="document('foo.svg')"/> or if needed, process the SVG further with templates.

Upvotes: 1

Related Questions