2017-05-23 15 views
1

顯示不同的值,我的代碼不輸出節點。當我嘗試輸出作者的不同名稱和姓氏時,用節點

distinct-values(
    for $b in doc("book.xml")//book/author 
order by $b/name, $b/surname 
return <author>{string($b/name), string($b/surname)}</author> 
) 

用這個xml。

<?xml version="1.0" encoding="UTF-8"?> 
<bib> 
    <book year="1994"> 
     <title>TCP/IP Illustrated</title> 
     <author> 
      <surname>Stevens</surname> 
      <name>W.</name> 
     </author> 
     <editorial>Addison-Wesley</editorial> 
     <price> 65.95</price> 
    </book> 

    <book year="1992"> 
     <title>Advan Programming for Unix environment</title> 
     <author> 
      <surname>Stevens</surname> 
      <name>W.</name> 
     </author> 
     <editorial>Addison-Wesley</editorial> 
     <price>65.95</price> 
    </book> 

    <book year="2000"> 
     <title>Millenium</title> 
     <author> 
      <surname>Falk</surname> 
      <name>Lombardo</name> 
     </author> 
     <editorial>Morgan Kaufmann editorials</editorial> 
     <price>19.50</price> 
    </book> 

    <book year="2000"> 
     <title>Data on the Web</title> 
     <author> 
      <surname>Abiteboul</surname> 
      <name>Serge</name> 
     </author> 
     <author> 
      <surname>Buneman</surname> 
      <name>Peter</name> 
     </author> 
     <author> 
      <surname>Suciu</surname> 
      <name>Dan</name> 
     </author> 
     <editorial>Morgan Kaufmann editorials</editorial> 
     <price>39.95</price> 
    </book> 


    <book year="1999"> 
     <title> Economics of Technology for Digital TV</title> 
     <editor> 
      <surname>Gerbarg</surname> 
      <name>Darcy</name> 
      <afiliacion>CITI</afiliacion> 
     </editor> 
     <editorial>Kluwer Academic editorials</editorial> 
     <price>129.95</price> 
    </book> 
</bib> 

下面的代碼輸出:

Dan Suciu 
Lombardo Falk 
Peter Buneman 
Serge Abiteboul 
W. Stevens 

但我想:

<author>Dan Suciu</author> 
<author>Lombardo Falk</author> 
<author>Peter Buneman</author> 
<author>Serge Abiteboul</author> 
<author>W. Stevens</author> 

有人能告訴我什麼,我做錯了什麼?謝謝。

回答

1

問題是distinct-values()期望並返回一系列原子值,即您的<author/>元素首先被轉換爲字符串。

所以相反,您希望先獲得不同的名稱,然後再將它們轉換爲元素。這樣的事情應該返回預期的結果:

for $name in distinct-values(
    for $b in doc("book.xml")//book/author 
    order by $b/name, $b/surname 
    return string-join(($b/name, $b/surname), " ") 
) 
return <author>{ $name }</author> 
+0

我認爲這是完全是我想要的,但我不知道這怎麼可能,謝謝,現在我得到的問題是,它是按姓氏排序等等我首先得到了姓氏,然後是名字,我怎樣才能將「蘇慈丹」的輸出翻譯成「丹素秀」? – TrnChristian

+0

@TrnChristian對不起,修好了,我的錯誤 – dirkk

+0

是的!正是我想要的,謝謝你的幫助! :) – TrnChristian

相關問題