Xpath to select value of sibling attribute with namespace

夙愿已清 提交于 2020-01-20 08:07:59

问题


I'm struggling to get an XPath defined to return the value of the uniqueappversionid from the following XML:

<?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>

More specifically, I need to get the value of the android:value attribute from the meta-data element with a android:name equal to uniqueappversionid.


回答1:


With a prefix bound to http://schemas.android.com/apk/res/android namespace:

/manifest
   /application
      /meta-data[@a:name='uniqueappversionid']
         /@a:value



回答2:


Ok, there's probably a better approach, but this is what I've ended up using:

//meta-data[@*='uniqueappversionid']/@*[2]

I'd greatly appreciate suggestions to improve this!




回答3:


Your question has as much to do with the framework in which you are executing your XPath query as with the content of the XPath query itself. For example, here's how you would do it in 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>

The means of namespace setup here is through xmlns attributes on the XSLT elements under which the XPath query is nested. In a typical imperative programming language environment, you're probably more likely to have to set up namespace aliases through parameterization of the XML object(s) against which you are evaluating the query. To specify an answer for such an environment, you'd have to get specific about the XML framework.



来源:https://stackoverflow.com/questions/5518096/xpath-to-select-value-of-sibling-attribute-with-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!