我试图使用XQuery只提取<xref>
数据以及他们的书籍ID(我是新手)。如何使用XQuery提取特定的XML记录并以逗号分隔格式输出?
下面是输入数据:
<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>
预期输出格式:
<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,我使用了格式1:
for$x in //book_xref/xref
return $x
问题为fo rmat 1:我试图单独包括book id,以便它包含在输出中,但它与上面提到的预期格式不匹配。我怎样才能获得书籍ID以及每格式的输出?
预期输出格式(逗号分隔):
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.
问题的格式2:我怎样才能在逗号输出通过XQuery的分隔的格式?我需要坚持使用XSLT吗?
我很感谢您的回复。
'// book_xref/xref'选择'xref'元素,如果你想'book'元素,那么你需要选择它们,例如'//书[.// book_xref /外部参照]'。 –
@MartinHonnen感谢您的回复。如果我这样做,它仍然显示我需要省略的''节点。我只希望''数据与书籍ID一起,而不管在''内但在''之外有多少其他节点存在。希望我的问题有道理! –
Fenil
对于CSV使用,例如''// book // book_xref/xref/string-join((ancestor :: book/@ id,@type,@type_id,。),',')'。 –