Xml Xpath选择具有命名空间的同级属性的值
我正在努力获取定义的XPath,以从以下XML返回Xml Xpath选择具有命名空间的同级属性的值,xml,xpath,xml-namespaces,Xml,Xpath,Xml Namespaces,我正在努力获取定义的XPath,以从以下XML返回uniqueappversionid的值: <?xml version="1.0" encoding="UTF-8"?> <manifest package="air.com.vzw.Foo" android:versionCode="0" android:versionName="0.0.0" android:installLocation="auto"
uniqueappversionid
的值:
<?xml version="1.0" encoding="UTF-8"?>
<manifest package="air.com.vzw.Foo"
android:versionCode="0"
android:versionName="0.0.0"
android:installLocation="auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="FooAIR">
<meta-data android:name="autoOrients" android:value="true" />
<meta-data android:name="fullScreen" android:value="false" />
<meta-data android:name="uniqueappversionid"
android:value="b1e1bfa8-20b4-4724-a9c3-34b79bc50b8d" />
<meta-data android:name="initialcontent" android:value="FooAIR.swf" />
</application>
</manifest>
更具体地说,我需要从
元数据元素中获取android:value
属性的值,其中android:name
等于uniqueappversionid好的,可能有更好的方法,但我最终使用的是:
//meta-data[@*='uniqueappversionid']/@*[2]
我非常感谢您的建议,以改善这一点 将a
前缀绑定到http://schemas.android.com/apk/res/android
名称空间:
/manifest
/application
/meta-data[@a:name='uniqueappversionid']
/@a:value
您的问题与执行XPath查询的框架以及XPath查询本身的内容有很大关系。例如,在XSLT中可以这样做:
<stylesheet version="1.0"
xmlns="http://www.w3.org/1999/XSL/Transform"
xmlns:android="http://schemas.android.com/apk/res/android">
<template match="/">
<value-of select="//meta-data[@android:name = 'uniqueappversionid']/@android:value" />
</template>
</stylesheet>
这里设置名称空间的方法是通过XSLT元素上的xmlns属性,XPath查询嵌套在XSLT元素下。在一个典型的命令式编程语言环境中,您可能更需要通过对XML对象进行参数化来设置名称空间别名,并根据这些对象计算查询。要为这种环境指定答案,您必须了解XML框架的具体情况。切勿对属性使用位置谓词。XML解析器没有任何保留源代码顺序的义务。最好是//元数据[@*='uniqueappversionid']/*[.!='uniqueappversionid']
Shorter:/*[.='uniqueappversionid']/../.[.!='uniqueappversionid']
我使用了/manifest/application/metadata[@android:name='uniqueappversionid']/@android:value
不绑定任何内容,基本上实现了相同的功能。