我有以下的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
而不重新定义它?
我想你需要在你的输出中特别排除你不想要的名字空间,用 排除结果前缀 指令。
例子 :
<?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 为的是在 一句话:
你想要的输出不是一个格式良好的XML文档。你不能在没有绑定命名空间的情况下使用前缀。没有XSLT处理器会产生这样的结果。