【发布时间】:2011-02-04 19:36:39
【问题描述】:
xsl:include 和默认命名空间存在问题,导致最终 xml 文档包含具有 xmlns=""
的节点在这个 synario 中,我有 1 个源文档,它是普通的旧 XML 并且没有命名空间:
<?xml version="1.0" encoding="UTF-8"?>
<SourceDoc>
<Description>Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<Description/>
<Title>Hello I'm the title</Title>
</SourceDoc>
此文档被转换为 2 个不同的 xml 文档,每个文档都有它们自己的默认命名空间。
第一个文件:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType1 xmlns="http://MadeupNS1">
<Description >Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<Title>Hello I'm the title</Title>
</OutputDocType1>
第二份文件:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType2 xmlns="http://MadeupNS2">
<Description>Hello I'm the source description</Description>
<Description>Hello I'm the source description 2</Description>
<DocTitle>Hello I'm the title</DocTitle>
</OutputDocType2>
我希望能够在两种转换中重复使用 descriptions 模板。因为这两种文档的逻辑相同。为此,我创建了一个模板文件,该文件在其他 2 个转换中是 xsl:included:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="Description[. != '']">
<Description>
<xsl:value-of select="."/>
</Description>
</xsl:template>
</xsl:stylesheet>
现在这里的问题是这个共享转换不能有一个默认的命名空间,因为它会根据调用转换调用它而有所不同。
例如对于第一个文档转换:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml"/>
<xsl:template match="SourceDoc">
<OutputDocType1 xmlns="http://MadeupNS1">
<xsl:apply-templates select="Description"/>
<xsl:if test="Title">
<Title>
<xsl:value-of select="Title"/>
</Title>
</xsl:if>
</OutputDocType1>
</xsl:template>
<xsl:include href="Template.xsl"/>
</xsl:stylesheet>
这实际上输出如下:
<?xml version="1.0" encoding="utf-8"?>
<OutputDocType1 xmlns="http://MadeupNS1">
<Description xmlns="">Hello I'm the source description</Description>
<Description xmlns="">Hello I'm the source description 2</Description>
<Title>Hello I'm the title</Title>
</OutputDocType1>
这就是问题所在。在描述行我得到一个 xmlns=""
有人知道如何解决这个问题吗?
谢谢
戴夫
【问题讨论】:
标签: xslt namespaces xml-namespaces