问题描述
是否可以从XML文档中检索CDATA章节?如果是这样,
有人会给我一个语法示例吗?
干杯
Aidy
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
Hi,
Is it possible to retrieve CDATA Sections from an XML document? If so,
could someone give me a syntax example?
Cheers
Aidy
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 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 & 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!