2013-04-22 2 views
0

저는 xQuery를 처음 접했고 다음을 수행하기 위해 쿼리를 작성했습니다.두 요소를 xQuery로 정렬하십시오.

연도별로 정렬 된 책 및 기사 표시.

나는 다음을 시도했다.

for $b in doc("myDB.xml")/myDB 
order by $b/book/year, $b/article/year 
return $b/book, $b/article 

또는

for $b in doc("myDB.xml")/myDB 
order by $b//year 
return $b/book, $b/article 

그것은 아무것도 정렬 아니에요.

XML입니다.

<myDB> 
    <book> 
     <title>ebXML : Concepts and Application</title> 
     <author> 
      <last_name>Gibb</last_name> 
      <first_name>Brian</first_name> 
     </author> 
     <year>2003</year> 
     <publisher>Wiley</publisher> 
    </book> 

    <article> 
     <title>test title</title> 
     <author> 
      <last_name>Gibb</last_name> 
      <first_name>Brian</first_name> 
     </author> 
     <year>2003</year> 
     <publisher>test Wiley</publisher> 
    </article> 

    <book> 
     <title>XML: Visual QuickStart Guide</title> 
     <author> 
      <last_name>Goldberg</last_name> 
      <first_name>Kevin</first_name> 
     </author> 
     <author> 
      <last_name>Doe</last_name> 
      <first_name>John</first_name> 
     </author> 

     <year>2008</year> 
     <publisher>Peachpit Press</publisher> 
    </book> 
    <book> 
     <title>Beginning XML</title> 
     <author> 
      <last_name>Hunter</last_name> 
      <first_name>David</first_name> 
     </author> 
     <year>2007</year> 
     <publisher>Wrox</publisher> 
    </book> 
    <book> 
     <title>Learning XML </title> 
     <author> 
      <last_name>Ray</last_name> 
      <first_name>Erik</first_name> 
     </author> 
     <author> 
      <last_name>Smith</last_name> 
      <first_name>David</first_name> 
     </author> 
     <year>2003</year> 
     <publisher>O'Reilly</publisher> 
    </book> 
    <book> 
     <title>Professional XML</title> 
     <author> 
      <last_name>Evjen</last_name> 
      <first_name>Bill</first_name> 
     </author> 
     <year>2007</year> 
     <publisher>Wrox</publisher> 
    </book> 
    <article> 
     <title>FileNet Integrates a New XML Editor for Non-Techies</title> 
     <author> 
      <last_name>Roe</last_name> 
      <first_name>David</first_name> 
     </author> 
     <journal>CMS Wire</journal> 
     <year>2009</year> 
    </article> 
    <article> 
     <title>The Groundbreaking Potential of Codeless Ajax</title> 
     <author> 
      <last_name>Dusoe</last_name> 
      <first_name>Jim</first_name> 
     </author> 
     <journal>Tech News World</journal> 
     <year>2009</year> 
     <pages>4</pages> 
    </article> 
    <article> 
     <title>Windows and .NET Watch: Recognizing DSL opportunities</title> 
     <author> 
      <last_name>O'Brien</last_name> 
      <first_name>Larry</first_name> 
     </author> 
     <journal>Software Development Times</journal> 
     <year>2009</year> 
    </article> 
    <article> 
     <title>An embedded XML Database: Oracle Berkeley DB XML</title> 
     <author> 
      <last_name>Gibb</last_name> 
      <first_name>Brian</first_name> 
     </author> 
     <journal>The Register</journal> 
     <year>2003</year> 
     <pages>8</pages> 
    </article> 
    <article> 
     <title>Open Source PostgreSQL 8.3 Better Suited For Web Applications </title> 
     <author> 
      <last_name>Babcock</last_name> 
      <first_name>Charles</first_name> 
     </author> 
     <journal>Information Week</journal> 
     <year>2008</year> 
     <pages>1</pages> 
    </article> 
</myDB> 

출력은 전체 xml 자체 뒤에 원래 순서대로 서적 및 기사 요소가옵니다.

정말 FOR이 필요합니까? 아무도 도와 줄 수 있니?

답변

1

확인이 하나

let $doc := doc("Your.xml)/myDB 
for $eachBook in $doc/child::* 
order by $eachBook/year 
return $eachBook 

출력 :

<?xml version="1.0" encoding="UTF-8"?> 
<book> 
    <title>ebXML : Concepts and Application</title> 
    <author> 
     <last_name>Gibb</last_name> 
     <first_name>Brian</first_name> 
    </author> 
    <year>2003</year> 
    <publisher>Wiley</publisher> 
    </book> 
<article> 
    <title>test title</title> 
    <author> 
     <last_name>Gibb</last_name> 
     <first_name>Brian</first_name> 
    </author> 
    <year>2003</year> 
    <publisher>test Wiley</publisher> 
    </article> 
<book> 
    <title>Learning XML </title> 
    <author> 
     <last_name>Ray</last_name> 
     <first_name>Erik</first_name> 
    </author> 
    <author> 
     <last_name>Smith</last_name> 
     <first_name>David</first_name> 
    </author> 
    <year>2003</year> 
    <publisher>O'Reilly</publisher> 
    </book> 
<article> 
    <title>An embedded XML Database: Oracle Berkeley DB XML</title> 
    <author> 
     <last_name>Gibb</last_name> 
     <first_name>Brian</first_name> 
    </author> 
    <journal>The Register</journal> 
    <year>2003</year> 
    <pages>8</pages> 
    </article> 
<book> 
    <title>Beginning XML</title> 
    <author> 
     <last_name>Hunter</last_name> 
     <first_name>David</first_name> 
    </author> 
    <year>2007</year> 
    <publisher>Wrox</publisher> 
    </book> 
<book> 
    <title>Professional XML</title> 
    <author> 
     <last_name>Evjen</last_name> 
     <first_name>Bill</first_name> 
    </author> 
    <year>2007</year> 
    <publisher>Wrox</publisher> 
    </book> 
<book> 
    <title>XML: Visual QuickStart Guide</title> 
    <author> 
     <last_name>Goldberg</last_name> 
     <first_name>Kevin</first_name> 
    </author> 
    <author> 
     <last_name>Doe</last_name> 
     <first_name>John</first_name> 
    </author> 

    <year>2008</year> 
    <publisher>Peachpit Press</publisher> 
    </book> 
<article> 
    <title>Open Source PostgreSQL 8.3 Better Suited For Web Applications </title> 
    <author> 
     <last_name>Babcock</last_name> 
     <first_name>Charles</first_name> 
    </author> 
    <journal>Information Week</journal> 
    <year>2008</year> 
    <pages>1</pages> 
    </article> 
<article> 
    <title>FileNet Integrates a New XML Editor for Non-Techies</title> 
    <author> 
     <last_name>Roe</last_name> 
     <first_name>David</first_name> 
    </author> 
    <journal>CMS Wire</journal> 
    <year>2009</year> 
    </article> 
<article> 
    <title>The Groundbreaking Potential of Codeless Ajax</title> 
    <author> 
     <last_name>Dusoe</last_name> 
     <first_name>Jim</first_name> 
    </author> 
    <journal>Tech News World</journal> 
    <year>2009</year> 
    <pages>4</pages> 
    </article> 
<article> 
    <title>Windows and .NET Watch: Recognizing DSL opportunities</title> 
    <author> 
     <last_name>O'Brien</last_name> 
     <first_name>Larry</first_name> 
    </author> 
    <journal>Software Development Times</journal> 
    <year>2009</year> 
    </article> 
+0

그것은 어떤 결과를 보여주는 것. 실행할 xBase를 사용하고 있습니다. – nommyravian

+0

doc ("Your.xml")을 변경 했습니까? XML 파일의 URI를 입력해야합니다. 또는 myDB 노드를 $ doc –

+0

에 전달할 수 있습니다. 이름을 doc ("myDB.xml")으로 변경했습니다. [XPST0003] 불완전한 위치 단계 – nommyravian

관련 문제