2017-01-23 2 views
0

XQuery를 사용하여 XML 문서를 정렬하려고합니다. 첫 번째 수준의 정렬은 부모 요소 중 하나를 기준으로합니다.XQuery로 정렬 : 중첩 FLWOR

두 번째 수준의 정렬은 연결된 하위 요소 중 하나입니다.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<FEATURES xmlns="http://www.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <FEATURE> 
     <abbreviation>CANAL</abbreviation> 
     <LINKED_PROPERTIES> 
      <LP> 
       <abbreviation>zltinf</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>altalt</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>betbet</abbreviation> 
       <category>C</category> 
       <remarks /> 
      </LP> 
     </LINKED_PROPERTIES> 
     <description>Water Canal</description> 
     <code>23</code> 
     <relation_type /> 
    </FEATURE> 
    <FEATURE> 
     <abbreviation>AREA</abbreviation> 
     <LINKED_PROPERTIES> 
      <LP> 
       <abbreviation>zltzlt</abbreviation> 
       <category>A</category> 
       <remarks>zolt zolt</remarks> 
      </LP> 
      <LP> 
       <abbreviation>altalt</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>betbet</abbreviation> 
       <category>C</category> 
       <remarks /> 
      </LP> 
     </LINKED_PROPERTIES> 
     <description>Area under administration</description> 
     <code>1</code> 
     <relation_type /> 
    </FEATURE> 
    <FEATURE> 
     <abbreviation>BUOY</abbreviation> 
     <LINKED_PROPERTIES> 
      <LP> 
       <abbreviation>zltinf</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>altalt</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>betbet</abbreviation> 
       <category>C</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>infinf</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>altalt</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>betbet</abbreviation> 
       <category>C</category> 
       <remarks /> 
      </LP> 
     </LINKED_PROPERTIES> 
     <description>Buoy on water</description> 
     <code>18</code> 
     <relation_type /> 
    </FEATURE> 
    <FEATURE> 
     <abbreviation>DRONE</abbreviation> 
     <LINKED_PROPERTIES> 
      <LP> 
       <abbreviation>zltinf</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>beltam</abbreviation> 
       <category>A</category> 
       <remarks /> 
      </LP> 
      <LP> 
       <abbreviation>betbet</abbreviation> 
       <category>C</category> 
       <remarks /> 
      </LP> 
     </LINKED_PROPERTIES> 
     <description>Drones Inland</description> 
     <code>2</code> 
     <relation_type /> 
    </FEATURE> 
</FEATURES> 

이 문서에는 표준에 속하는 2 가지 레벨의 기능이 포함되어 있습니다. 그래서 분명히 XML 문서를 수정할 수 없습니다. 위의 데이터는 익명화 된 데이터입니다.

내 XQuery는 다음과 같습니다.

<FEATURES xmlns="http://www.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    { 
    let $doc := doc("features.xml") 
    for $feature in $doc/FEATURES/FEATURE 
    order by $feature/abbreviation ascending 
    return 
    <FEATURE> 
     { 
     for $ft in $feature 
     return if ($ft/child::element/name() eq 
     "LINKED_PROPERTIES")then 
     <LINKED_PROPERTIES_XXXXXXXX>{ 
      for $lp in $ft/LINKED_PROPERTIES/LP 
      order 
      by $lp/abbreviation ascending 
      return $lp 
      } 
     </LINKED_PROPERTIES_XXXXXXXX> 
     else $ft/*} 
    </FEATURE> 
    } 
</FEATURES> 

하위 수준에서 적절한 동등 조건을 가져올 수 없어서 하위 수준에서 원하는 정렬을 얻을 수 없습니다.

몇 가지 이유로 DOM/XSL로 돌아가고 싶지 않습니다. 도와주세요. 당신이 FEATURE이 CHID 요소 LINKED_PROPERTIES이 있는지 테스트하는 의미 경우

답변

1

내부의 for은 기능의 하위 항목에 대해 반복해야합니다. 그렇지 않으면 반복 할 항목이 하나만 있습니다. 또한 나무 아래쪽에있는 LINKED_PROPERTIES은 $ ft가 해당 노드에 이미 있으므로 제거해야하며 다른 자식은 그대로 반환해야합니다. 마지막으로 종류 테스트가 적절하지 않았습니다 (child::element()이어야 함).

<FEATURES xmlns="http://www.example.com/" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">{ 
    let $doc := doc("features.xml") 
    for $feature in $doc/FEATURES/FEATURE 
    order by $feature/abbreviation ascending 
    return <FEATURE>{ 
    for $ft-child in $feature/child::element() 
    return if ($ft-child/name() eq "LINKED_PROPERTIES") 
      then <LINKED_PROPERTIES_XXXXXXXX>{ 
      for $lp in $ft-child/LP 
      order by $lp/abbreviation ascending 
      return $lp 
      }</LINKED_PROPERTIES_XXXXXXXX> 
      else $ft-child 
    }</FEATURE> 
}</FEATURES> 

위의 쿼리는 oXygen에서 성공합니다.

+0

저는 산소가 없기 때문에 BaseH를 시도했습니다. 내가 관찰 한 것은 xquery를 실행 한 후 상위 기능 요소와 하위 링크 속성을 혼합 한 것입니다. –

+0

XQuery가 표준화되었으므로 BaseX에서도 작동해야합니다. 언제든지 잘못된 결과가있을 경우 쿼리와 출력을 붙여 넣으십시오. 기꺼이 도와 드리겠습니다. –

+0

그 지금 일하고있어 라! –

0

:

element라는 이름의 자식 요소를 검색하기 때문에 훨씬 이해가되지 않습니다 동일한에 대한 귀하의 XQuery를 시도, 그것은 참으로 평가하지 않습니다
return if ($ft/LINKED_PROPERTIES) then 
    <LINKED_PROPERTIES_XXXXXXXX> 
    { 
     ..... 
    } 
    </LINKED_PROPERTIES_XXXXXXXX> 

($ft/child::element), 요소 이름이 "LINKED_PROPERTIES" (/name() eq "LINKED_PROPERTIES")인지 다시 테스트하십시오.