2016-08-22 1 views
1

XSLT를 사용하여 조상 노드 이름을 인쇄하려고합니다.조상 이름을 선택하는 데 혼란이 있음

다음은 내 XSLT입니다.

<xsl:apply-templates select="(//title)[1]"/> 

<xsl:template match="title"> 
<xsl:value-of select="name(ancestor::node())"/> 
</xsl:template> 

내 XML은 아래와 같습니다.

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <!--ancestor--> 
    <cd> 
     <!--parent--> 
     <title name="titleName" property="propertyName">Empire Burlesque</title> 
     <!--self--> 
     <artist>Bob Dylan</artist> 
     <!--following-sibling--> 
     <country>USA</country> 
     <!--following-sibling--> 
     <company>Columbia</company> 
     <!--following-sibling--> 
     <price>10.90</price> 
     <!--following-sibling--> 
     <year>1985</year> 
     <!--following-sibling--> 
    </cd> 
</catalog> 

여기 아래 문장을 사용할 때. 나는 그것이 나에게 아래의 예외

XSLT 2.0 Debugging Error: Error: file:///C:/Users/u0138039/Desktop/Training%20Docs/XSLT%20Training/Sample.xsl:209: Wrong occurrence to match required sequence type - Details: -  XPTY0004: The parameter value ('3' item(s)) at position '1' of the 'name' function has the wrong occurrence to match the sequence type node() ('zero or one') 
을주고있다

<xsl:value-of select="name(ancestor::node())"/> 

또는

<xsl:value-of select="name(ancestor::*)"/> 

를 사용하는 다른

<xsl:value-of select="name(../..)"/> 

하지만 사용할 때, 부모의 이름을 얻을 수 있습니다

<xsl:value-of select="name(parent::*)"/> 

이 이유는 parent에서 작동하지만 ancestor에서는 작동하지 않는 이유를 알려주세요.

이것은 매우 혼란 스럽습니다. <xsl:value-of select="name(ancestor::node())" 같은 것을 사용하여 ancestor 이름을 얻으려면 어떻게해야합니까?

감사

+1

조상의 이름은? 'title' 엘리먼트의 문맥에서'ancestor :: node()'를 사용하면,'cd' 엘리먼트와'catalog' 엘리먼트와 문서 노드를 모두 조상 (ancestor) 노드로 선택합니다. ''은 조부모의 이름을 출력해야합니다. –

답변

2

ancestor 축이 컨텍스트 노드의 모든 조상 포함 모든 방법까지 루트 노드 등 부모, 그 조부모, 가장 큰-조부모.

조상의 이름을 묻는 경우 축에 노드 중 중 하나를 선택해야합니다 (). 예를 들어 :

<xsl:value-of select="name(ancestor::*[last()])"/> 

는 (참고 조상 역 축입니다) 귀하의 예제에서 "catalog"를 반환합니다.

OTOH이 :

<xsl:value-of select="ancestor::*/name()"/> 

은 (XSLT 2.0) "catalog cd" 반환 - 노드의 조상의 모든 이름 즉 공간 구분하여.