2016-11-10 2 views
4

게시 : here 이 질문은 teiid 엔진의 XMLTABLE에서만 발생합니다. 나는 다음과 같은 XML 콘텐츠를 구문 분석하고 있습니다 :xpath를 사용하여 xpath를 사용하여 노드 값과 속성의 여러 연결을 결합하십시오. Teeth에서

<AttributeSets> 
    <ns2:ItemAttributes xml:lang="de-DE"> 
     <ns2:Creator Role="Role1">Creator One</ns2:Creator> 
     <ns2:Creator Role="Role2">Creator Two</ns2:Creator> 
     <ns2:Creator Role="Role3">Creator Three</ns2:Creator> 
    </ns2:ItemAttributes> 
</AttributeSets> 

여기

Select * From 
     XMLTABLE(XMLNAMESPACES(DEFAULT 'http://ns1', 
      'http://ns2/default.xsd' as ns2), 
      'AttributeSets/ns2:ItemAttributes' PASSING x.AttributeSets 
     COLUMNS 
      Creator STRING PATH 'string-join(//ns2:Creator/(@Role, node()) , '', '')' 
    ) p; 

x.AttributeSets를 사용하여 위의 내용에있는 XML 입력 변수입니다.

xpath를 사용하여 한 줄로 포맷하고 결합하려고합니다. 뭔가 같은 :

Role3을 Role1, Role2 : 나에게 역할의 쉼표로 구분 된 목록을 제공

string-join(//ns2:Creator/@Role , ', ') 

작품 :

string-join(//ns2:Creator/concat(./text(), @Role), ', ') 

나는 생각이 있기 때문에, 내가 어딘가 가까운 해요

및이

string-join(//ns2:Creator/node(), ', ') 

은 다음과 같은 값을 결합합니다. "Creator One, Creator T wo, 창조자 3 ".

string-join(//ns2:Creator/(@Role, node()) , ', ') 

이 한 줄에 모든 역할과 제작자를 쉼표 - 구분 :

는 내가 얻을 수있는 가장이었다

Role1: Creator One, Role2: Creator Two, Role3: Creator Three 

의 최종 출력을하고 싶습니다. 어떤 이유인지에 대한 concat 연산자는 작동하지 않는 것 같습니다. 도와주세요.

답변

1

string-join(for $n in /AttributeSets/ItemAttributes/Creator return concat($n/@Role, ':',$n/text()),',') 

는 소스 트리의 상황에 따라의 내에서 선택 XPath를 조정하는 것을 잊지 다음의 XPath를보십시오. 나는에 따라서

/AttributeSets/ItemAttributes/Creator 

문서 문서 루트를 상정 문자열 가입 W3C에서 기능을 꽤 비슷한 예를 가지고있다.

업데이트 :

Role3을 Role1, Role2 ... 작품과 나에게 역할의 쉼표로 구분 된 목록을 제공합니다 내가 생각

이 있기 때문에, 내가 어딘가 가까운 해요

아주 가까이에 있다고 생각하세요. 나는 그에게 작은 비틀기를 시도하고이 일 :

string-join(//ns2:Creator/concat(@Role, ':', ./text()), ', ') 

을 CONCAT 연산자가 더가 작동하는 것 같다없는 몇 가지 이유.

잘 모르겠습니다. 온라인 xpath 테스터 here으로 확인해도 완벽하게 작동합니다. 사실 온라인 도구는 xapth를 다음 출력으로 받아들입니다.

Creator OneRole1, Creator TwoRole2, Creator ThreeRole3 
+0

고맙습니다! 두 버전 모두 원활하고 원하는대로 작동합니다! –