Xpath 谓词匹配 <![CDATA[sometext]]> [英] Xpath Predicate to match <![CDATA[sometext]]>
问题描述
我的 xml 元素内容包含特殊字符,例如 <,>,[,].我想找到匹配内容的元素 并用一些新值替换它,例如
代码>.
My xml element content is having special characters like <,>,[,]. I want to find element macthing the content <![CDATA[someText]]>
and replace it with some new value like <![CDATA[newValue]]>
.
<property name="sourcePath">
<string><![CDATA[someText]]></string>
</property>`
我正在尝试使用以下 xsl 模板来执行此操作.但它不起作用.
I am trying to do this using below xsl template. But it is not working.
<xsl:template match="property[string='<![CDATA[someText]]>']">
<xsl:element name="string">
<xsl:text disable-output-escaping="yes"><![CDATA[newValue]]></xsl:text>
</xsl:element>
请帮忙.
推荐答案
CDATA 部分未保存在 XML INFOSET 中,因此其内容只是常规文本——一个完整的文本节点或文本节点的一部分.
A CDATA section is not presereved in the XML INFOSET, so its contents is just regular text -- a complete text node or a part of a text node.
在您的情况下,CDATA 部分是完整的文本节点,因此您可以:
<xsl:template match="property[string='someEscapedText']">
<string>newEscapedValue</string>
</xsl:template>
您不需要使用 DOE 或 CDATA -- 只需对 NewValue 中的字符进行转义 并使用 cdata-section-elements="string"
属性
声明.
You don't need to use DOE or CDATA -- just escape the characters in NewValue and use the cdata-section-elements="string"
attribute of the <xsl:output>
declaration.
这里是完整的解决方案:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output cdata-section-elements="string"
omit-xml-declaration="yes" indent="yes"/>
<xsl:template match=
"property[string='<p>Hello, World</p>']">
<string><p>Hello, You</p></string>
</xsl:template>
</xsl:stylesheet>
当此转换应用于以下 XML 文档时:
<property>
<string><![CDATA[<p>Hello, World</p>]]></string>
</property>
产生想要的、正确的结果:
<string><![CDATA[<p>Hello, You</p>]]></string>
这篇关于Xpath 谓词匹配 <![CDATA[sometext]]>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!