csv - How to use XQuery to extract specific XML records and output in comma delimited format? -


i'm trying extract <xref> data along book ids using xquery (i'm new this).

here input data:

  <book id="6636551">     <master_information>         <book_xref>             <xref type="fiction" type_id="1">72771kam3</xref>             <xref type="non_fiction" type_id="2">us72771kam36</xref>         </book_xref>     </master_information>     <book_details>         <price>24.95</price>         <publish_date>2000-10-01</publish_date>         <description>an in-depth @ creating applications xml.</description>     </book_details>   </book>   <book id="119818569">     <master_information>         <book_xref>             <xref type="fiction" type_id="1">070185ul5</xref>             <xref type="non_fiction" type_id="2">us070185ul50</xref>         </book_xref>     </master_information>     <book_details>         <price>19.25</price>         <publish_date>2002-11-01</publish_date>         <description>a former architect battles corporate zombies,    evil sorceress, , own childhood become queen    of world.</description>     </book_details>   </book>   <book id="119818568">   <master_information>         <book_xref>             <xref type="fiction" type_id="1">070185uk7</xref>             <xref type="non_fiction" type_id="2">us070185uk77</xref>         </book_xref>     </master_information>     <book_details>         <price>5.95</price>         <publish_date>2004-05-01</publish_date>         <description>after collapse of nanotechnology    society in england, young survivors lay    foundation new society.</description>     </book_details>   </book>   <book id="119818567">     <master_information>         <book_xref>             <xref type="fiction" type_id="1">070185uj0</xref>             <xref type="non_fiction" type_id="2">us070185uj05</xref>         </book_xref>     </master_information>     <book_details>         <price>4.95</price>         <publish_date>2000-09-02</publish_date>         <description>when carla meets paul @ ornithology    conference, tempers fly feathers ruffled.</description>     </book_details>   </book> 


expected output format 1:

  <book id="6636551">     <master_information>         <book_xref>             <xref type="fiction" type_id="1">72771kam3</xref>             <xref type="non_fiction" type_id="2">us72771kam36</xref>         </book_xref>     </master_information>   </book> 

xquery i'm using format 1:

  for$x in //book_xref/xref   return $x 

question format 1: tried including book id separately it's included in output doesn't match expected format mentioned above. how book id fetched in output per format?


expected output format 2 (comma delimited):

  book_id, xref_type, xref_type_id, xref   6636551, fiction, 1, 72771kam3   6636551, non_fiction, 2, us72771kam36   119818569, fiction, 1, 070185ul5   119818569, non_fiction, 2, us070185ul50   etc. 

question format 2: how can output in comma delimited format through xquery? need stick xslt that?

i appreciate response.

for csv can use string-join i.e. 4 values can use

//book//book_xref/xref/string-join((ancestor::book/@id, @type, @type_id, .), ',') 

which give sequence of strings record data; if want single string header line , data lines can use string-join:

string-join(('book_id,xref_type,xref_type_id,xref', //book//book_xref/xref/string-join((ancestor::book/@id, @type, @type_id, .), ',')), '&#10;') 

for transformation/xml extraction reconstruct book elements xref descendants , add master_information e.g.

//book[.//book_xref/xref]/<book id="{@id}">{master_information}</book> 

Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -