如何使用XQuery基于逗号分隔格式的多个条件提取特定的XML记录?

时间:2017-10-02 18:38:53

标签: xml xquery

输入文件:

<?xml version="1.0" encoding="UTF-8"?> 
        <books>
            <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>
                <global_information>
                    <ratings>
                        <rating agency="ABC Agency" type="Author Rating">A++</rating>
                        <rating agency="DEF Agency" type="Author Rating">A+</rating>
                        <rating agency="DEF Agency" type="Book Rating">A</rating>
                    </ratings>
                </global_information>
                <country_info>
                    <country_code>US</country_code>
                </country_info>
            </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>
                <global_information>
                    <ratings>
                        <rating agency="ABC Agency" type="Author Rating">A+</rating>
                        <rating agency="ABC Agency" type="Book Rating">A</rating>
                        <rating agency="DEF Agency" type="Author Rating">A</rating>
                        <rating agency="DEF Agency" type="Book Rating">B+</rating>
                    </ratings>
                </global_information>
                <country_info>
                    <country_code>CA</country_code>
                </country_info>
            </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>
                <global_information>
                    <ratings>
                        <rating agency="ABC Agency" type="Author Rating">A+</rating>
                        <rating agency="ABC Agency" type="Book Rating">A+</rating>
                        <rating agency="DEF Agency" type="Author Rating">B++</rating>
                        <rating agency="DEF Agency" type="Book Rating">A+</rating>
                    </ratings>
                </global_information>
                <country_info>
                    <country_code>UK</country_code>
                </country_info>
            </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>
                <global_information>
                    <ratings>
                        <rating agency="ABC Agency" type="Author Rating">B+</rating>
                        <rating agency="ABC Agency" type="Book Rating">A+</rating>
                        <rating agency="DEF Agency" type="Author Rating">B++</rating>
                        <rating agency="DEF Agency" type="Book Rating">A+</rating>
                    </ratings>
                </global_information>
                <country_info>
                    <country_code>US</country_code>
                </country_info>
            </book>
        </books>

我写了一个XQuery来获取CSV格式的特定XML记录:

for $x in string-join(('book_id, xref_type, xref, country, desc, rating_agency, rating_type, rating', //book//global_information/ratings/rating[@type='Author Rating' and .=('A+','B++')]/string-join((ancestor::book/@id, ancestor::book//book_xref/xref/@type, ancestor::book//book_xref/xref, ancestor::book//country_info/country_code, ancestor::book//book_details/description, @agency, @type, .), ',')), '&#10;')
return $x

预期输出为:

book_id, xref_type, xref, country, desc, rating_agency, rating_type, rating
6636551,Fiction,72771KAM3,US,An in-depth look at creating applications with XML.,DEF Agency,Author Rating,A+
6636551,Non_Fiction,US72771KAM36,US,An in-depth look at creating applications with XML.,DEF Agency,Author Rating,A+
119818569,Fiction,070185UL5,CA,A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.,ABC Agency,Author Rating,A+
119818569,Non_Fiction,US070185UL50,CA,A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.,ABC Agency,Author Rating,A+
etc.

我希望book_ids可以重复,因为我没有对xref_type进行过滤,因此它应该在不同的行中显示它们,但它不会。生成的输出如下:

book_id, xref_type, xref, country, desc, rating_agency, rating_type, rating
6636551,Fiction,Non_Fiction,72771KAM3,US72771KAM36,US,An in-depth look at creating applications with XML.,DEF Agency,Author Rating,A+
119818569,Fiction,Non_Fiction,070185UL5,US070185UL50,CA,A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.,ABC Agency,Author Rating,A+

当我过滤xref_type =&#34;小说&#34;我必须在xref / @ type和xref上执行此操作。它有效,但有更好的方法吗?

基本上,我的问题可以分解为三个小问题:

  1. 我怎样才能拥有&#34;小说&#34;和&#34;非小说&#34;列出的项目 给定book_id的单独行?
  2. 有没有更好的方法来编写此代码的条件?
  3. 如果在特定条件下缺少数据时,如何以输出空白值的方式编写条件?这是自动完成的吗?
  4. 感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

在评估 XPath 表达式ancestor::book//book_xref/xref/@type时,上下文是单个rating元素,结果是序列 type="..."个属性。因此,当您汇编 CSV 行时,祖先book元素的所有类型的书籍参考都被视为一个(可以是的序列>项的)。

如果要将每个单独的项目分隔为单独的 CSV 行,则必须使用例如递归迭代序列。一个for循环。我使用booklet绑定到变量,并将所有xref的循环绑定到:

string-join(
  (
    'book_id, xref_type, xref, country, desc, rating_agency, rating_type, rating',
    for $rating in //book//global_information/ratings/rating[@type='Author Rating' and .=('A+','B++')]
    let $book := $rating/ancestor::book
    for $xref in $book//xref
    return (
      string-join(
        (
          $book/@id,
          $xref/@type,
          $xref/text(),
          $book//country_info/country_code,
          $book//book_details/description/text(),
          $rating/@agency,
          $rating/@type,
          $rating/text()
        ),
        ','
      )
    )
  ),
  '&#10;'
)

如果你想避免向后轴(有时候有点尴尬),你也可以首先迭代book,然后再遍历你感兴趣的rating

string-join(
  (
    'book_id, xref_type, xref, country, desc, rating_agency, rating_type, rating',
    for $book in //book
    for $rating in $book/global_information/ratings/rating[@type='Author Rating' and .=('A+','B++')]
    for $xref in $book//xref
    [...]