0

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

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
marcinn
  • 1,879
  • 2
  • 22
  • 46

3 Answers3

3

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

Dennis Münkle
  • 5,036
  • 1
  • 19
  • 18
3

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())]
Ro.
  • 1,525
  • 1
  • 14
  • 17
1

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

Skarab
  • 6,981
  • 13
  • 48
  • 86