xquerymarklogicmarklogic-10

how to find attribute in xml with specific values without putting specific element in the MarkLogic Xquery?


<book>
    <abc x="1">value</abc>
    <xyz>sadf</xyz>
</book>

<book>
    <ijk x="1">value</ijk>
    <xyz>sadf</xyz>
</book>

<book>
    <plm x="1">value</plm>
    <xyz>sadf</xyz>
</book>

I have xml like above and I want all the xml where attribute x=1(element name is not fixed). I can easily do this using XPath but I need to run this query on a huge data set, so do we have any cts query to make it faster? Or better way to query this?


Solution

  • Unfortunately, the built-in cts:* functions that query attributes must have an element QName specified. If you don't know what elements might have the attribute, then it may be easier to create a field index or TDE.

    One option would be to create a path field with a generic XPath, such as //*/@x or /book/*/@x

    If you enable the options for field value searches, then you could use a query such as:

    cts:field-value-query("x", "1")