Key
Key

Reputation: 396

xQuery get value of attribute with multiples

I am trying to get the exact text of an attribute using xQuery. The issue i am finding is that i have multiple elements with the same name that have an attribute with a colon in the text.

Example

    <body>
      <tag xlink:href="1.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="2.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="3.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="4.jpg" position="float" orientation="portrait"/>
    </body>

Some examples i have used are the following

for $graphic in $body//tag
  return element tag { ($graphic//@*[name()="xlink:href"]) },

element tag { $body//graphic/@*[name()="xlink:href"] }

Both of my current examples give some output but not whats expected. The intended output that i am looking for is...

1.jpg
2.jpg
3.jpg
4.jpg

Any ideas?

Upvotes: 0

Views: 1757

Answers (3)

Honza Hejzl
Honza Hejzl

Reputation: 884

For me this works:

xquery version "3.0";

declare namespace xlink = "http://xlink.im";

let $body := <body>
      <tag xlink:href="1.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="2.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="3.jpg" position="float" orientation="portrait"/>
      <tag xlink:href="4.jpg" position="float" orientation="portrait"/>
    </body>
for $graphic in $body//tag
  return $graphic/@xlink:href

Simply try to return $graphic/@xlink:href.

Upvotes: 2

Tyler Replogle
Tyler Replogle

Reputation: 1339

The part before a colon in an xquery element or QName is the namespace.

In your example xlink would be the name space. In the example XML xlink should be define in the XML. A lot of times its defined in the root node.

If you need to xpath to an element with a namespace make sure the namespace is define in your xquery and just put it in the xpath

body/tag/@xlink:href

Upvotes: 0

har07
har07

Reputation: 89305

You can use simple XPath expression to return the attributes :

$body//tag/@*[name()="xlink:href"]/data()

Given the HTML snippet in question, output of the above XPath/XQuery is exactly as what you are looking for, see demo

Alternatively, if you mean to get single string value in such format :

string-join($body//tag/@*[name()="xlink:href"], "&#10;")

Upvotes: 1

Related Questions