我编写了一个用于将 XML 转换为 ePub 的包。一切正常,除了某些情况下,空白命名空间 ( xmlns="") 节点被写入结果文档。在转换之前,我准备了用于保存主段(即meta,body等)的临时变量,最后将节点(使用xsl:copy-of[@copy-namespaces='no']指令)复制到结果文档。我也有使用@exclude-result-prefixes='ns_list_sep_by_space'内xsl:transform元素,仍然没能得到期望的结果。
oXygen IDE 在弹出窗口中显示一条消息:
使用 xsl:copy-of 时,新元素还将具有从原始元素节点复制的名称空间节点,除非通过指定 copy-namespaces="no" 将它们排除在外。如果省略此属性,或取值为 yes,则原始元素的所有命名空间节点都将复制到新元素。如果取值为 no,则不会复制任何名称空间节点:但是,名称空间节点仍将根据名称空间修复过程的要求在结果树中创建。
这是我的问题的更多详细信息:
主要样式表:
main.xsl:main caller
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:cylian="local-ns-for-extension-functions"
exclude-result-prefixes="xs xd cylian"
version="2.0">
<xsl:import href="modules/core.xsl"/>
<xsl:variable name="base" select="base-uri()" as="xs:anyURI"/>
<xsl:template match="/">
<xsl:call-template name="procA"/>
</xsl:template>
</xsl:transform>
Run Code Online (Sandbox Code Playgroud)
主要样式表:
core.xsl: core processing unit
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:cylian="local-ns-for-extension-functions"
exclude-result-prefixes="xs xd cylian"
version="2.0">
<xsl:import href="sub1.xsl"/>
<xsl:import href="sub2.xsl"/>
<!--and more-->
<!-- variable to hold intermediate results for stage1 -->
<xsl:variable name="stage1">
<cylianz>
<xsl:copy-of select="$a" copy-namespaces="no"/>
<xsl:copy-of select="$b" copy-namespaces="no"/>
<!--and more-->
</cylianz>
</xsl:variable>
<!-- variable to hold intermediate results for stage2 -->
<xsl:variable name="stage2">
<cylianz>
<xsl:for-each select="$stage1//cylian">
<xsl:sort select="@pos"/>
<xsl:sequence select="."/>
</xsl:for-each>
</cylianz>
</xsl:variable>
<xsl:template name="procA">
<xsl:for-each select="$stage2//cylian">
<xsl:result-document href="{concat($outdir,@href)}" format="general">
<xsl:call-template name="procB">
<xsl:with-param name="context" select="."/>
<xsl:with-param name="title">
<xsl:value-of select="$book_title"/>
</xsl:with-param>
</xsl:call-template>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template name="procB">
<xsl:param name="context"/>
<xsl:param name="title"/>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<xsl:call-template name="header">
<xsl:with-param name="title" select="$title"/>
</xsl:call-template>
</head>
<body>
<div id="root">
<xsl:apply-templates select="."/>
</div>
</body>
</html>
</xsl:template>
<!--
1/ other rules are shortened for clarity
2/ declaration «xmlns:cylian='local-ns-for-extension-functions'» has to retain, some parts of transformation uses some extension functions from that namespace
-->
</xsl:transform>
Run Code Online (Sandbox Code Playgroud)
这是输出:
a.html
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta xmlns="" http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title xmlns="">BookTitle</title>
<!--
2012.04.16 - 18:27:36 [XSLT processor: SAXON 9.1.0.5 from Saxonica]
-->
<link xmlns="" href="isbn.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="root">
<div xmlns="" id="a1">
<!--...-->
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我希望它会更容易理解发生了什么问题。欢迎提出所有建议。提前致谢。
好吧,我们需要查看您的代码才能确定,但我怀疑您有例如
<xsl:template match="/">
<foo xmlns="http://example.com/ns">
<xsl:apply-templates/>
</foo>
</xsl:template>
<xsl:template match="whatever">
<bar/>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
然后你得到
<foo xmlns="http://example.com/ns">
<bar xmlns=""/>
</foo>
Run Code Online (Sandbox Code Playgroud)
当你想要的时候
<foo xmlns="http://example.com/ns">
<bar/>
</foo>
Run Code Online (Sandbox Code Playgroud)
要解决这个问题,请确保使用例如移动 xsl:stylesheet 元素上的默认命名空间声明
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://example.com/ns">
version="1.0">
<xsl:template match="/">
<foo>
<xsl:apply-templates/>
</foo>
</xsl:template>
<xsl:template match="whatever">
<bar/>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这样它就适用于在不同模板中创建的所有结果元素。
[编辑] 根据您现在提供的示例,我认为我的建议是正确的,您只需要使用几个文件来确保您放置xmlns="http://www.w3.org/1999/xhtml"在xsl:stylesheet各个xsl:transform元素上的所有样式表模块,以便所有结果元素最终都位于 XHTML 命名空间中。
[第二次编辑] 我想你想要
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
xmlns:cylian="local-ns-for-extension-functions"
exclude-result-prefixes="xs xd cylian"
version="2.0">
<xsl:import href="sub1.xsl"/>
<xsl:import href="sub2.xsl"/>
<!--and more-->
<!-- variable to hold intermediate results for stage1 -->
<xsl:variable name="stage1" xmlns="">
<cylianz>
<xsl:copy-of select="$a" copy-namespaces="no"/>
<xsl:copy-of select="$b" copy-namespaces="no"/>
<!--and more-->
</cylianz>
</xsl:variable>
<!-- variable to hold intermediate results for stage2 -->
<xsl:variable name="stage2" xmlns="">
<cylianz>
<xsl:for-each select="$stage1//cylian">
<xsl:sort select="@pos"/>
<xsl:sequence select="."/>
</xsl:for-each>
</cylianz>
</xsl:variable>
<xsl:template name="procA">
<xsl:for-each select="$stage2//cylian">
<xsl:result-document href="{concat($outdir,@href)}" format="general">
<xsl:call-template name="procB">
<xsl:with-param name="context" select="."/>
<xsl:with-param name="title">
<xsl:value-of select="$book_title"/>
</xsl:with-param>
</xsl:call-template>
</xsl:result-document>
</xsl:for-each>
</xsl:template>
<xsl:template name="procB">
<xsl:param name="context"/>
<xsl:param name="title"/>
<html >
<head>
<xsl:call-template name="header">
<xsl:with-param name="title" select="$title"/>
</xsl:call-template>
</head>
<body>
<div id="root">
<xsl:apply-templates select="."/>
</div>
</body>
</html>
</xsl:template>
</xsl:transform>
Run Code Online (Sandbox Code Playgroud)
然后,如果您有任何应该生成 XHTML 元素的附加模块,请确保放在xmlns="http://www.w3.org/1999/xhtml"模块的根元素上,或者如果您需要在其他命名空间中创建元素以及应该在任何应该输出 XHTML 的模板上。