XQuery格式化输出

时间:2014-04-16 17:03:09

标签: xml xquery

我正在尝试格式化输出

我有这个xml

<?xml version="1.0" encoding="UTF-8"?> 
<personnel> 
    <person id="Big.Boss"> 
        <name> 
            <family>Boss</family> 
            <given>Big</given> 
        </name> 
        <email>chief@oxygenxml.com</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>one@oxygenxml.com</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="two.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Two</given> 
        </name> 
        <email>two@oxygenxml.com</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="three.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Three</given> 
        </name> 
        <email>three@oxygenxml.com</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="four.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Four</given> 
        </name> 
        <email>four@oxygenxml.com</email> 
        <link manager="Big.Boss"/> 
    </person> 
    <person id="five.worker"> 
        <name> 
            <family>Worker</family> 
            <given>Five</given> 
        </name> 
        <email>five@oxygenxml.com</email> 
        <link manager="Big.Boss"/> 
    </person> 
</personnel> 

这是XQuery

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

我得到了这个输出

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

如何获取此输出,以空格分隔

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

3 个答案:

答案 0 :(得分:1)

只需选择元素的字符串值,而不是元素本身:

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

您也可以使用union运算符|以更紧凑的方式编写此代码:

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

答案 1 :(得分:0)

这些是其他可能的选择。

使用fn:data() -

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

使用fn:data() -

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

使用fn:string-join() -

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

答案 2 :(得分:0)

$b/family

如您所知,这将返回节点<family>Boss</family>,并包含任何子节点。在这种情况下,唯一的孩子是你想要的(#PCDATA)。

要在节点内返回文本(#PCDATA),请使用text()

$b/family/text()

你的回归应该是:

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