简体   繁体   English

Marklogic 6如何使用xquery获取属性值?

[英]Marklogic 6 how to get attribute value using xquery?

XML:1 XML:1

<?xml version="1.0" encoding="UTF-8"?>
<PE uri="/MY/Cases/ILJ/ilj2010_1_00042.xml">
          <P name="antony" value="cse"/>
          <P name="type" value="reported"/>
          <P name="year" value="2010"/>
          <P name="part" value="1"/>
          <P name="volume" value="2"/>
          <P name="decdate-year" value="2010"/>
          <P name="decdate-month" value="01"/>
          <P name="decdate-day" value="27"/>
</PE>

XML:2 XML:2

<?xml version="1.0" encoding="UTF-8"?>    
<PE uri="/MY/Cases/ILJ/ilj2010_1_00042.xml">
          <P name="antony" value="cse"/>
          <P name="type" value="reported"/>
          <P name="year" value="2010"/>
          <P name="part" value="1"/>
          <P name="volume" value="1"/>
          <P name="decdate-year" value="2010"/>
          <P name="decdate-month" value="01"/>
          <P name="decdate-day" value="27"/>
</PE>

I'm using the below Xquery to differentiate the multiple xml. 我正在使用下面的Xquery来区分多个xml。 But I'm not getting the exact output. 但我没有得到确切的输出。 Here my question is how to filter the element with multiple attributes? 这里我的问题是如何过滤具有多个属性的元素? I want to get the result for Volume =2 means it should return only the first xml. 我想得到Volume = 2的结果意味着它应该只返回第一个xml。

MY query 我的查询

xquery version "1.0-ml";
declare namespace xs = "http://www.w3.org/2001/XMLSchema";

let $value1  := "antony"
let $value2 := "cse"
let $value3  := "year"
let $value4 := "2010"
let $value5  := "volume"
let $value6 := "2"
let $value7  := "part"
let $value8 := "1"

    for  $uri1 in cts:uris((),(), (
          cts:element-query(xs:QName("P"),

            cts:and-query(
            (
cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value1)  ,
cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value2),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value3),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value4),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value5),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value6),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value7),
cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value8)

))
 )) )

return doc($uri1)

The best approach is to change the XML representation and manifest the semantics of the model with element or attribute names instead of attribute values, as in: 最好的方法是更改​​XML表示并使用元素或属性名称而不是属性值来显示模型的语义,如下所示:

<pe>
    <type>reported</type>
    <part>1</part>
    <volume>1</volume>
    <date>2010-01-27</date>
</pe>

Then the query becomes something like the following: 然后查询变为如下所示:

cts:element-query(xs:QName("pe"), (
    cts:element-value-query(xs:QName("type"),"reported"),
    cts:element-value-query(xs:QName("part"),"1"),
    cts:element-value-query(xs:QName("volume"),"1"),
    cts:element-value-query(xs:QName("date"),"2010-01-27")
    ))

This approach also lets you create useful range indexes or path range indexes on the values. 此方法还允许您在值上创建有用的范围索引或路径范围索引。

Hoping that helps, 希望有所帮助,

For each of the name/value attribute pairs, use a cts:element-query() specifying the P element, with a cts:and-query() containing cts:element-attribute-value-query() criteria for both of the attributes for that particular P element. 对于每个名称/值属性对,使用cts:element-query()指定P元素,使用cts:and-query()包含cts:element-attribute-value-query()标准。该特定P元素的属性。

This will ensure that the criteria has to be met for both attributes on the same P element, rather than just a test to ensure that there is an attribute with that value anywhere within the document. 这将确保必须满足同一P元素上的两个属性的标准,而不仅仅是测试以确保在文档中的任何位置存在具有该值的属性。

xquery version "1.0-ml";
declare namespace xs = "http://www.w3.org/2001/XMLSchema";

let $value1  := "antony"
let $value2 := "cse"
let $value3  := "year"
let $value4 := "2010"
let $value5  := "volume"
let $value6 := "2"
let $value7  := "part"
let $value8 := "1"

for  $uri1 in cts:uris((),(), (
      cts:element-query(xs:QName("P"),
        cts:and-query((
         cts:element-query(xs:QName("P"), cts:and-query((
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value1),
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value2)))),
         cts:element-query(xs:QName("P"), cts:and-query((
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value3),
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value4)))),
          cts:element-query(xs:QName("P"), cts:and-query((
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value5), 
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value6)))),
         cts:element-query(xs:QName("P"), cts:and-query((
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("name"),$value7),
            cts:element-attribute-value-query(xs:QName("P"),xs:QName("value"),$value8))))
     )) )) )

return doc($uri1)

cts:element-query()

Searches for matches in the specified element and all of its descendants. 搜索指定元素及其所有后代中的匹配项。 If the specified query in the second parameter has any cts:element-attribute-*-query constructors, it will search attributes directly on the specified element and attributes on any descendant elements. 如果第二个参数中的指定查询具有任何cts:element-attribute - * - 查询构造函数,它将直接在指定元素上搜索属性,并在任何后代元素上搜索属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM