返回 XML 不变的 XSL [英] XSL That Returns the XML unchanged

查看:27
本文介绍了返回 XML 不变的 XSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个 XSL 片段,它只返回未更改的 XML.这听起来微不足道,但我似乎无法在网络上的任何地方找到示例.有什么帮助吗?

I am looking for a XSL snippet that simply returns the XML unaltered. It sounds trivial but I can't seem to find an example anywhere on the web. Any help out there?

推荐答案

为了复制完整的 XML 文档,必须有一个与根匹配的模板.这可能是:

In order to copy the complete XML document, it is necessary to have a template that matches the root. This might be:

    

     <xsl:template match="/">

    

     <xsl:template match="node()">

然后复制当前节点(根节点)就足够了:

Then a single copying of the current node (the root node) is just sufficient:

     select="."/>

     <xsl:copy-of select="."/>

因此,这样一个完整的转换是:

So, one such complete transformation is:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
      <xsl:copy-of select="."/>
    </xsl:template>
</xsl:stylesheet>

虽然这可能是最简单的此类转换,但 XSLT 程序员使用另一种转换,广泛称为 identity 转换身份规则:

Although this is probably the simplest such transformation, XSLT programmers use another one, widely known as the identity transformation or the identity rule:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

身份转换被认为是最基本的 XSLT 设计模式之一并被广泛使用的原因是,通过用其他更具体的模板覆盖此模板规则,人们可以非常轻松地执行各种操作,否则会很困难.示例是删除具有特定名称或满足某些其他条件的特定(一组)元素、重命名特定元素、更改特定元素的名称空间、创建特定元素的新子项或兄弟项等.

The reson the identity transformation is considered to be one the most fundamental XSLT design patterns and to be so massively used, is that by overriding this template rule with other, more specific templates, one can very easily perform a variety of operations that otherwise will be difficult. Examples are deleting a particular (set of) element(s) that have a specific name or satisfy some other condition, renaming particular elements, changing the namespace of particular elements, creating new children or siblings of particular elements, ..., etc.

有关使用身份转换的更多信息和代码片段,请查看此处.

For more information and code snippets using the identity transformation, do look here.

这篇关于返回 XML 不变的 XSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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