2010-02-18 2 views
2

이미 존재하지 않을 때 XSLT를 사용하여 순차적으로 고유 ID를 생성하는 방법이 있습니까? 나는 다음과 같은 한 :XSLT에서 생성 된 카운터 값을 갖는 방법은 무엇입니까?

item1 = 100 
item2 = 200 
unnamed1 = 300 
: 내가 끝낼 수 있도록 고유 한 값을 지정할 수 있도록하고 싶습니다

<Foo name="item1" value="100"/> 
<Foo name="item2" value="200"/> 
<Foo value="300"/> 

: 같은의 입력으로

<xsl:template match="Foo"> 
    <xsl:variable name="varName"> 
     <xsl:call-template name="getVarName"> 
     <xsl:with-param name="name" select="@name"/> 
     </xsl:call-template> 
    </xsl:variable> 
    <xsl:value-of select="$varName"/> = <xsl:value-of select="@value"/> 
</xsl:template> 

<xsl:template name="getVarName"> 
    <xsl:param name="name" select="''"/> 
    <xsl:choose> 
     <xsl:when test="string-length($name) > 0"> 
     <xsl:value-of select="$name"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:text>someUniqueID</xsl:text> <!-- Stuck here --> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

답변

2

이 먼저 떨어져, 문맥 노드가 변경되지 않습니다, 당신은에 매개 변수를 전달 할 필요가 없습니다 당신의 상태.

<xsl:template match="Foo"> 
    <xsl:variable name="varName"> 
     <xsl:call-template name="getVarName" /> 
    </xsl:variable> 
    <xsl:value-of select="$varName"/> = <xsl:value-of select="@value"/> 
</xsl:template> 

<xsl:template name="getVarName"> 
    <xsl:choose> 
     <xsl:when test="@name != ''"> 
     <xsl:value-of select="@name"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <!-- position() is sequential and unique to the batch --> 
     <xsl:value-of select="concat('unnamed', position())" /> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

어쩌면 지금 모두 필요합니다. 이름이없는 노드의 출력에는 엄격하게 순차적으로 번호가 매겨지지 않습니다 (unnamed1, unnamed2 등). 당신은 이것을 얻을 것입니다 :

 
item1 = 100 
item2 = 200 
unnamed3 = 300 
+0

이것은 아름답게, 고마워! – Jon

1

generate-id의 결과에 자신의 상수를 추가하면 트릭을 수행 할 수 있습니까? 대신 템플릿의이 같은

0

시도 뭔가 : 템플릿을 호출 할 때

<xsl:template match="/DocumentRootElement"> 
<xsl:for-each select="Foo"> 
    <xsl:variable name="varName"> 
    <xsl:choose> 
     <xsl:when test="string-length(@name) > 0"> 
     <xsl:value-of select="@name"/> 
     </xsl:when> 
     <xsl:otherwise>unnamed<xsl:value-of select="position()"/></xsl:otherwise> 
    </xsl:choose> 
    </xsl:variable> 
    <xsl:value-of select="$varName"/> = <xsl:value-of select="@value"/>\r\n 
</xsl:for-each> 

관련 문제