2012-02-22 6 views
2

XSLT 2.0은 노드 집합 매개 변수를 position() 함수의 일부로 전달하는 이점을 제공합니다. 불행히도 XSLT 1.0에서는 사용할 수 없습니다. 이 동작을 모방하는 방법이 있습니까?XSLT 1.0에서 위치 (노드 집합)를 모방합니까?

<wishlists> 
    <wishlist name="Games"> 
    <product name="Crash Bandicoot"/> 
    <product name="Super Mario Brothers"/> 
    <product name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
    <product name="Back to the Future"/> 
    </wishlist> 
</wishlists> 

이 XSLT 2.0 :

는 예를 들어, XML을 부여

<xsl:value-of select="position(/wishlists/wishlist/product)"/> 

마지막 "미래 등을 맞댄"노드를 처리 할 때 "4"반환되는 값. 가장 가까운 내가 XSLT 1.0 얻을 수있을 것 불행하게도

은 다음과 같다 : 나는 "미래 위로"같은에서 "1"의 값을 얻을 것이다, 그러나

<xsl:template match="product"> 
    <xsl:value-of select="position()"/> 
</xsl:template> 

노드 , 내가 정말로 원했던 "4"값과는 대조적이다.

+1

'position()'에서 인수를 허용하는 XSLT 2.0 프로세서는 무엇을 사용하고 있습니까? –

+0

XSLT 2.0에서이 기능을 읽을 수 있다고 생각했습니다. 어쩌면 내가 읽은 것을 오해했을 수도 있고, 아마도 position() 또는 something으로 잘못 읽었을 수도 있습니다.내가 XSLT 2.0에 대해 오해하는 경우, 내 질문은 여전히 ​​내가하려는 일에 어떻게 가야하는지에 관한 것이다. :) –

+0

위치 (node-set()) 함수에 대해 상상하는 의미를 알지 못하기 때문에이를 구현하는 방법을 설명하기가 어렵습니다. –

답변

3

preceding axis을 사용할 수 있습니다.

이 XSLT 1.0 스타일 시트 : 당신의 XML 입력에 적용

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <xsl:template match="node()|@*"> 
    <xsl:copy> 
     <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="product"> 
    <product position="{count(preceding::product) + 1}"> 
     <xsl:apply-templates select="@*"/> 
    </product>  
    </xsl:template> 

</xsl:stylesheet> 

생산 :

<wishlists> 
    <wishlist name="Games"> 
     <product position="1" name="Crash Bandicoot"/> 
     <product position="2" name="Super Mario Brothers"/> 
     <product position="3" name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
     <product position="4" name="Back to the Future"/> 
    </wishlist> 
</wishlists> 
+0

엄청난, 선행 축과 같은 소리는 내가 찾고 있던 것이 분명합니다. 지금 시도해 볼 것이고 그것이 작동하는 것을 보자 마자 응답으로 표시 할 것입니다. 감사! –

+0

대단히 환영합니다! –

2

XSLT 2.0은 위치의 일환으로 노드 집합의 PARAM을 전달하는 혜택을 제공합니다() 함수를 호출합니다.

이 설명이 잘못되었습니다. position() 함수는 XSLT 2.0에서 사용하는 XPath 1.0 또는 XPath 2.0에서 인수가 없습니다.

은 당신이 원하는 것은입니다 :

count(preceding::product) +1 

하거나, 또는, xsl:number 명령을 사용할 수 있습니다. 여기

모두 이러한 방법 시연이다 원하는 정확한 결과가 얻어

<wishlists> 
    <wishlist name="Games"> 
     <product name="Crash Bandicoot"/> 
     <product name="Super Mario Brothers"/> 
     <product name="Sonic the Hedgehog"/> 
    </wishlist> 
    <wishlist name="Movies"> 
     <product name="Back to the Future"/> 
    </wishlist> 
</wishlists> 

이 변환이 제공된 XML 문서에인가

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vLastProd" select= 
    "//product[@name='Back to the Future']"/> 

<xsl:template match="/"> 
    <xsl:value-of select="count($vLastProd/preceding::product) +1"/> 
========= 
<xsl:text/> 
    <xsl:apply-templates select="$vLastProd"/> 
</xsl:template> 

<xsl:template match="product"> 
    <xsl:number level="any" count="product"/>  
</xsl:template> 
</xsl:stylesheet> 

두 가지 방법 모두 사용 - 출력 :

4 
========= 
4 

참고 : 결과가 직접 출력되지 않으면 xsl:number의 결과는 변수 본문에 캡처해야합니다.

+0

네, DevNull이 질문에 언급 한 내용을 언급했고, 분명히 내가 어딘가에서 읽은 것을 잘못 해석했습니다! –

+0

@MattHuggins : 예, "위치"라고하는 것을 얻기 위해 두 가지 방법을 제공했습니다. 둘 다 XSLT 1.0 또는 XSLT 2.0과 함께 사용할 수 있습니다. –

+0

쿨, 고마워! 처음 의견을 쓸 때 편집 내용이 없습니다. ;) –