我正在寻找一种将 XSL 嵌入 XML 的解决方案,这样只有 1 个 XML 文件发送到浏览器。我在这里尝试了 Dimitre Novatchev 提出的解决方案:将 xsl 嵌入到 XML 文件中 https://stackoverflow.com/questions/360628/embed-xsl-into-an-xml-file/15121012#15121012
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes"/> <xsl:variable name="vEmbDoc">
<doc>
<head></head>
<body>
<para id="foo">Hello I am foo</para>
</body>
</doc>
</xsl:variable>
<xsl:template match="para">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="xsl:template"/></xsl:stylesheet>
问题是,通过这个解决方案,我找不到将样式元素包含在头部内的方法。似乎在建议的解决方案中,头和正文标签没有任何效果,因为浏览器会在解析过程中自动添加它们,并且即使不包含这些标签,解决方案也能正常工作。
所以问题是:如何在上述解决方案的头部包含样式元素,如下所示:
<head><style>body {font-size:10pt;padding:20pt} </style></head>
这个XML文档:
<?xml-stylesheet type="text/xsl" href="myEmbedded.xml"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
exclude-result-prefixes="xsl">
<xsl:output omit-xml-declaration="yes"/>
<xsl:variable name="vEmbDoc">
<doc>
<head>
<style>body {font-size:10pt;padding:20pt}</style>
</head>
<body>
<para id="foo">Hello I am foo</para>
</body>
</doc>
</xsl:variable>
<xsl:template match="para">
<h1><xsl:value-of select="."/></h1>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="doc">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<xsl:template match="xsl:template"/>
<xsl:template match="xsl:*">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
包含 XSLT 样式表。起始 PI 指示浏览器在自身上应用此样式表.
这样指定的转换会产生所需的结果:
<html>
<head xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style>body {font-size:10pt;padding:20pt}</style>
</head>
<body xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<h1>Hello I am foo</h1>
</body>
</html>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)