Using copy-of with document() to add SVGs to XHTML output
To embed SVGs directly into XHTML using XSLT, combine document()
function to fetch the SVG XML and xsl:copy-of
to insert it:
Here, document('your-svg.svg')
loads the SVG, and the /svg
ensures you are precisely duplicating the <svg>
element itself into your XHTML. It's critical that 'your-svg.svg' is a valid XML SVG file.
Namespace management in XSLT
SVG content often includes elements with namespaces, like xlink:href
. XSLT's namespace-alias
directive preserves these namespaces during the embedding process:
In the XSLT copy operations, reference the declared prefix when handling namespaced attributes. This prevents accidental namespace transformations:
Advanced scenarios & potential pitfalls
Cross-origin restrictions
Ensure cross-origin restrictions aren't blocking your SVG sources. The XSLT processor needs access.
Dynamic inclusion of SVGs
SVG files determined at runtime can be handled by dynamically constructing the URI:
Preserving embedded CSS styles
Embedded CSS within SVGs can be preserved during the inclusion process:
Performance optimizations
SVG document caching
Avoid repetitive fetching and parsing of SVG files. Implement a caching mechanism to save computational resources.
SVG pre-processing
Make adjustments to SVG files before the XSLT process. Tools like Inkscape can streamline your task.
Was this article helpful?