我有一个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代替?这可能吗?如果是,怎么样?
答案 0 :(得分:10)
是的,可以将XSLT嵌入到XML中。
XSLT 是一个XML文件 ,因此您只需要确保将其放在XML文件的document元素中,以便XML文件仍然是良好的形式。
事实上,it is described in the XSLT specification:
2.7 Embedding Stylesheets
通常,XSLT样式表是一个完整的XML文档 xsl:stylesheet元素作为文档元素。但是,一个XSLT 样式表也可以嵌入另一个资源中。两种形式 嵌入是可能的:
- XSLT样式表可以以文本方式嵌入非XML中 资源,或
- xsl:stylesheet元素可能出现在XML文档之外 作为文件要素。
为了方便第二种嵌入形式,xsl:stylesheet元素 允许具有指定唯一标识符的ID属性。
注意:为了使这样的属性与XPath id一起使用 函数,它实际上必须在DTD中声明为ID。
以下示例显示了xml样式表的处理方式 指令[XML样式表]可用于允许文档 包含自己的样式表。 URI引用使用相对URI 用于定位xsl:stylesheet元素的片段标识符:
<?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>
注意:一个嵌入到文档中的样式表 应用或可以包含或导入样式表 如此嵌入通常需要包含一个模板规则 指定要忽略xsl:stylesheet元素。
根据您计划如何利用它,可能不支持嵌入式样式表。例如,在IE 6/7/8中。 There are some workarounds。
答案 1 :(得分:0)
要跨客户端处理器进行测试,请使用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>