I have a XML file and an external XSLT file.
Currently, within my XML I refer to an external XSLT link using an href:
Yes, it is possible to embed the XSLT inside of your XML.
XSLT is an XML file, so you would just need to make sure that you put it inside of the document element of your XML file, so that the XML file is still well-formed.
In fact, it is described in the XSLT specification:
2.7 Embedding Stylesheets
Normally an XSLT stylesheet is a complete XML document with the xsl:stylesheet element as the document element. However, an XSLT stylesheet may also be embedded in another resource. Two forms of embedding are possible:
- the XSLT stylesheet may be textually embedded in a non-XML resource, or
- the xsl:stylesheet element may occur in an XML document other than as the document element.
To facilitate the second form of embedding, the xsl:stylesheet element is allowed to have an ID attribute that specifies a unique identifier.
NOTE: In order for such an attribute to be used with the XPath id function, it must actually be declared in the DTD as being an ID.
The following example shows how the xml-stylesheet processing instruction [XML Stylesheet] can be used to allow a document to contain its own stylesheet. The URI reference uses a relative URI with a fragment identifier to locate the xsl:stylesheet element:
<?xml-stylesheet type="text/xml" href="#style1"?>
<!DOCTYPE doc SYSTEM "doc.dtd">
<doc>
<head>
<xsl:stylesheet id="style1"
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:import href="doc.xsl"/>
<xsl:template match="id('foo')">
<fo:block font-weight="bold"><xsl:apply-templates/></fo:block>
</xsl:template>
<xsl:template match="xsl:stylesheet">
<!-- ignore -->
</xsl:template>
</xsl:stylesheet>
</head>
<body>
<para id="foo">
...
</para>
</body>
</doc>
NOTE: A stylesheet that is embedded in the document to which it is to be applied or that may be included or imported into an stylesheet that is so embedded typically needs to contain a template rule that specifies that xsl:stylesheet elements are to be ignored.
Depending on how you plan to leverage it, embedded stylesheets may not be supported. For instance, in IE 6/7/8. There are some workarounds.
For testing across client-side processors, use a self-referencing stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<!--Reference the file name as the href value-->
<?xml-stylesheet type="text/xsl" href="html5.xml"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml"
>
<!-- Output HTML doctype with text/html content-type and without XML declaration-->
<xsl:output method="xml" encoding="utf-8" version="" indent="yes" standalone="no" media-type="text/html" omit-xml-declaration="yes" doctype-system="about:legacy-compat" />
<!-- Read the children of the stylesheet itself -->
<xsl:template match="xsl:stylesheet">
<xsl:apply-templates/>
</xsl:template>
<!-- Output the HTML markup-->
<xsl:template match="/">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="foo.css"/>
</head>
<body>
<div class="foo">
<span class="bar">
<span class="baz">1</span>
</span>
<!--Added comment to fill empty node-->
<span class="placeholder"><xsl:comment/></span>
</div>
<!-- Read matching templates -->
<xsl:apply-templates />
<!--Add comment to fill empty script tag-->
<script src="foo.js" type="application/x-javascript"><xsl:comment/></script>
</body>
</html>
</xsl:template>
<!-- Don't reprint text nodes within the xsl:stylesheet node -->
<xsl:template match="text()"/>
<!-- Read non-namespaced nodes within the xsl:stylesheet node -->
<xsl:template match="//node()[local-name() = name()]">
<xsl:if test="local-name() = 'foo'">
<xsl:variable name="foo" select="."/>
<input type="text" id="{$foo}" value="{$foo}"></input>
</xsl:if>
<xsl:apply-templates/>
</xsl:template>
<test>
<foo>A</foo>
<foo>B</foo>
<foo>C</foo>
</test>
</xsl:stylesheet>