집합 연산

XPath(XML Path Language)는 set 작업을 지원합니다. |.

통합(|) 연산자

|통합 연산자는 두 피연산자의 통합을 반환하며 이는 노드 집합이어야 합니다.예를 들어, //author | //publisher는 //author 노드와 //publisher 노드를 모두 결합한 노드 집합을 반환합니다.여러 통합 연산자를 연결하여 여러 노드 집합을 결합할 수 있습니다.예를 들어, //author | //publisher | //editor | //book-seller는 모든 //author, //publisher, //editor 및 //book-seller elements가 포함된 노드 집합을 반환합니다.통합 연산자는 문서 순서를 유지하며 중복된 항목을 반환하지 않습니다.

예제

참조 항목

first-name | last-name

현재 컨텍스트에서 <first-name> 및 <last-name> 요소를 포함하는 노드 집합

(bookstore/book | bookstore/magazine)

<bookstore> 요소 내에 있는 <book> 또는 <magazine> 요소를 포함하는 노드 집합

book | book/author

모든 <book> 요소 및 <book> 요소 내의 모든 <author> 요소를 포함하는 노드 집합

(book | magazine)/price

<book> 또는 <magazine> 요소의 모든 <price> 요소를 포함하는 노드 집합

예제

다음 예제에서는 통합 연산자를 사용한 결과를 보여 줍니다.

XML 파일(test.xml)

<?xml version="1.0"?>
<test>
    <x a="1">
      <x a="2" b="B">
        <x>
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>
</test>

XSLT 파일(test.xsl)

다음 XSLT 스타일시트는 a 특성이 2인 모든 <x> 요소와 특성이 없는 <x> 요소를 선택합니다.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

   <!-- Suppress text nodes not covered in subsequent template rule. -->
   <xsl:template match="text()"/>

  <!-- Handles a generic element node. -->
   <xsl:template match="*">
      <xsl:element name="{name()}">
         <xsl:apply-templates select="*|@*" />
         <xsl:if test="text()">
            <xsl:value-of select="."/>
         </xsl:if>
      </xsl:element>
   </xsl:template>

   <!-- Handles a generic attribute node. -->
   <xsl:template match="@*">
      <xsl:attribute name="{name()}">
         <xsl:value-of select="."/>
      </xsl:attribute>
   </xsl:template>

   <xsl:template match="/test">
      <xsl:apply-templates select="//x[@a=2] | //x[not(@*)]"/>
   </xsl:template>

</xsl:stylesheet>

변환 결과는 다음과 같습니다.

<x a="2" b="B">
   <x>
      <y>31</y>
      <y>y32</y>
   </x>
</x>
<x>
   <y>y31</y>
   <y>y32</y>
</x>

우선 순위

다음 표에서는 부울과 비교 연산자 사이의 우선 순위를 우선 순위가 높은 것부터 순서대로 보여 줍니다.

우선 순위

연산자

설명

1

( )

그룹화

2

[ ]

필터

3

/

//

경로 연산

4

&lt;

&lt;=

&gt;

&gt;=

비교

5

=

!=

비교

6

|

통합

7

not()

부울 not

8

and

부울 and

9

or

부울 or

예제

다음 예제에서는 위에 나열된 연산자 우선 순위를 적용한 결과를 보여 줍니다.

XML 파일(test.xml)

<?xml version="1.0"?>
<test>

    <x a="1">
      <x a="2" b="B">
        <x>
          <y>y31</y>
          <y>y32</y>
        </x>
      </x>
    </x>

    <x a="1">
      <x a="2">
        <y>y21</y>
        <y>y22</y>
      </x>
    </x>

    <x a="1">
      <y>y11</y>
      <y>y12</y>
    </x>

    <x>
      <y>y03</y>
      <y>y04</y>
    </x>

</test>

기본 XSLT 파일(test.xsl)

이 기본 XSLT 파일을 사용하여 다음 설명을 시작합니다.

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>

   <!-- Suppress text nodes not covered in subsequent template rule. -->
   <xsl:template match="text()"/>

  <!-- Handles a generic element node. -->
   <xsl:template match="*">
      <xsl:element name="{name()}">
         <xsl:apply-templates select="*|@*" />
         <xsl:if test="text()">
            <xsl:value-of select="."/>
         </xsl:if>
      </xsl:element>
   </xsl:template>

   <!-- Handles a generic attribute node. -->
   <xsl:template match="@*">
      <xsl:attribute name="{name()}">
         <xsl:value-of select="."/>
      </xsl:attribute>
   </xsl:template>

</xsl:stylesheet>

사례 0:테스트 실행

다음 템플릿 규칙을 XSLT 스타일시트에 추가할 수 있습니다.

<xsl:template match="/test">
      <xsl:apply-templates select="*|@*/>
   </xsl:template>

이렇게 하면 <?xml version="1.0"?> 처리 명령을 제외하고 원본과 동일한 XML 문서가 생성됩니다.

다음 사례에서는 이 템플릿 규칙을 쓰는 다른 방법을 보여 줍니다.여기서 요점은 XPath 연산자가 요소에 바인딩하는 순서를 보여 주는 것입니다.

사례 1: ()가 []보다 더 엄격하게 바인딩

다음 템플릿 규칙은 소스 문서의 모든 <y> 요소 중에서 문서 순서가 첫 번째인 <y> 요소를 선택합니다.

<xsl:template match="/test">
      <xsl:apply-templates select="(//y)[1]"/>
   </xsl:template>

결과는 다음과 같습니다.

<y>y31</y>

사례 2: []가 / 또는 //보다 더 엄격하게 바인딩

다음 템플릿 규칙은 해당 형제 중 첫 번째인 <y> 요소를 모두 선택합니다.

<xsl:template match="/test">
   <xsl:apply-templates select="//y[1]"/>
</xsl:template>

결과는 다음과 같습니다.

<y>y31</y>

<y>y21</y>

<y>y11</y>

<y>y03</y>

사례 3: and, not

다음 템플릿 규칙은 <x> 부모 요소가 있지만 <x> 자식 요소가 없고 특성이 없는 <x> 요소를 모두 선택합니다.

<xsl:template match="/test">
   <xsl:apply-templates select=
    "//x[./ancestor::*[name()='x'] and *[name()!='x'] and not(@*)]"/>
</xsl:template>

결과로 단일 <x> 요소가 아래에 자식과 함께 나열됩니다.

<x>
   <y>y31</y>
   <y>y32</y>
</x>

사례 4: or, and, not

다음 템플릿 규칙은 <x> 요소의 자식이거나 <x> 요소의 부모가 아니고 특성이 없는 각 <x> 요소를 선택합니다.

<xsl:template match="/test">
   <xsl:apply-templates select=
    "//x[./ancestor::*[name()='x'] or *[name()!='x'] and not(@*)]"/>
</xsl:template>

결과는 자식과 함께 아래에 나열된 다음 <x> 요소를 포함하는 노드 집합입니다.

<x a="2" b="B">
  <x>
     <y>y31</y>
     <y>y32</y>
  </x>
</x>
<x>
  <y>y31</y>
  <y>y32</y>
</x>
<x a="2">
  <y>y21</y>
  <y>y22</y>
</x>
<x>
  <y>y03</y>
  <y>y04</y>
</x>

사례 5: and, or, not

다음 템플릿 규칙은 <x> 요소의 자식이지만 <x> 요소의 부모가 아니거나 특성이 없는 각 <x> 요소를 선택합니다.

<xsl:template match="/test">
   <xsl:apply-templates select=
    "//x[./ancestor::*[name()='x'] and *[name()!='x'] or not(@*)]"/>
</xsl:template>

결과는 자식과 함께 아래에 나열된 다음 <x> 요소를 포함하는 노드 집합입니다.

<x>
   <y>y31</y>
   <y>y32</y>
</x>
<x a="2">
  <y>y21</y>
  <y>y22</y>
</x>
<x>
  <y>y03</y>
  <y>y04</y>
</x>