如果镜像输入数据,XSL 文档会是什么样子? [英] How does an XSL document look like if the mirrors the input data?

查看:28
本文介绍了如果镜像输入数据,XSL 文档会是什么样子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型的XSL用法是:

XML1.xml -> *transformed using xsl* -> XML2.xml

如果我只想镜像输入数据,XSL 文档是什么样子的?

How does an XSL document look like, if I want to simply mirror the input data?

例如:

XML1.xml -> *transformed using xsl* -> XML1.xml

推荐答案

一个 XSL 文档是什么样子的,如果我想简单地镜像输入数据?

How does an XSL document look like, if I want to simply mirror the input data?

这个问题的答案不止一个,但所有答案都可以命名为身份转换":

There are more than one answers to this question, however all of them could be named "Identity Transform":

  1. 这是最短、最简单、最高效、最不灵活、不可扩展且无用的 身份转换.

经典身份规则,每个人都知道(或应该知道):

The classical identity rule, which everybody knows (or should know):

_

<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()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
</xsl:stylesheet>

这仍然是非常简短的单模板转换,它是更具可扩展性和有用性的身份转换,也称为身份规则".使用和覆盖身份转换是最基本和最强大的 XSLT 设计模式,允许在短短几行中解决常见的复制和替换/重命名/删除/添加问题.xslt 标签中可能 90% 以上的答案都使用这种形式的身份转换.

This is still very short, one-template transformation, which is so much more extensible and useful identity transform, known also as the "identity rule". Using and overriding the identity transform is the most fundamental and powerful XSLT design pattern, allowing to solve common copy and replace/rename/delete/add problems in just a few lines. Maybe 90%+ of all answers in the xslt tag use this form of the identity transform.

.3.细粒度控制身份规则,每个人都应该知道(而且很少有人知道):

.3. The fine-grained control identity rule, which everybody should know (and very few people know):

<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()|@*">
  <xsl:copy>
   <xsl:apply-templates select="@*|node()[1]"/>
  </xsl:copy>
  <xsl:apply-templates select="following-sibling::node()[1]"/>
 </xsl:template>
</xsl:stylesheet>

这类似于上面 2. 中定义的众所周知的身份规则,但它提供了对 XSLT 处理的更好控制.

This is similar to the generally known identity rule defined at 2. above, but it provides a finer control over the XSLT processing.

通常与 2. <xsl:apply-templates select="@*|node()"> 触发许多转换(对于所有属性和子节点),这可以以任何顺序甚至并行完成.有些任务我们不希望某些类型的节点在其他一些节点之后被处理,因此我们必须通过使用与不需要的节点匹配的空模板覆盖它并在特定模式下添加其他模板来探测身份规则的泄漏在时机成熟时"处理这些节点......

Typically with 2. the <xsl:apply-templates select="@*|node()"> triggers a number of transformations (for all attributes and child nodes), that can be done in any order or even in parallel. There are tasks where we don't want certain types of nodes to be processed after some other nodes, so we have to plumb the leakage of the identity rule with overriding it with empty templates matching the unwanted nodes and adding other templates in a specific mode to process these nodes "when the time comes"...

.3.更适合我们想要更多控制和真正顺序类型处理的任务.一些用 2. 很难解决的任务,用 3. 很容易解决.

.3. is more appropriate for tasks where we want more control and really sequential-type processing. Some tasks that are very difficult to solve with 2. are easy using 3.

这篇关于如果镜像输入数据,XSL 文档会是什么样子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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