경로 식 단계에서 축 지정
경로 식의 축 단계는 다음 구성 요소로 구성됩니다.
자세한 내용은 경로 식(XQuery)를 참조하십시오.
SQL Server 2005 의 XQuery 구현은 다음 축 단계를 지원합니다.
축 | 설명 |
---|---|
child |
컨텍스트 노드의 자식을 반환합니다. |
descendant |
컨텍스트 노드의 모든 하위 노드를 반환합니다. |
parent |
컨텍스트 노드의 부모를 반환합니다. |
attribute |
컨텍스트 노드의 특성을 반환합니다. |
self |
컨텍스트 노드 자신을 반환합니다. |
descendant-or-self |
컨텍스트 노드와 해당 컨텍스트 노드의 모든 하위 노드를 반환합니다. |
앞에서 설명한 축은 모두 parent 축을 제외하고 정방향 축입니다. parent 축은 문서 계층 구조에서 거꾸로 검색하므로 역방향 축입니다. 예를 들어 상대 경로 식 child::ProductDescription/child::Summary
에는 두 단계가 있는데 각 단계는 하나의 child
축을 지정합니다. 첫째 단계는 컨텍스트 노드의 <ProductDescription> 요소 자식을 검색합니다. 둘째 단계는 각 <ProductDescription> 요소 노드에 대해 <Summary> 요소 노드 자식을 검색합니다.
상대 경로 식 child::root/child::Location/attribute::LocationID
에는 세 단계가 있습니다. 처음 두 단계는 각각 child
축을 지정하고 세째 단계는 attribute
축을 지정합니다. Production.ProductModel 테이블의 제조 지침 XML 문서에 대해 실행하면 식은 <root> 요소의 <Location> 요소 노드 자식에 대한 LocationID
특성을 반환합니다.
예
이 항목의 쿼리 예는 AdventureWorks 데이터베이스의 xml 유형 열에 대해 지정된 것입니다. 이러한 열에 대한 자세한 내용은 AdventureWorks 데이터베이스의 xml 데이터 형식 표시를 참조하십시오.
1. child 축 지정
다음 쿼리는 특정 제품 모델에 대해 Production.ProductModel
테이블에 저장되어 있는 제품 카탈로그 설명에서 <ProductDescription> 요소 노드의 <Features> 요소 노드 자식을 검색합니다.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
/child::PD:ProductDescription/child::PD:Features')
FROM Production.ProductModel
WHERE ProductModelID=19
앞의 쿼리에서 다음을 유의하십시오.
- xml 데이터 유형의
query()
메서드는 경로 식을 지정합니다. - 경로 식의 단계는 모두
child
축과 노드 이름,ProductDescription
과Features
를 노드 테스트로서 지정합니다. 노드 테스트에 대한 자세한 내용은 경로 식 단계에서 노드 테스트 지정을 참조하십시오.
2. descendant 및 descendant-or-self 축 지정
다음 예에서는 descendant 및 descendant-or-self 축을 사용합니다. 이 예의 쿼리는 xml 유형 변수에 대해 지정됩니다. 생성되는 결과의 차이점을 쉽게 설명하기 위해 XML 인스턴스를 단순화하였습니다.
declare @x xml
set @x='
<a>
<b>text1
<c>text2
<d>text3</d>
</c>
</b>
</a>'
declare @y xml
set @y = @x.query('
/child::a/child::b
')
select @y
아래 결과에서 식은 <a>
요소 노드의 <b>
요소 노드 자식을 반환합니다.
<b>text1
<c>text2
<d>text3</d>
</c>
</b>
이 식에서 경로 식
/child::a/child::b/descendant::*
에 대해 descendant 축을 지정하면 <b
> 요소 노드의 모든 하위 노드를 검색합니다.
노드 테스트에서 별표(*)는 노드 테스트로서 노드 이름을 나타냅니다. 따라서 descendant 축의 기본 노드 유형인 요소 노드에 따라 반환되는 노드 유형이 결정됩니다. 즉, 식이 모든 요소 노드를 반환합니다. 텍스트 노드는 반환되지 않습니다. 기본 노드 유형과 노드 테스트와의 관계에 대한 자세한 내용은 경로 식 단계에서 노드 테스트 지정 항목을 참조하십시오.
아래 결과에서 볼 수 있듯이 <c
> 및 <d
> 요소 노드가 반환됩니다.
<c>text2
<d>text3</d>
</c>
<d>text3</d>
descendant 축 대신 descendant-or-self 축을 지정하면 /child::a/child::b/descendant-or-self::*
가 컨텍스트 노드, <b
> 요소 및 그 하위 노드를 반환합니다.
다음은 결과입니다.
<b>text1
<c>text2
<d>text3</d>
</c>
</b>
<c>text2
<d>text3</d>
</c>
<d>text3</d>
AdventureWorks 데이터베이스에 대한 다음 예제 쿼리는 <ProductDescription
> 요소의 <Features
> 요소 자식에 대한 모든 하위 항목 요소 노드를 검색합니다.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
/child::PD:ProductDescription/child::PD:Features/descendant::*
')
FROM Production.ProductModel
WHERE ProductModelID=19
3. parent 축 지정
다음 쿼리는 Production.ProductModel
테이블에 저장되어 있는 제품 카탈로그 XML 문서에서 <ProductDescription
> 요소의 <Summary
> 요소 자식을 반환합니다.
이 예에서는 parent 축을 사용하여 <Feature
> 요소의 부모로 돌아가 <ProductDescription
> 요소의 <Summary
> 요소 자식을 검색합니다.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
/child::PD:ProductDescription/child::PD:Features/parent::PD:ProductDescription/child::PD:Summary
')
FROM Production.ProductModel
WHERE ProductModelID=19
이 쿼리 예에서 경로 식은 parent
축을 사용합니다. 아래와 같이 parent 축을 사용하지 않도록 식을 다시 작성할 수 있습니다.
/child::PD:ProductDescription[child::PD:Features]/child::PD:Summary
parent 축에 대한 보다 적절한 예는 아래와 같습니다.
ProductModel 테이블의 CatalogDescription 열에 저장되어 있는 각 제품 모델 카탈로그 설명에는 아래의 그림과 같이 ProductModelID
특성을 가진 <ProductDescription>
요소와 <Features>
자식 요소가 있습니다.
<ProductDescription ProductModelID="..." >
...
<Features>
<Feature1>...</Feature1>
<Feature2>...</Feature2>
...
</ProductDescription>
쿼리는 FLWOR 문에서 반복기 변수 $f
를 설정하여 <Features>
요소의 요소 자식을 반환합니다. 자세한 내용은 FLWOR 문 및 반복(XQuery)을 참조하십시오. return
절은 각 기능에 대해 다음 형식으로 XML을 작성합니다.
<Feature ProductModelID="...">...</Feature>
<Feature ProductModelID="...">...</Feature>
각 <Feature
> 요소에 대해 ProductModelID
를 추가하기 위해 parent
축이 지정됩니다.
SELECT CatalogDescription.query('
declare namespace PD="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelDescription";
declare namespace wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain";
for $f in /child::PD:ProductDescription/child::PD:Features/child::*
return
<Feature
ProductModelID="{ ($f/parent::PD:Features/parent::PD:ProductDescription/attribute::ProductModelID)[1]}" >
{ $f }
</Feature>
')
FROM Production.ProductModel
WHERE ProductModelID=19
다음은 결과의 일부입니다.
<Feature ProductModelID="19">
<wm:Warranty
xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
<wm:WarrantyPeriod>3 years</wm:WarrantyPeriod>
<wm:Description>parts and labor</wm:Description>
</wm:Warranty>
</Feature>
<Feature ProductModelID="19">
<wm:Maintenance
xmlns:wm="https://schemas.microsoft.com/sqlserver/2004/07/adventure-works/ProductModelWarrAndMain">
<wm:NoOfYears>10 years</wm:NoOfYears>
<wm:Description>maintenance contract available through your dealer
or any AdventureWorks retail store.</wm:Description>
</wm:Maintenance>
</Feature>
<Feature ProductModelID="19">
<p1:wheel
xmlns:p1="https://www.adventure-works.com/schemas/OtherFeatures">
High performance wheels.
</p1:wheel>
</Feature>
단일 값이 반환되도록 경로 식에서 [1]
조건자가 추가되었습니다.