【问题标题】:xslt transform with escape entity带有转义实体的 xslt 转换
【发布时间】:2016-03-25 13:10:50
【问题描述】:

我有一个 XML,我想使用以下 xslt 将属性值 (name="name") 更改为另一个 (name="value"):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<!--xsl:template match="text()"-->
<!--xsl:text select="." disable-output-escaping="yes" /-->
<!--xsl:value-of select="." disable-output-escaping="yes" />
   <xsl:copy-of select="child::*"/> 
  </xsl:template-->

<xsl:template match="@*|node()" mode="s">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="s"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@*|node()" mode="se">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="se"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="@name" mode="se">
    <xsl:attribute name="name">value</xsl:attribute>
</xsl:template>
<xsl:template match="tag5[@type='testtype']">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="s"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="tag6[@name='name']" mode="s">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="se"/>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

XML 输入 sn-p:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot version="2.0">
<Model id="111" name="test">
    <tag1 id="222" type="VERSION">
        <tag2 id="333" name="Version" value="2"/>
    </tag1>
    <tag3 id="444">
        <tag4 id="555" versionID="test/00001" name="name" definition="through test. &#xD;&#xA;" attrs="12 23"/>
        <tag4 id="666" versionID="test/00001" name="name" definition="through test 2. &#xD;&#xA;" messages="34 45"/>
    </tag3>
    <tag5 id="777" type="testtype">
        <tag6 id="888" name="name" value="667"/>
        <tag6 id="999" name="context" value="FIX 5.0"/>
    </tag5>
</Model>
</dataroot>

应用 xslt 后的 XML OUTPUT 为:

<?xml version="1.0" encoding="UTF-8"?>
<dataroot version="2.0">
<Model id="111" name="test">
    <tag1 id="222" type="VERSION">
        <tag2 id="333" name="Version" value="2"/>
    </tag1>
    <tag3 id="444">
        <tag4 id="555" versionID="test/00001" name="name" definition="through test. &#13;&#10;" attrs="12 23"/>
        <tag4 id="666" versionID="test/00001" name="name" definition="through test 2. &#13;&#10;" messages="34 45"/>
    </tag3>
    <tag5 id="777" type="testtype">
        <tag6 id="888" name="value" value="667"/>
        <tag6 id="999" name="context" value="FIX 5.0"/>
    </tag5>
</Model>
</dataroot>

xslt 基本上完成了预期的工作。然而,意外的转变:

&#xD;&#xA;   ->   &#13;&#10;   (unexpected transform)

我想保留原始实体。 我试过 disable-output-escape (请参阅 xslt 中的注释部分,将文本更改为@definition),不起作用。有什么建议吗?

顺便说一句,我使用 xsltproc。

提前致谢!

【问题讨论】:

  • 您不能使用 XSLT 保留字符引用,XSLT 处理器使用 XML 解析器来解析输入,引入具有 Unicode 字符值的节点树,然后使用 Unicode 字符。然后在完成任何必要的转义时对转换结果进行序列化。所以你不能用纯 XSLT 解决这个问题,你需要查看你的特定 XSLT 处理器及其序列化功能是否可以强制使用十六进制字符引用。
  • @MartinHonnen,我使用 xsltproc,有什么提示吗?
  • @delair 它有什么不同? &amp;amp;#xA;&amp;#10; 都是 equally valid representation of the same character. 你为什么要关心这个?
  • @michael.hor257k, XML 文件是一个模型,它是许多其他工具的来源。因此,每个属性都由不同的多个工具处理。我想尽量减少更改以避免意外。
  • 任何符合 XML 标准的工具都应该能够平等地处理这两种表示。在任何情况下,您都不能保留原始表示(正如 Martin Honnen 已经解释过的),您只能强制使用一种或另一种表示。在 XSLT 1.0 self 中可以做到这一点,通过用转义字符串(例如&amp;amp;#xA;)替换字符并使用 DOE 输出这些字符 - 但这是很多工作。

标签: xml xslt xslt-1.0 libxslt


【解决方案1】:

xslt 似乎无法处理这个要求,所以我在 perl 中做了。 谢谢大家!

【讨论】:

  • "xslt 无法处理这个要求" 我不是这么说的。
  • 您的 Perl 程序几乎肯定会引入对输入文档表示的精确方式的依赖。这是因为人们坚持编写这样的程序,而不是使用真正的 XML 解析器,所以你最终会遇到开始时遇到的困难:你不能生成任意 XML,你必须生成一些 XML 子集/方言,因为有人写过接收端的应用程序未正确解析 XML。因此,您最终陷入了一个恶性循环,在这种循环中,您使用专有语法交换数据,并失去了标准 XML 带来的所有好处。
  • @michael.hor257k,对不起,我可能误解了。但我无法轻松找到满足此要求的解决方案。
  • @MichaelKay,您的评论是有效的,但有时我们希望灵活地将技术用于不同目的。在这种情况下,我使用替代方案来满足需求仅仅是因为我无法通过 xslt 来实现。请随时发布您的答案,我会很乐意接受。
  • @delair 我在上面的评论中概述了一个可能的解决方案。我不打算详细说明,因为(1)正如我所说,这是很多工作,(2)我认为没有必要。 ——附言你不能用“新”格式测试目标应用程序吗?您可能正在尝试解决一个不存在(当然也不应该存在)的问题。
猜你喜欢
  • 2022-01-20
  • 1970-01-01
  • 2015-11-30
  • 2012-03-19
  • 2013-06-03
  • 2012-06-25
  • 2015-07-15
  • 2011-06-16
  • 2011-04-05
相关资源
最近更新 更多