2017-09-13 5 views
3

XQuery를 사용하여 도서 ID와 함께 <xref> 개의 데이터 만 추출하려고합니다 (이 글을 처음 사용하는 경우).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> 


예상 출력 포맷 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는 포맷 (1)에 대해 사용하고 :

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

여기

입력 데이터 인 fo에 대한 질문 rmat 1 : 도서 ID를 별도로 포함 시키려고 했으므로 출력에 포함되지만 위에서 언급 한 예상 형식과 일치하지 않습니다. 책 ID를 포맷별로 출력물에 가져 오는 방법은 무엇입니까?


예상 출력 형식 2 (쉼표로 구분) : 형식 2

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. 

질문 : 어떻게 쉼표 출력이 XQuery를 통해 형식을 구분받을 수 있나요? 그것을 위해 XSLT를 고수해야합니까?

답변 해 주셔서 감사합니다.

+0

'book_xref/xref'는'xref' 요소를 선택합니다. 만약'book' 요소가 필요하면 그것을 선택해야합니다. '// book [.// book_xref/xref]'. –

+0

@MartinHonnen이 응답 해 주셔서 감사합니다. 그렇게하면 생략해야하는' '노드가 여전히 표시됩니다. 단순히 ''내부에 다른 노드가 얼마나 많은지와 관계없이 책 번호와 함께 ''데이터를 원하지만 ''외부에 넣기 만하면됩니다. 내 질문이 이해되기를 바랍니다! – Fenil

+2

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

답변

2

CSV를 들어, 당신은 기록 데이터와 문자열의 시퀀스를 제공 할

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

를 사용하여 이러한 네 개의 값, 즉 string-join를 사용할 수있다; xref 후손과 book 요소를 재구성 변환/XML 추출을위한

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

을하고 master_information 예를 들어, 추가 : 헤더 행에 하나의 문자열과 그 데이터를 라인을 원하는 경우 다른 사용할 수있는 문자열을 조인

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

변환/XML 추출을위한 코드 실행은 RAM이 16GB 인 경우에도 주 메모리가 부족합니다. 해당 쿼리에서 어떤 종류의 성능 튜닝이 가능합니까? – Fenil

+0

어떤 XQuery 구현을 사용합니까? 어떻게 쿼리를 실행합니까? 그리고 16GB RAM으로 메모리가 부족할 때 입력 XML 문서의 크기는 어느 정도입니까? XQuery 프로세서에 사용 가능한 RAM을 제공 하시겠습니까? 예를 들어 자바 프로그램을 실행할 때 기본적으로 사용 가능한 모든 메모리를 할당하지 않고, 힙 공간을 늘리거나 제어하기위한'java.exe '옵션이 있습니다. XQuery 구현에 대한 새로운 질문과 XML 입력 크기에 대한 필요한 세부 사항을 묻는 것이 좋습니다. –

+0

16GB의 RAM이있는 Windows 7 Pro의 BaseX GUI에 512MB XML 파일을로드하고 있습니다. 쿼리 창에서 쿼리를 실행합니다. BaseX가 사용 가능한 RAM을 제공하는지 확인하는 방법을 모릅니다. 어떻게 확인할 수 있습니까? 자세한 내용이 필요하면 이에 대한 새로운 질문을 만들 수 있습니다. – Fenil

2

XQuery는 소스가 하나의 XML 문서 또는 파일 시스템에 또는 XML 데이터베이스에 저장된 XML 문서의 모음인지, XML 데이터로부터 CSV 파일을 생성 할 수있는 좋은 방법입니다. XQuery에는 여러 가지 접근 방식이 있습니다. XQuery 3.1 배열 구조와 직렬화 기능을 사용하여 데이터를 행과 셀로 구성하려면 https://github.com/CliffordAnderson/XQuery4Humanists/blob/master/05-Generating-JSON-and-CSV.md의 자습서를 참조하십시오.