2013-01-10 3 views
6

작은 문제가 있습니다.동적 이름을 통해 XSLT 변수 참조

XSL-파일 :

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:fn="http://www.w3.org/2005/xpath-functions"> 
<xsl:template match="/"> 


<xsl:variable name="unumericValue" select="10" /> 
<xsl:variable name="uanotherValue" select="8" /> 



<xsl:for-each select="/root/try"> 
<xsl:value-of select="var" /> 
<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable> 
<xsl:value-of select="@type" /> 
<xsl:variable name="referenceName"><xsl:value-of select='concat("u",var)' /></xsl:variable> 
<xsl:value-of select="$referenceName" /> 
<xsl:if test='$referenceName > $min'> 
<p>Do something.</p> 
</xsl:if> 
</xsl:for-each> 

</xsl:template> 
</xsl:stylesheet> 

XML 파일 :

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="q1.xsl"?> 
<root> 
<try type="compare" minimum="9"> 
<var>numericValue</var> 
<something>...</something> 
</try> 

<try type="compare" minimum="10"> 
<var>anotherValue</var> 
<something>...</something> 
</try> 
</root> 

당신은 XML 파일을 XSLT 파일의 변수에 일치해야 두 개의 VAR-요소를 가지고 볼 수 있듯이 . 그러나 어떤 구문이 올바른지 모르겠습니다. $ referenceName은 내가 사용하고자하는 변수의 이름 일뿐입니다. 그러나 기존 변수의 이름을 참조하는 방법을 모르겠습니다.

답변

11

$referenceName은 "unumericValue"라는 이름을 가진 변수에 대한 참조가 아니거나 그렇지 않습니다. 문자열 값은 "unumericValue"입니다. 따라서 절대 값은 $min보다 클 수 없습니다. 그러나, 약간의 추가 작업으로, 이름으로 변수를 찾을 수있는 트릭이있다 : 여기서주의 할

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:variable name="numericValue" select="10" /> 
    <xsl:variable name="anotherValue" select="8" /> 
    <xsl:variable name="vars" select="document('')/*/xsl:variable" /> 

    <xsl:template match="/"> 
    <xsl:variable name="referenceName" select="'numericValue'" /> 
    <xsl:variable name="referenceValue" select="$vars[@name = $referenceName]/@select" /> 
    Reference value: <xsl:value-of select="$referenceValue" /> 
    </xsl:template> 
</xsl:stylesheet> 

하나의 큰 제한이 단지 일정한 숫자 값입니다 변수 일 것입니다. 마지막으로, 계산 된 값과 변수를 시뮬레이션 할 수있는 방법이

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:v="variables-node" 
> 
    <v:variables> 
    <v:variable n="numericValue" value="10" /> 
    <v:variable n="nonNumericValue" value="Hello World" /> 
    </v:variables> 
    <xsl:variable name="vars" select="document('')//v:variables/v:variable" /> 

    <xsl:template match="/"> 
     <xsl:variable name="referenceName" select="'nonNumericValue'" /> 
     <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/> 
    </xsl:template> 
</xsl:stylesheet> 

그리고 :

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:exslt="http://exslt.org/common" 
> 

    <xsl:variable name="varsRaw"> 
    <var n="computedValue" value="{concat('2 + 4 is ', 2 + 4)}" /> 
    <var n="computedNumber" value="{22 div 7}" /> 
    </xsl:variable> 
    <xsl:variable name="vars" select="exslt:node-set($varsRaw)/var" /> 

    <xsl:template match="/"> 
     <xsl:variable name="referenceName" select="'computedValue'" /> 
     <xsl:variable name="referenceValue" select="$vars[@n = $referenceName]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName, ' has the value ', $referenceValue)"/> 

     <xsl:value-of select="'  '"/> 

     <xsl:variable name="referenceName2" select="'computedNumber'" /> 
     <xsl:variable name="referenceValue2" select="$vars[@n = $referenceName2]/@value" /> 
     <xsl:value-of select="concat('The variable with the name ', $referenceName2, ' has the value ', $referenceValue2)"/> 
    </xsl:template> 
</xsl:stylesheet> 

마지막 방법은 실제로 아마도 가장 정통입니다 여기에

는 상수 문자열 값으로 변수를 시뮬레이션하는 방법 그러나 XSLT 프로세서 종속성 (적어도 XSLT 1.0에서는) node-set() 기능이 필요합니다.

+0

고마워요. 첫 번째 제안은 저에게 큰 도움이되었습니다. – eadrax

1

대부분의 프로그래밍 언어와 마찬가지로 XSLT 변수 이름은 런타임에 액세스 할 수 없습니다. 런타임에 변수가 존재하지 않을 수도 있습니다. 옵티마이 저는 변수가 사용되는 시점에서 변수에 대한 모든 참조를 인라인하는 것과 같은 모든 종류의 트릭을 재생할 수 있습니다.

가장 좋은 방법은 변수에 표준 이름을 지정하고 XML 내용을 지정하는 것입니다. XML의 요소 및 속성 이름은 변수 이름과 달리 런타임에 액세스 할 수 있습니다. 그런데

5

,이 작업을 수행하지 않습니다

<xsl:variable name="min"><xsl:value-of select="@minimum" /></xsl:variable> 

당신은이 작업을 수행 할 수있을 때 :

<xsl:variable name="min" select="@minimum" /> 

그것은 단지 자세한, 또한 비효율적 아니에요 - 데이터를 복사 할 필요가 없습니다 당신이 원하는 것은 기존 노드에 대한 참조 일 때 매우 비싼 작업 인 새로운 트리를 생성 할 수 있습니다.

+0

물론 당신 말이 맞습니다. 또한 쓰기가 훨씬 쉽고 짧습니다. :) – eadrax

관련 문제