如何使用XQuery以逗号分隔格式提取特定的XML记录和输出?

时间:2017-09-13 18:57:08

标签: xml csv xslt xpath xquery

我尝试使用XQuery(我刚才这个)提取<TextBlock Text="{Binding DateTimeModel.CurrentDateTime}"/> 数据以及他们的图书ID。

这是输入数据:

<xref>


预期输出格式 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_details>
        <price>24.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications with 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, 
  an evil sorceress, and her own childhood to become queen 
  of the 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 the collapse of a nanotechnology 
  society in England, the young survivors lay the 
  foundation for a 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 at an ornithology 
  conference, tempers fly as feathers get ruffled.</description>
    </book_details>
  </book>

我用于格式1的XQuery:

  <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>

格式1的问题:我尝试单独包含图书ID,以便将其包含在输出中,但它与我上面提到的预期格式不匹配。如何在每个格式的输出中获取书籍ID?


预期输出格式 2 (以逗号分隔):

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

格式2的问题:如何通过XQuery以逗号分隔格式输出?我需要坚持使用XSLT吗?

感谢您的回复。

2 个答案:

答案 0 :(得分:2)

对于CSV,您可以使用string-join,即可以使用

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

这将给出一系列带有记录数据的字符串;如果你想要一个带有标题行的字符串和那些数据行,你可以使用另一个字符串连接:

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

对于转换/ XML提取,使用book后代重建xref元素并添加master_information,例如

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

答案 1 :(得分:2)

XQuery是一种从XML数据生成CSV文件的好方法,无论源是单个XML文档还是存储在文件系统或XML数据库中的XML文档集合。 XQuery中可能采用多种方法。对于使用XQuery 3.1数组结构和序列化工具将数据存储到行和单元格中的数据,请参阅https://github.com/CliffordAnderson/XQuery4Humanists/blob/master/05-Generating-JSON-and-CSV.md上的教程。