如何使用 Xpath 访问此 xml 节点中的数据 (CDATA)?
我有下一个 XML:
<?xml version="1.0" encoding="UTF-8"?>
<mailAndMessageSettings>
<settings>
<add key="Url" value=""/>
<add key="UserName" value=""/>
<add key="Password" value=""/>
</settings>
<mail>
<subject>
Mp3 Submission
</subject>
<body>
<![CDATA[
<meta http-equiv="Content-Type" content="text/html; charset="utf-8""/>
<head></head>
<body>
<p>Hi,</p>
<p>Please find the attached mp3</p>
<p><a href="mymp3.mp33">here</a></p>
<p>Regards,</br>
Pete</p>
</body>
</html>
]]>
</body>
</mail>
</mailAndMessageSettings>
我想使用 XPath:
/mailAndMessageSettings/mail/body
但是,当我使用它时,它会选择从第一个 body 标记(正确)到 html 内的 body 标记的所有内容,而不是 XML 文档中的 body 标记...
如何在不包含 CDATA 标签的情况下选择正文 XML 内的所有 CDATA?
I have next XML:
<?xml version="1.0" encoding="UTF-8"?>
<mailAndMessageSettings>
<settings>
<add key="Url" value=""/>
<add key="UserName" value=""/>
<add key="Password" value=""/>
</settings>
<mail>
<subject>
Mp3 Submission
</subject>
<body>
<![CDATA[
<meta http-equiv="Content-Type" content="text/html; charset="utf-8""/>
<head></head>
<body>
<p>Hi,</p>
<p>Please find the attached mp3</p>
<p><a href="mymp3.mp33">here</a></p>
<p>Regards,</br>
Pete</p>
</body>
</html>
]]>
</body>
</mail>
</mailAndMessageSettings>
And I want to use an XPath:
/mailAndMessageSettings/mail/body
However when I use it, it is selecting everything from the first body tag (correct) to the body tag inside the html rather than the body tag in the XML document...
How can I select all of the CDATA inside the body XML without the CDATA tag included?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要获取CDATA节点,将其加载到单独的XmlDocument中,然后再次调用XPath查询。
You need to get CDATA node, load it to separate XmlDocument, and call XPath query again.
所以实际上我只是将外部 body 标签更改为 bodyHtml 标签......然后使用:
So actually I just changed the outer body tag to bodyHtml tag instead.... and then use:
解析器只会使 CDATA 可作为文本访问,因为这就是它的用途。
如果您没有验证这一点,并且您似乎没有将其声明为 HTML(这将限制双体标记的存在),我不确定为什么您需要
CDATA
声明。如果因为它是动态生成的,并且您不确定字符串中可能会生成什么,则应该将文本包装在CDATA
中动态生成的 html 中。The parser is only going to make the CDATA accessible as text, because that's what it's used for.
If you aren't validating this, and you don't appear to be declaring it as HTML (which would restrict the presence of double body tags), I'm not sure why you need the
CDATA
declaration. If it's because it's dynamically generated, and you're not sure what might be generated in the strings, you should be wrapping the text in the dynamically generated html inCDATA
instead.使用
/mailAndMessageSettings/mail/body/text()
。Use
/mailAndMessageSettings/mail/body/text()
.