XSL 输出中出现的已声明命名空间的定义
Definitions of declared namespaces appearing in XSL output
我有以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<e:export xmlns="http://docbook.org/ns/docbook"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0">
<e:resource id="31750" uuid="UUID-df90c0cb-f3ad-ee14-9123-02e25dedcc21" title="custom-dashboard-widget-displayed.png" />
</e:export>
我有以下转换 -- 注意命名空间的定义 xinfo
:
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0" version="2.0" exclude-result-prefixes="e" >
<xsl:output indent="yes" method="xml" omit-xml-declaration="no" exclude-result-prefixes="xinfo e"/>
<xsl:template match="/">
<xsl:apply-templates select="//e:resource" />
</xsl:template>
<xsl:template match="e:resource">
<mediaobject xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata>
<xsl:attribute name="fileref"><xsl:value-of select="./@uuid"/></xsl:attribute>
<xsl:attribute name="contentwidth">1014</xsl:attribute>
<xsl:attribute name="xinfo:image"><xsl:value-of select="./@uuid"/></xsl:attribute>
</imagedata>
</imageobject>
</mediaobject>
</xsl:template>
</xsl:transform>
这就是我想要的 -- 一些带有 xinfo
命名空间的属性。
<mediaobject xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata fileref="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5" contentwidth="1014" xinfo:image="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"/>
</imageobject>
</mediaobject>
这就是我得到的 -- 先前声明的 xinfo
命名空间的定义:
<mediaobject xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata fileref="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"
contentwidth="1014"
xinfo:image="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"/>
</imageobject>
</mediaobject>
如何在不重新定义的情况下输出属性的命名空间 xinfo
?
我认为您需要使用 exclude-result-prefixes 指令在输出中专门排除不需要的命名空间。
示例:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0"
exclude-result-prefixes="xinfo e">
...
</xsl:stylesheet>
的确,我想要的不是可能的。它发生了,比我想要的更频繁。
我重写了将命名空间绑定到 URL 的转换。
感谢michael.hor257k for pointing that out in :
The output you want is not a well-formed XML document. You cannot use a prefix without binding it a namespace. No XSLT processor will produce such result.
我有以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<e:export xmlns="http://docbook.org/ns/docbook"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0">
<e:resource id="31750" uuid="UUID-df90c0cb-f3ad-ee14-9123-02e25dedcc21" title="custom-dashboard-widget-displayed.png" />
</e:export>
我有以下转换 -- 注意命名空间的定义 xinfo
:
<?xml version="1.0"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0" version="2.0" exclude-result-prefixes="e" >
<xsl:output indent="yes" method="xml" omit-xml-declaration="no" exclude-result-prefixes="xinfo e"/>
<xsl:template match="/">
<xsl:apply-templates select="//e:resource" />
</xsl:template>
<xsl:template match="e:resource">
<mediaobject xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata>
<xsl:attribute name="fileref"><xsl:value-of select="./@uuid"/></xsl:attribute>
<xsl:attribute name="contentwidth">1014</xsl:attribute>
<xsl:attribute name="xinfo:image"><xsl:value-of select="./@uuid"/></xsl:attribute>
</imagedata>
</imageobject>
</mediaobject>
</xsl:template>
</xsl:transform>
这就是我想要的 -- 一些带有 xinfo
命名空间的属性。
<mediaobject xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata fileref="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5" contentwidth="1014" xinfo:image="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"/>
</imageobject>
</mediaobject>
这就是我得到的 -- 先前声明的 xinfo
命名空间的定义:
<mediaobject xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xinfo:version="4.0;4.1;4.2">
<imageobject>
<imagedata fileref="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"
contentwidth="1014"
xinfo:image="UUID-af013f1c-dad0-7c4b-e7df-81c3b79d55d5"/>
</imageobject>
</mediaobject>
如何在不重新定义的情况下输出属性的命名空间 xinfo
?
我认为您需要使用 exclude-result-prefixes 指令在输出中专门排除不需要的命名空间。
示例:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xinfo="http://ns.expertinfo.se/cms/xmlns/1.0"
xmlns:e="http://ns.expertinfo.se/cms/xmlns/export/1.0"
exclude-result-prefixes="xinfo e">
...
</xsl:stylesheet>
的确,我想要的不是可能的。它发生了,比我想要的更频繁。
我重写了将命名空间绑定到 URL 的转换。
感谢michael.hor257k for pointing that out in
The output you want is not a well-formed XML document. You cannot use a prefix without binding it a namespace. No XSLT processor will produce such result.