想更换"与 "从一个 xml [英] want to replace " with " from a xml

查看:34
本文介绍了想更换"与 "从一个 xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将 CDATA 应用于 xml 中的节点部分,所有 <> 都被 &lt; 替换> 和 &gt;.另外我想用 &quot; 替换 ".

Hi i have applied CDATA to a sectio of nodes in a xml all the < and > are replaced by &lt; and &gt;. I want to replace " with &quot; in addition.

我需要对 CDATA 部分进行哪些更改以将 " 替换为 &quot;

what changes do i need to make to the CDATA part to replace " with &quot;

推荐答案

你没有说清楚你在做什么,所以很难简单地回答.

You don't say clearly what you are doing, so it's hard to answer simply.

如果你的意思是

  • 您在要提供给 XSLT 样式表的 XML 文档中添加了 CDATA 标记部分;
  • 与输入中的 CDATA 部分相对应的样式表输出部分引用了实体 ltgt,其中输入带有尖括号(所以 <p class="greeting">Hello, world</p> 变成 &lt;p class="greeting"&gt;Hello, world!&lt;/p&;gt;,这就是你想要的;和
  • 您希望 " 在输出中也不按字面出现,而是替换为对实体的引用 quot
  • you have added a CDATA marked section in an XML document you are feeding to an XSLT stylesheet;
  • the portion of the stylesheet's output which corresponds to the CDATA section in the input has references to the entities lt and gt where the input has angle brackets (so <p class="greeting">Hello, world</p> becomes &lt;p class="greeting"&gt;Hello, world!&lt;/p&gt;, and this is what you desire; and
  • you would like " not to appear literally in the output either, but be replaced by a reference to the entity quot

then 实现您的目标的一种方法是编写一个模板来处理文本节点,该模板测试的存在",在第一个将文本节点分为左部分和右部分",写出左边部分,写出一个&符号,写出quot;,然后用字符串的右边部分递归调用自己.

then one way to achieve your aim is to write a template to handle text nodes, which tests for the presence of ", splits the text node into left-part and right-part on the first ", writes out the left part, writes out an ampersand, writes out quot;, and then calls itself recursively with the right part of the string.

以下样式表说明了该模式:

The following stylesheet illustrates the pattern:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">

  <xsl:template match="doc">
    <xsl:element name="doc">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <xsl:template match="text()" name="escape-quot">
    <xsl:param name="s" select="."/>
    <xsl:choose>
      <xsl:when test="contains($s,'&quot;')">
        <xsl:variable name="sL" 
          select="substring-before($s,'&quot;')"/>
        <xsl:variable name="sR" 
          select="substring-after($s,'&quot;')"/>
        <xsl:value-of select="$sL"/>
        <xsl:text>&amp;quot;</xsl:text>
        <xsl:call-template name="escape-quot">
          <xsl:with-param name="s" select="$sR"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$s"/>
      </xsl:otherwise>
    </xsl:choose>    
  </xsl:template>
</xsl:stylesheet>

我们可以将其应用于以下输入以查看结果:

We can apply it to the following input to see the result:

<doc>Hi. This is a test.
<![CDATA[<p class="greeting">Hello, 
world!</p>]]>
</doc>

我得到的结果是,我猜想,你在找什么.

The result I get is, I conjecture, what you are looking for.

<?xml version="1.0"?>
<doc><p>Hi. This is a test.</p>
<p>&lt;p class=&amp;quot;greeting&amp;quot;&gt;Hello, 
world!&lt;/p&gt;</p>
</doc>

如果这不是您想要的,您可以尝试更详细地解释您的问题.在这种情况下,提供 (a) 当前代码的关键位,(b) 示例输入,(c) 您当前获得的输出示例,以及对它有什么问题的描述,总是一个好主意, 和 (d) 您希望输出的样例.(保持示例和代码简短——您希望提供尽可能最小的完整工作示例,以便读者可以重现您的问题.)

If that wasn't what you wanted, you might try explaining your question in more detail. It's always a good idea in cases like this to provide (a) the key bits of your current code, (b) sample input, (c) a sample of the output you're currently getting, with a description of what's wrong with it, and (d) a sample of what you would like the output to look like. (Keep both the samples and the code short -- you want to provide the smallest possible complete working example so readers can recreate your problem.)

这篇关于想更换&quot;与 &amp;quot;从一个 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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