2017-09-13 32 views
3

我试图使用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吗?

我很感谢您的回复。

+0

'// book_xref/xref'选择'xref'元素,如果你想'book'元素,那么你需要选择它们,例如'//书[.// book_xref /外部参照]'。 –

+0

@MartinHonnen感谢您的回复。如果我这样做,它仍然显示我需要省略的''节点。我只希望''数据与书籍ID一起,而不管在''内但在''之外有多少其他节点存在。希望我的问题有道理! – Fenil

+2

对于CSV使用,例如''// book // book_xref/xref/string-join((ancestor :: book/@ id,@type,@type_id,。),',')'。 –

回答

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

即使使用16GB的RAM,转换/ XML提取的代码执行也会耗尽主内存。该查询是否可以进行任何类型的性能调整? – Fenil

+0

那么你使用哪个XQuery实现,你如何运行查询?输入XML文档在使用16 GB RAM时内存不足时的大小是多少?你确定你给XQuery处理器提供了可用的RAM吗?例如,当你运行一个Java程序时,默认情况下它不会分配所有可用的内存,这些选项是'java.exe'来增加/控制堆空间。您可能会更好地询问特定于您的XQuery实现的新问题以及XML输入大小的必要细节。 –

+0

我正在加载一个512 MB的XML文件到Windows 7 Pro上的BaseX GUI中,并带有16 GB的RAM。我在查询窗口中运行查询。不确定如何检查BaseX是否提供可用的RAM。我如何检查?如果这需要更多细节,我可以为此创建一个新问题。 – Fenil