在应用呈现html的XSL时,我观察到不一致的行为。我的具体问题是在下面的代码块之后。
XML实例:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element1>
<child2>XXXXXX</child2>
</element1>
</root>
我的简单XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Instance</title>
</head>
<body>
<xsl:value-of select="/root/element1/child2"/>
<br/>
NBSP = ' '
<br/>
<xsl:apply-templates select="//element1"/>
</body>
</html>
</xsl:template>
<xsl:template match="//element1">
Call From Template:
<br/>
<xsl:value-of select="./child2"/>
<br/>
</xsl:template>
</xsl:stylesheet>
我的结果输出
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test Instance</title>
</head>
<body>XXXXXX<br></br>
NBSP = ' '
<br></br>
Call From Template:
<br xmlns="">XXXXXX<br xmlns=""></body>
</html>
所以我的两个具体问题如下:
由于
答案 0 :(得分:0)
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="html" version="5" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<title>Test Instance</title>
</head>
<body>
<xsl:value-of select="/root/element1/child2" />
<br/>
NBSP = ' '
<br/>
<xsl:apply-templates select="//element1" />
</body>
</html>
</xsl:template>
<xsl:template match="//element1">
Call From Template:
<br />
<xsl:value-of select="./child2" />
<br />
</xsl:template>
</xsl:transform>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xhtml" encoding="UTF-8" indent="yes" use-character-maps="a" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:character-map name="a">
<xsl:output-character character=" " string="&nbsp;"/>
</xsl:character-map>
<xsl:template match="/">
<html>
<head>
<title>Test Instance</title>
</head>
<body>
<xsl:value-of select="/root/element1/child2" />
<br/>
NBSP = ' '
<br/>
<xsl:apply-templates select="//element1" />
</body>
</html>
</xsl:template>
<xsl:template match="//element1">
Call From Template:
<br />
<xsl:value-of select="./child2" />
<br />
</xsl:template>
</xsl:transform>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="xhtml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<title>Test Instance</title>
</head>
<body>
<xsl:value-of select="/root/element1/child2" />
<br/>
NBSP = ' '
<br/>
<xsl:apply-templates select="//element1" />
</body>
</html>
</xsl:template>
<xsl:template match="//element1">
Call From Template:
<br />
<xsl:value-of select="./child2" />
<br />
</xsl:template>
</xsl:transform>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="/">
<html>
<head>
<title>Test Instance</title>
</head>
<body>
<xsl:value-of select="/root/element1/child2" />
<br/>
NBSP = ' '
<br/>
<xsl:apply-templates select="//element1" />
</body>
</html>
</xsl:template>
<xsl:template match="//element1">
Call From Template:
<br />
<xsl:value-of select="./child2" />
<br />
</xsl:template>
</xsl:transform>
答案 1 :(得分:0)
With the HTML output method, a br
element is treated specially (rendered as <br>
) only if it is in no namespace.
With the XHTML output method, a br
element is treated specially (rendered as <br />
) only if it is in the XHTML namespace.
This changes with XSLT 3.0 which supports method="html" version="5", where "no namespace" and "XHTML namespace" are both accepted.