xQuery使用倍数获取属性的值

xQuery使用倍数获取属性的值,xquery,Xquery,我正在尝试使用xQuery获取属性的确切文本。我发现的问题是,我有多个同名的元素,它们的属性在文本中带有冒号 范例 <body> <tag xlink:href="1.jpg" position="float" orientation="portrait"/> <tag xlink:href="2.jpg" position="float" orientation="portrait"/> <tag xlink

我正在尝试使用xQuery获取属性的确切文本。我发现的问题是,我有多个同名的元素,它们的属性在文本中带有冒号

范例

    <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>
我当前的两个示例都给出了一些输出,但不是预期的结果。我正在寻找的预期输出是

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

有什么想法吗?

您可以使用简单的XPath表达式返回属性:

$body//tag/@*[name()="xlink:href"]/data()
考虑到所讨论的HTML片段,上述XPath/XQuery的输出与您要查找的完全相同,请参见

或者,如果要以这种格式获取单个字符串值:

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

可以使用简单的XPath表达式返回属性:

$body//tag/@*[name()="xlink:href"]/data()
考虑到所讨论的HTML片段,上述XPath/XQuery的输出与您要查找的完全相同,请参见

或者,如果要以这种格式获取单个字符串值:

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

xquery元素或QName中冒号前面的部分是名称空间

在您的示例中,xlink将是名称空间。在本例中,应在XML中定义XML xlink。很多时候它是在根节点中定义的

如果需要使用xpath将名称空间转换为元素,请确保在xquery中定义了名称空间,并将其放在xpath中

body/tag/@xlink:href

xquery元素或QName中冒号前面的部分是名称空间

在您的示例中,xlink将是名称空间。在本例中,应在XML中定义XML xlink。很多时候它是在根节点中定义的

如果需要使用xpath将名称空间转换为元素,请确保在xquery中定义了名称空间,并将其放在xpath中

body/tag/@xlink:href
对我来说,这很有效:

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
xquery版本“3.0”;
声明命名空间xlink=”http://xlink.im";
让$body:=
对于$body//标记中的$graphic
返回$graphic/@xlink:href
只需尝试返回
$graphic/@xlink:href

对我来说,这很有效:

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
xquery版本“3.0”;
声明命名空间xlink=”http://xlink.im";
让$body:=
对于$body//标记中的$graphic
返回$graphic/@xlink:href

只需返回
$graphic/@xlink:href

谢谢,这太完美了。谢谢,这太完美了。