user3490634
user3490634

Reputation: 9

XQuery formatting the output

I am trying to formatting the output

I have this xml

<?xml version="1.0" encoding="UTF-8"?> 
<personnel> 
    <person id="Big.Boss"> 
        <name> 
            <family>Boss</family> 
            <given>Big</given> 
        </name> 
        <email>[email protected]</email> 
        <link subordinates="one.worker two.worker three.worker four.worker 
            five.worker"/> 
    </person> 
    <person id="one.worker"> 
        <name> 
            <family>Worker</family> 
            <given>One</given> 
        </name> 
        <email>[email protected]</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="two.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Two</given> 
        </name> 
        <email>[email protected]</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="three.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Three</given> 
        </name> 
        <email>[email protected]</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="four.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Four</given> 
        </name> 
        <email>[email protected]</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="five.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Five</given> 
        </name> 
        <email>[email protected]</email> 
        <link manager="Big.Boss"/> 
    </person> 
</personnel> 

and this is XQuery

for $b in doc("Persons.xml")/personnel/person/name
where $b/family = "Boss"
return
 <persons>
 <found>  { $b/family, $b/given}</found> 
</persons>

I get this output

<?xml version="1.0" encoding="UTF-8"?>
<persons>
   <found>
      <family>Boss</family>
      <given>Big</given>
   </found>
</persons>

How can I get this output instead, separated by space

<persons> 
  <found>Big  Boss</found> 
</persons> 

Upvotes: 0

Views: 985

Answers (3)

Matt Stevens
Matt Stevens

Reputation: 1124

$b/family

As you note, this returns the node <family>Boss</family>, complete with any children nodes. In this case, the only child is the (#PCDATA) you want.

To return the text (#PCDATA) within the node, use text()

$b/family/text()

Your return should be:

<persons>
  <found>
    {
      $b/family/text(),
      " ",
      $b/given/text()
    }
  </found>
</persons>

Upvotes: 0

John
John

Reputation: 2852

These are other possible options.

Using fn:data() -

<persons>
  <found>  { data($b/family), data($b/given)}</found> 
</persons>

Using fn:data() -

<persons>
  <found>  { data($b/family), data($b/given)}</found> 
</persons>

Using fn:string-join() -

<persons>
  <found>  { string-join(($b/family, $b/given), " ")}</found> 
</persons>

Upvotes: 0

joemfb
joemfb

Reputation: 3056

Simply select the string value of the elements, instead of the elements themselves:

<found>{
  $b/family/fn:string(),
  $b/given/fn:string()
}</found> 

You could also write this in a more compact way by using the union operator |:

<found>{ $b/(family|given)/fn:string() }</found> 

Upvotes: 1

Related Questions