如何通过在 XSLT 中编写单个语句来避免输出中出现所有名称空间 [英] How to avoid all namespace occurrence in output by writing a single statement in XSLT

查看:23
本文介绍了如何通过在 XSLT 中编写单个语句来避免输出中出现所有名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了exclude-result-prefixes",即便如此,我还是在输出中看到了名称空间出现的外观.

I wrote "exclude-result-prefixes" and even then I see the name space occurrence appearance in output.

<xsl:stylesheet
    version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:simple="Simple name space"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:tcm="http://www.tridion.com/ContentManager/5.0"
    xmlns:xh="http://www.w3.org/1999/xhtml"
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:transform-ext="urn:tridion:transform-ext"
    xmlns="http://www.w3.org/1999/xhtml"
    exclude-result-prefixes="#default simple xh">

实际输出

<strong xmlns="Simple name space">abcd
    <link xmlns="http://www.w3.org/1999/xhtml">
        <text>Header</text>
    </link>
</strong>

有什么方法可以通过编写单个语句排除所有命名空间.

Is there any method so that I can exclude all namespace by writing single statement.

这里我明确提到

exclude-result-prefixes="#default simple xh"

如何避免在 xslt 中出现所有命名空间?

How to avoid all namespace appearance in xslt?

推荐答案

xsl:stylesheetexclude-result-prefixes 属性,当指定为 时"yes" 要求删除字面结果元素(仅)的任何名称空间节点,这些节点是继承的,并且没有定义字面结果元素的名称空间 uri 和前缀.

The exclude-result-prefixes attribute of xsl:stylesheet, when specified as "yes" mandates the removal of any namespace nodes of a litreral result element (only) that are inherited and don't define both the namespace-uri and prefix of the literal result element.

Markus Jarderot 回答中的以下陈述是错误的:

"exclude-result-prefixes 只是删除了 xmlns:foo="" 属性结果的根标签."

"exclude-result-prefixes just removes the xmlns:foo="" attributes on the root tag of the result."

这是一个反例:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:z="z:z" exclude-result-prefixes="z">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
  <z:x xmlns:z="z:z">
   <z:y/>
  </z:x>
 </xsl:template>
</xsl:stylesheet>

将此转换应用于任何 XML 文档(未使用)时,结果是:

<z:x xmlns:z="z:z">
   <z:y/>
</z:x>

我们看到:

  1. 值为 (namespace-uri) "z:z" 的命名空间的命名空间节点(和定义)不会从顶部元素中移除(Markus Jarderot 称之为根标记").

  1. The namespace node (and definition) for the namespace with value (namespace-uri) "z:z" is not removed from the top element (what Markus Jarderot calls "root tag").

带有前缀 "z" 的命名空间根本不会从任何文字元素中删除.

The namespace with prefix "z" isn't removed from any literal element at all.

这表明了一个简单的事实,即指定 exclude-result-prefixes="yes" 不能删除不在 LRE(文字结果元素)上的命名空间,即使命名空间节点位于 LRE 上,但正在定义元素所属的命名空间.

That shows the simple fact that specifying exclude-result-prefixes="yes" cannot remove a namespace if it isn't on an LRE (Literal Result Element) and even if a namespace node is on an LRE but is defining the namespace to which the element belongs.

为了从它所属的命名空间中移除一个元素,或者从非 LRE 元素中移除命名空间,我们需要指定一些额外的处理.

In order to remove an element from the namespace it belongs to, or remove namespaces from non-LRE elements, we need to specify some additional processing.

一个例子是用以下内容替换传统的身份规则:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()[not(self::*)]">
     <xsl:copy>
       <xsl:apply-templates/>
     </xsl:copy>
 </xsl:template>

 <xsl:template match="*">
  <xsl:element name="{local-name()}">
   <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:attribute name="{local-name()}">
   <xsl:value-of select="."/>
  </xsl:attribute>
 </xsl:template>
</xsl:stylesheet>

上述转换将任何元素或属性替换为属于无命名空间"的相应元素或属性.它的一个潜在用途是将具有默认命名空间的文档转换为没有默认命名空间的文档.

The above transformation replaces any element or attribute with a corresponding element or attribute that belongs to "no namespace". One potential use for it is to convert a document with a default namespace to a document without such.

例如,当应用于以下源 XML 文档时:

<z:x xmlns:z="z:z">
    <z:y z:attr="someValue"/>
</z:x>

转换的结果是:

<x>
   <y attr="someValue"/>
</x>

最后警告:

如果将此转换应用于包含具有相同本地名称但属于两个不同命名空间的两个元素(或两个属性)的文档,则此转换可能是有害的——转换将这些元素(或属性)替换为都属于相同的命名空间(无命名空间).

This transformation may be harmful if applied on documents that contains two elements (or two attributes) that have the same local name but belong to two different namespaces -- the transformation replaces these with elements (or attributes) that both belong to the same namespace (no namespace).

这篇关于如何通过在 XSLT 中编写单个语句来避免输出中出现所有名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆