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:
...
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.