XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round three / External sources: documents and entities / The XSLT function unparsed-entity-uri()
(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 3.)
There are various tricks for including images or external resources without declaring them as entities. Suppose element <bild> has a CDATA attribute named quelle declared as follows:
<!ELEMENT bild EMPTY>
<!ATTLIST bild quelle CDATA #REQUIRED>
An XML document could contain:
<bild quelle="mein_bild.gif"/>and transform it to an HTML <img>:
<xsl:template match="bild">
<img src="{@quelle}"/>
</xsl:template>
Why not? Passing path and filename as a string often works and is simpler. Still, consider these points:
The value of
quelleis only a string; CDATA is so permissive that it cannot constrain the value.
The parser cannot check the resource’s existence because it cannot recognize the string as a reference.
Paths must be updated manually if documents and images move relative to one another.
What is the advantage of an external entity?
Resource existence can be checked, depending on whether the XML parser implements it. A NOTATION- declaration might also describe further processing or display. Both arguments are weak because few implementations support this.
Paths remain problematic. If document, DTD and image occupy different locations, or another XSLT process indirectly accesses an external source document’s image, manual path maintenance becomes difficult. Declaring the image as an unparsed entity provides a solution: unparsed-entity-uri().
XSLT function unparsed-entity-uri() resolves base URIs and paths between document and resource. With an internal DTD, it returns the reference-to-resource path supplemented by the stylesheet’s base URI.
The function
The function means: find the entity declaration matching the argument, construct its resource URL and return that URL as a string.
Its argument is the entity name as a string, possibly obtained from an XPath expression selecting an attribute containing that name.
It is usually used in an attribute value template:
<xsl:template match="bild">
<img src="{unparsed-entity-uri(@quelle)}"/>
</xsl:template>
The return value is the string value of the generated URI.
Entity declarations in internal or external DTDs
With a DTD inside the document, the function takes the document’s absolute base URI and appends the relative entity path.
With an external DTD, the entity path in the DTD is relative to the DTD, not relative to the document. Starting from the document, the parser adds the path to the DTD, then the path from DTD to entity.
This works across three different directories, even if the reference occurs in an external parsed entity rather than the document. Its base URI is then included too.
Figure: The function combines the path to the DTD and then to the image.
Indirect inclusion through XSLT
Additional XML documents can join a transformation through function document(), covered elsewhere.
Even then, function unparsed-entity-uri() constructs a valid path by considering every file’s relative location.
Figure: Paths between involved documents are resolved.
This last scenario justifies unparsed entities and is less unusual than it seems. The function always requires a DTD, wherever located, accessible to the processing program.
Copyright © Galileo Press, Bonn 2008
You may print the online version for your private use.
Otherwise, this chapter from “XSLT 2.0 & XPath 2.0 ― Das umfassende Handbuch” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
Galileo Press, Rheinwerkallee 4, 53227 Bonn