开发者

Xpath to select value of sibling attribute with namespace

开发者 https://www.devze.com 2023-02-21 04:10 出处:网络
I\'m struggling to get an XPath defined to return the value of the uniqueappversionid from the foll开发者_如何学JAVAowing XML:

I'm struggling to get an XPath defined to return the value of the uniqueappversionid from the foll开发者_如何学JAVAowing 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.


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

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


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!


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消