How to Import Document Trees using XSL

XSLT provides ways to import document trees from sources outside the document you create for output. This has the effect of creating a modular architecture. Using two elements to drive this process, the xsl : import element and the xsl: include element, you can create one main style sheet that consists of several others.


Using the xsl:import Element

The xsl : import element imports other style sheets into your style sheet document. When you import a style sheet, you add all the nodes and content of that style sheet to the imported style sheet document.

Unlike most XSLT style sheet elements, the xsl: import elements is governed by strict rules about where it can appear in the style sheet: It must always be the first child xsl: style sheet element.

The xsl: import element defines the value for the elements required href attribute. The value of the href attribute consists of a URI where the imported style sheet is located. If the style sheet being imported is being referenced through a relative URI, the base URI is always the importing style sheet:

<xsl:stylesheet
xmlns:xsl=http://www.testing.org/1999/xsl/transform>
<xsl:import href= “myimp_1.xsl”/>

Of course, you can also use an absolute URI:

<xsl: import href= http://www.somedomain.com/myimp_2.xsl/>

If you’re wondering which elements take precedence in this scenario the rule is pretty simple. The elements in the style sheet containing the link to the imported style sheet take precedence over the elements in the imported style sheet.

Using the xsl:apply-imports elements

The xsl:apply-imported elements is similar to the xsl:apply-templeates elements. The difference is that xsl: apply-imports is used to apply an imported style sheet to a template.

The rules for imported style sheets dictate that the oldest style sheet always takes precedence. You might have a style sheet with an imported style sheet, which in turn may have an imported style sheet, which in turn may have an imported style sheet of its own.

What happens to precedence then? The result nodes simply appear in the order of appearance within the various imports. The xsl:apply-imports element cannot have other elements nested within it and is always an empty element: it also has no attributes.

Using the xsl:include Element

This element works the same way as the xsl:import element, except that the elements included within the included document replace the xsl:include element. Again, the only attribute is a href whose Value is a URI. Consider an included document that look like this:

<xsl:stylesheet
xmlns:xsl=http://www.test.org/1999/xsl/transform>
<xsl:template match = “testmatch”>
<testmatch>
<xsl:apply-templates/>
</testmatch>
</xsl:template>
</xsl:stylsheet>

If this style sheet is named myStylesheet_1.xsl, and you include it in another style sheet, its elements will replace the xsl:include element in memory:

<xsl:include href= “myStylesheet_1.xsl”/>

The preceding line of the code becomes:

<xsl:templeate match= “testmatch”>
<testmatch><xsl:apply-templates/></testmatch>
</xsl:template>

If this included style sheet contained any xsl: import elements, those elements would move into their place in the tree, right under any other xsl:import elemet already there.

 


SetApp - 100 Apps for everything you ever wanted to do on Mac

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________







FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

Copyright - © 2004 - 2019 - All Rights Reserved.