XSL:避免将命名空间定义导出到生成的 XML 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/857010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:29:30  来源:igfitidea点击:

XSL: Avoid exporting namespace definitions to resulting XML documents

xmlxsltnamespacesexportmsxsl

提问by pypmannetjies

I'd like to take data from some XML files and transform them into a new XML document. However, I do not want the definition of a namespace in the XSLT to occur in the result document.

我想从一些 XML 文件中获取数据并将它们转换为一个新的 XML 文档。但是,我不希望 XSLT 中的名称空间定义出现在结果文档中。

In other words:

换句话说:

source:

来源:

<Namespace:Root xmlns:Namespace="http://www.something.com">

stylesheet:

样式表:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:Namespace="http://www.something.com">

result:

结果:

<resultRoot xmlns:Namespace="http://www.something.com">
<!--I don't want the Namespace definition above-->

I am using msxsl for the transformation.

我正在使用 msxsl 进行转换。

回答by Dirk Vollmar

You can use the exclude-result-prefixesattribute of the xsl:stylesheetelement to avoid emitting namespace prefixes into the output document:

您可以使用元素的exclude-result-prefixes属性xsl:stylesheet来避免将命名空间前缀发送到输出文档中:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:prefix1="http://www.something.com"
         exclude-result-prefixes="prefix1">

</xsl:stylesheet>

To suppress multiple namespaces from the output document specify them separated by whitespace:

要从输出文档中抑制多个命名空间,请指定它们以空格分隔:

exclude-result-prefixes="prefix1 prefix2 prefix3"

From the XSLT specification:

XSLT 规范

When a stylesheet uses a namespace declaration only for the purposes of addressing the source tree, specifying the prefix in the exclude-result-prefixes attribute will avoid superfluous namespace declarations in the result tree.

当样式表仅出于寻址源树的目的使用命名空间声明时,在 exclude-result-prefixes 属性中指定前缀将避免结果树中多余的命名空间声明。

回答by Evan Lenz

divo's answer was already chosen, and appropriately so.

divo 的答案已经被选中,而且是适当的。

But if you're interested in digging deeper, check out the "Too many namespaces"section in my magnum opus on the wildly popular topic of "Namespaces in XSLT". (Yes, that's meant to be tongue-in-cheek. :-) )

但是,如果您有兴趣深入挖掘,请查看我的巨著中有关“XSLT 中的命名空间”这一广受欢迎的主题中的“命名空间过多”部分。(是的,这是开玩笑的。:-))

回答by alamar

use extension-element-prefixes="Namespace"

使用扩展元素前缀=“命名空间”

like:

喜欢:

<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:datetime="http://exslt.org/dates-and-times"
xmlns:str="http://exslt.org/strings"
xmlns:exsl="http://exslt.org/common"
xmlns:uw="xalan://ru.sbtc.util.XSLUtil"
extension-element-prefixes="exsl str datetime uw"
version="1.0">