使用内联XSLT作为XML文件

前端之家收集整理的这篇文章主要介绍了使用内联XSLT作为XML文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个XML文件和一个外部XSLT文件.

目前,在我的XML中,我使用href引用外部XSLT链接

<?xml version="1.0" encoding="utf-8"?>
    <?xml-stylesheet type="text/xsl" href="stylesheet.xsl" ?>
     <mytag>
         <t1> </t1>
         <t2> </t2>
         <t3> <t3>
     <mytag>

如何使用内联XSLT?这可能吗?如果是,怎么样?

是的,可以将XSLT嵌入到XML中.

XSLT是一个XML文件,所以您只需要确保将其放在XML文件的文档元素中,以使XML文件格式正确.

其实it is described in the XSLT specification

07001

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.

根据您计划如何利用它,可能不支持嵌入式样式表.例如,在IE 6/7/8中. There are some workarounds.

猜你在找的XML相关文章