在不使用字符映射的情况下在 xslt 样式表输出中保留实体 [英] Retaining entity in xslt stylesheet output without using character-map

查看:81
本文介绍了在不使用字符映射的情况下在 xslt 样式表输出中保留实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们哪里出错了?

当我在撒克逊语上用 xslt 2 处理这个 xml 时:

When I process this xml with xslt 2 on saxon he:

 <data>
      <grab>Grab me and print me back &quot;</grab>
 </data>

使用此样式表:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:template match="/">
        <xsl:apply-templates select="/data/grab"/>
    </xsl:template>

    <xsl:template match="/data/grab">
        <node><xsl:value-of select="text()"/></node>
    </xsl:template>

</xsl:stylesheet>

我得到这个输出:

<?xml version="1.0" encoding="UTF-8"?><node>Grab me and print me back "</node>

但我想保留 &quot;在输出的xml中.因此我们需要添加一个字符映射:

But I want to retain the &quot; in the outputted xml. Therefore we needed to add a character-map:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:character-map name="specialchar">
        <xsl:output-character character="&quot;" string="&amp;quot;"/>
    </xsl:character-map>
    <xsl:output method="xml" indent="no"  use-character-maps="specialchar"/>
    <xsl:template match="/">
        <xsl:apply-templates select="/data/grab"/>
    </xsl:template>

    <xsl:template match="/data/grab">
        <node><xsl:value-of select="text()"/></node>
    </xsl:template>

</xsl:stylesheet>

其中保留了 &quot;实体...,恕我直言,看起来冗长而丑陋,

Which retains the &quot; entity... which, imho, looks verbose and ugly,

这真的有必要吗?没有更优雅的选择吗?如果不是,这背后的原理是什么?

Is this really necessary? Is there not a more elegant alternative? If not, what is the rationale behind this?

推荐答案

在体系结构上,XSLT 将 XDM 树转换为 XDM 树,它不会将词法 XML 转换为词法 XML.XDM 树不区分 &quot;",就像它们区分 <a id="5"/>> 和 <a id = '5'></a>.事实上,您编写 XML 的方式中的任意和不相关的差异对 XSLT 程序员是隐藏的,这在很大程度上是设计的, 并且可以更轻松地编写正确的转换.

Architecturally, XSLT transforms XDM trees to XDM trees, it does not transform lexical XML to lexical XML. XDM trees do not distinguish between &quot; and ", any more than they distinguish between <a id="5"/> and <a id = '5'></a>. The fact that arbitrary and irrelevant differences in the way you write the XML are hidden from the XSLT programmer is very much by design, and makes it much easier to write correct transformations.

现在肯定有保存实体引用的用例:特别是像 &author; 这样的语义实体引用,它们可能在不同的场合采用不同的值.但是实体引用并不是满足该要求的特别好的解决方案.XInclude 通常更好.并且该论点不适用于像 &quot; 这样的字符引用:很难找到处理 &quot;" 的好的用例 不同,你当然没有提供.

Now there are certainly use cases for preserving entity references: particularly semantic entity references like &author; that might take different values on different occasions. But entity references aren't a particularly good solution to that requirement; XInclude is usually better. And the argument doesn't apply to character references like &quot;: it's really hard to see a good use case for treating &quot; and " differently, and you certainly haven't provided one.

在实际层面,Saxon 无法保留 &quot;,即使它想要,因为它不知道它在那里:XML 解析器(将词法 XML 转换为 XDM) 不会通知对应用程序的字符引用.同样,这是设计使然:理论是应用程序不应该知道也不应该关心.它的一大优点是,我们不会从未能满足这种可能性的应用程序开发人员那里收到数以百万计的 SO 问题.

At a practical level, Saxon couldn't preserve the &quot; even if it wanted to, because it doesn't know it's there: the XML parser (which converts lexical XML to XDM) doesn't notify character references to the application. Again, that's by design: the theory is that applications shouldn't know and shouldn't care. And it has the great virtue that we don't get zillions of SO questions from application developers who failed to cater for the possibility.

这篇关于在不使用字符映射的情况下在 xslt 样式表输出中保留实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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