Xpath to select value of sibling attribute with namespace

旧时模样 提交于 2019-11-29 14:41:47

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.

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