XPath和CDATA [英] XPath and CDATA

查看:459
本文介绍了XPath和CDATA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




是否可以从XML文档中检索CDATA章节?如果是这样,

有人会给我一个语法示例吗?


干杯


Aidy

解决方案

aidy写道:

是否可以从XML文档中检索CDATA章节?



是的,只需将其解析为带有Perl的文本文件,或者某些东西。这可能不是你想要的。


如果你想使用XML,你可能会通过一些
$带有DOM接口的b $ b工具。你不能通过这个

看到一个CDATA部分,因为那里只有_isn''t_一个。 CDATA不是XML-Infoset的一部分,它只是

该文件的特定序列化的人工制品。
http://www.w3.org/TR/ 2004 / REC-xml-in ... 40204 /#省略


< a> foo< / a>



< a><![CDATA [foo]]>< / a>

不仅无法区分通过DOM查看时,它们绝对是_the_same_thing_。它们中的任何一个都是同一个有效的基础XML内容的序列化。


< a><![CDATA [< foo>] ]>< / A>当然可以通过查看它的

文本进行分析,你可以为

some_encooding_maybe_a_cdata_is_needed设置一个标志,但这是一个问题

写作,而不是阅读。

这是XML的基础(至少对于良好的XML设计而言),你不能看到CDATA和类似的问题,你也不在乎他们。

_使用工具,不要打他们。透明度很好,你不在乎

是否有CDATA在那里。你的应用程序同样有效

无论如何也不需要知道。如果确实如此,那么你正在做一些非常错误的事情。


XPath数据模型认为CDATA只是

文本的替代标记,所以不,你不能将CDATA部分与任何其他形式的

文本区分开来。当然,你可以检索他们的文字价值......


-

Joe Kesselman /小心病人的愤怒。 - John Dryden





aidy写道:

是否有可能从XML文档中检索CDATA部分?如果是这样,
有人会给我一个语法示例吗?




XPath数据模型不区分普通文本节点和

CDATA部分节点W3C DOM的工作方式,在XPath 1.0数据中

模型只有你选择的文本节点

text()

所以对于XPath而言,无论你是否有例如

< element> Kibo& amp; Xibo< / element>



< element><![CDATA [Kibo& Xibo]]>< / element>

您将使用例如

/ element / text()

来选择文本节点XPath及其字符串值是

Kibo& Xibo


-


Martin Honnen
http://JavaScript.FAQTs.com/


Hi,

Is it possible to retrieve CDATA Sections from an XML document? If so,
could someone give me a syntax example?

Cheers

Aidy

解决方案

aidy wrote:

Is it possible to retrieve CDATA Sections from an XML document?



Yes, just parse it up as a textfile with Perl, or somesuch. This is
probably not what you want.

If you want to work with XML though, you''ll probably work through some
tool with a DOM interface. You can''t see a CDATA section through this
because there just _isn''t_ one. CDATA is not part of XML-Infoset, it''s
solely an artefact of the particular serialisation of that instance of
that file.
http://www.w3.org/TR/2004/REC-xml-in...40204/#omitted

<a>foo</a>
and
<a><![CDATA[foo]]></a>
are not only "indistinguishable" when viewed through the DOM, they are
absolutely _the_same_thing_. Either of them is an equally valid
serialisation of the same underlying XML content.

<a><![CDATA[<foo>]]></a> can of course be analysed by looking at its
text and you could set a flag for
"some_encooding_maybe_a_cdata_is_needed", but that''s a question of
writing, not reading.
It''s fundamental to XML (or at least to good XML design) that you can''t
see CDATA and similar issues, and you don''t care about them either.
_Use_ the tools, don''t fight them. Transparency is good, you don''t care
about whether there''s a CDATA in there or not. Your app works equally
well either way and doesn''t need to know. If it does, then you''re doing
something badly wrong.


The XPath data model considers CDATA to be just an alternative markup of
text, so no, you can''t distinguish CDATA sections from any other form of
text. You can, of course, retrieve their text value...

--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden




aidy wrote:

Is it possible to retrieve CDATA Sections from an XML document? If so,
could someone give me a syntax example?



The XPath data model does not distinguish between normal text nodes and
CDATA section nodes the way the W3C DOM does, in the XPath 1.0 data
model there are only text nodes which you select with
text()
So for XPath it does not matter whether you have e.g.
<element>Kibo &amp; Xibo</element>
or
<element><![CDATA[Kibo & Xibo]]></element>
you would use e.g.
/element/text()
to select the text node with XPath and its string value is
Kibo & Xibo

--

Martin Honnen
http://JavaScript.FAQTs.com/


这篇关于XPath和CDATA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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