xml - 使用 XPath 从命名空间 XML 有条件地提取值
问题描述
我需要使用 XPath 从 XML 中提取一些信息……XML 如下所示……
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" >
<title type="text">DataEntities</title>
<m:count>4</m:count>
<entry>
<id>0001</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf597f3aee4f0d93e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0001</d:Product>
<d:Version>6.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0002</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf345f3aee4f0d34e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0002</d:Product>
<d:Version>1.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server02.mydomain.com</d:Hostname>
<d:Technology>Java</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0003</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf123f3aee4f0d33e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0003</d:Product>
<d:Version>5.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
<entry>
<id>0004</id>
<title type="text">DataEntities</title>
<content type="application/xml">
<m:properties>
<d:internalId>5b1daf345f3aee4f0d34e1ed</d:internalId>
<d:datasetVersion>2</d:datasetVersion>
<d:Product>PRODUCT0004</d:Product>
<d:Version>4.0.0</d:Version>
<d:Middleware>Apache WebServer</d:Middleware>
<d:Versionmiddleware>2.2.31</d:Versionmiddleware>
<d:Hostname>server01.mydomain.com</d:Hostname>
<d:Technology>PHP</d:Technology>
</m:properties>
</content>
</entry>
</feed>
我需要找到位于同一主机名上的产品,例如修复像“server01.mydomain.com”这样的服务器以提取 PRODUCT0001、PRODUCT0003 和 PRODUCT0004(注意不是 PRODUCT0002),或者修复服务器像“server02.mydomain.com”一样提取,结果只有PRODUCT0002。
我试过用
//d:Product
它有效,结果是
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0001</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0002</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0003</d:Product>'
Element='<d:Product xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">PRODUCT0004</d:Product>'
我不知道如何表明我需要“仅”d:Hostname
价值为“server01.mydomain.com”的产品
我试过使用
//d:Product/@d:Hostname['server01.mydomain.com']
但它不起作用。
关于我必须使用的 XPath 语法有什么建议吗?
解决方案
假设您已将XML 中声明的命名空间前缀正确绑定到 XPath 库,这个 XPath,
//m:properties[d:Hostname="server01.mydomain.com"]/d:Product
将根据要求返回字符串值等于的那些d:Product
元素。m:properties
d:Hostname
"server01.mydomain.com"
推荐阅读
- javascript - React-Loadable 跳过渲染“加载组件”
- java - html 格式的印地语文本中出现不需要的换行符
- visual-studio - 本地 Azure 存储帐户 - 在哪里可以找到连接字符串
- c - 程序使用 strtok [C] 崩溃
- sql - Oracle:仅存在重复行的 SUM 列
- python - 尝试使用 Pymongo 将数据从 MongoDB 解析为 JSON,然后在 DOM 中返回一个列表
- java - 如何在 spring-mvc 中跳过杰克逊时区校正?
- angular - 离子应用程序中的 location.back() 路由给出“输入无效视图”错误
- ios - UIPopoverPresentationController : 多个 allowedArrowDirections 顺序优先级
- r - 在 R 函数中定义布尔 TRUE/FALSE 变量