와 일부 노드의 조상을 가져 오기 :내가 예를 들어 XML을 XQuery를 재귀
<types>
<type type="A">
<type type="A.A"/>
<type type="A.B">
<type type="A.B.A"/>
</type>
<type type="A.C"/>
</type>
</types>
지금, 내가 좋아하는 입력 문자열이 "A.B.A을". 나는 "type"속성의 값처럼이 문자열을 가진 모든 조상과 자체 노드를 얻길 원합니다.
<types>
<type type="A"/>
<type type="A.B"/>
<type type="A.B.A"/>
</types>
(간단 여기 유형 단지 예와하지 않는 당연히 이름을, 그래서 나는 문자열 기능을 사용할 수 없습니다) 나는 두 개의 XML 스키마가
을 : 그것은 의미 TypesTree.xsd 및 Ancestros.xsd을.
둘 다 "유형"및 "유형"요소가 있습니다. 그래서 어쩌면
, 나는 $ 유형이 유형의 트리 보유하고 축 조상과 XPath를 사용하기 위해 할 수있는 :
let $seq := $types//type[@type=$typeName]/ancestor::*
$seq := ($seq, $types//type[@type=$typeName])
내가 XQuery에 새로운 해요, 나는이 작동하는지 확인하지 않는,하지만 중요하지 않은 문제는 내 XQuery 프로세서 (!)에서 구현 된 "조상"축이없고이 프로세서에 대한 코드를 명시 적으로 작성해야한다는 것입니다.
그래서 재귀 함수를 작성하는 것이 좋습니다. "tre :"는 TypesTree.xsd의 접두사이고 Ancestors.xsd의 "anc :"입니다. 이 함수는 tre : type 요소를 가져 오며 anc : type 요소의 시퀀스를 반환합니다.
declare function BuildAncestors($type as element(tre:type), $typeName as xs:string) as element(anc:type)*
{
for $currentType in $type/tre:type (: for each nested type node :)
return
if ($currentType[@type=$typeName]) (: if current type node's attribute's values is what we search for :)
then ($currentType) (: return this node :)
else (: if this node's attribute's value isn't what we search, check it's children :)
let $retSeq := BuildAncestors($currentType, $typeName) (: get sequence of all ancestor's of this type till this node or null if $currentType has not searched node in it's descendants :)
if ($retSeq) (: if not null returned :)
then ($currentType, $retSeq) (: return new sequence built from this node and returned sequence :)
else() (: return null :)
}
따라서 작동하지 않는 것이 분명합니다. 심지어 시퀀스를 반환하고, 시퀀스를 추가하고, null을 반환하고, null이 반환되는지 여부를 확인하는 방법을 모르겠다 ...
나는 모든 조상을 얻는 방법을 알고있다.
감사합니다.
죄송합니다. 제 실제 코드에는 ancestor ::와 ancestors ::가 없습니다. 개발 네트워크에 단순히 inetrnet에 대한 액세스 권한이 없으므로 코드를 복사하지 않아도됩니다. 그러나 해결책을 찾았습니다. 나는 그것을 게시 할 것입니다. 어쨌든 고맙습니다. – rodnower