Xquery node values where node starts with same text

2.4k Views Asked by At

How to create xquery to select node values where node name starts with some text. for example document

<doc>
   <cpv1>Value1</cpv1>
   <cpv2>Value2</cpv2>
   <cpv3>Value3</cpv3>
   <zzz>Hello world!</zzz>
</doc>

It should get Value1,Value2,Value3

3

There are 3 best solutions below

0
On

So DocumentRequest nodes contains Alias nodes. I select all the Alias nodes which start with a prefix val.

<ArrayOfDocumentRequest>
    <DocumentRequest>
        <Alias>
            prefix1_OtherText
        </Alias>
    </DocumentRequest>
    <DocumentRequest>
        <Alias>
            prefix2_OtherText
        </Alias>
    </DocumentRequest>
</ArrayOfDocumentRequest>

<F_PREFIXLIST>
    <prefixes>
        <p>prefix1</p>
        <p>prefix2</p>
        <p>prefix3</p>
    </prefixes>
</F_PREFIXLIST>

for $i in /ArrayOfDocumentRequest
    for $p in $F_PREFIXLIST/prefixes/p                 
        return $i/DocumentRequest/Alias[fn:starts-with(text(), $p/text())]
0
On

Here is a xpath expression which given you Value1, Value2, Value3: //*[substring(text(), 1,5) ="Value"]/text()

0
On

//doc/*[fn:starts-with(fn:local-name(), 'cpv')]/text()