【发布时间】:2014-03-06 14:28:27
【问题描述】:
假设您有一个定义了多个命名空间前缀的 XML 块,其中一些实际上是相同的命名空间,只是前缀不同。使用 XSLT 是否有一种不太复杂的方法来合并这些前缀,以便最终每个名称空间只有一个前缀?比如选最短的?
示例
<soapenv:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:f="http://api.example.com/Service"
xmlns:foo="http://api.example.com/Service">
<soapenv:Body>
<foo:serviceResponse>
<f:profile id="1">Alice</f:profile>
<f:profile id="2">Bob</f:profile>
</foo:serviceResponse>
</soapenv:Body>
</soapenv:Envelope>
例如应该变成这样:
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:f="http://api.example.com/Service">
<soap:Body>
<f:serviceResponse>
<f:profile id="1">Alice</f:profile>
<f:profile id="2">Bob</f:profile>
</f:serviceResponse>
</soap:Body>
</soap:Envelope>
【问题讨论】:
-
只要命名空间正确,为什么还要关心前缀?
-
因为我是一个喜欢干净的完美主义者。如果有一个简单的解决方案可以解决这个问题,我可以添加它而不必再为它烦恼;)
-
"如果有一个简单的解决方案" 好吧,现在你知道了,哈哈。
标签: xml xslt namespaces xslt-2.0