2009-07-13 3 views
7

매개 변수가 설정되지 않지만, & 캡쳐 시도가 작동하지 않는 상황이 있습니다. - 어느 작업XSLT : 설정되어 있는지를 알기위한 테스트 매개 변수

<xsl:template match="xs:complexType"> 
    <xsl:param name="prefix" /> 

    <xsl:variable name="prefix-no-core"> 
    <xsl:choose> 
     <!-- if no value, default to 'AcRec' --> 
     <xsl:when test="not($prefix)"> 
     <xsl:value-of select="'AcRec'" /> 
     </xsl:when> 
     <!-- if 'core', leave as empty string --> 
     <xsl:when test="$prefix = 'core'"> 
     </xsl:when> 
     <!-- if 'AcRec', set the value --> 
     <xsl:when test="$prefix = 'AcRec'"> 
     <xsl:value-of select="$prefix" /> 
     </xsl:when>    
    </xsl:choose> 
    </xsl:variable> 

    <xs:complexType name="{concat($prefix-no-core, @name)}"> 
    ... 
</xsl:template> 

나는 또한 첫 번째 테스트에서 ''= $ 접두사를 시도했다 :

나는 다음과 같은 사용하고 있습니다. 하지만 내가 사용하는 경우 :

<xsl:value-of select="not($prefix)" /> 

... 값이 true로 출력됩니다. 하지만 내 xsl : choose를 사용하면 출력이 생성되지 않습니다.

답변

0

는 일을하는 유일한 솔루션은 템플릿에 대한 호출이 이루어질 때 항상 매개 변수 값을 설정하는 것이 었습니다. 예를 들어, 이전에 내가 있었다 :

<xsl:apply-templates select="//xs:complexType[@name='AddressType']" /> 

이로 변경했다 : 알고

<xsl:apply-templates select="//xs:complexType[@name='AddressType']"> 
    <xsl:with-param name="prefix" select="'AcRec'" /> 
</xsl:apply-templates> 

내가 정말 좋아하는 것 왜 안 ($의 PARAM)와 $ PARAM = '하지' WHEN 테스트에서 작동하지만 xsl : value-of 문에서 not ($ param) 값을 얻을 수 있습니다.

1

참고 : 원하는 경우 이전 답변을 교체하고 기록을 확인하십시오.

다음 입력 :

<test xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:complexType name="something"/> 
    <xs:complexType name="somethingElse"/> 
</test> 

연준 다음 XSLT에 :

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> 
    <xsl:template match="/"> 
     <xsl:for-each select="node()"> 
      <xsl:apply-templates/> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="xs:complexType"> 
     <xsl:param name="prefix" /> 
     <xsl:variable name="prefix-no-core"> 
      <xsl:choose> 
       <xsl:when test="not($prefix)">AcRec</xsl:when> 
       <xsl:when test="$prefix = 'core'"/> 
       <xsl:when test="$prefix = 'AcRec'">AcRec</xsl:when>      
      </xsl:choose> 
     </xsl:variable> 

     <xs:complexType name="{concat($prefix-no-core, @name)}"/> 
    </xsl:template> 
</xsl:transform> 

는 다음과 같은 결과를 제공합니다 :

<xs:complexType name="AcRecsomething" xmlns:xs="http://www.w3.org/2001/XMLSchema"/> 
<xs:complexType name="AcRecsomethingElse" xmlns:xs="http://www.w3.org/2001/XMLSchema"/> 

을 좀 더 당신이있어 모르겠어요 찾고 있습니다 ...

+0

다른 접두사의 가능성을 지원해야합니다. –

+0

시도해 보았습니다. xsl : 그렇지 않으면 거짓 긍정을 제공하여 접두사를 적용해서는 안되는 접두어를 적용합니다. –

+0

언제 접두사를 적용하고 싶지 않으십니까? 다시 말하지만 완전한 유스 케이스를 제공하십시오. prefix = core 인 경우, 접두사는 필요 없습니다. 접두어가 설정되어 있지 않으면 AcDec를 접두어로 사용합니다. 다른 경우는 무엇입니까? 제공된 접두어를 사용 하시겠습니까? 아니면 각 "허용 된"접두어에 대/소문자를 수동으로 추가해야합니까? 그렇다면 발신자가 인식하지 못하는 접두어를 어떻게 제공합니까? –

1

그것은 해킹이지만, 먼저 빈 매개 변수를 테스트하기 전에 normalize-space() 함수로 매개 변수를 래핑하여 성공했습니다. 지금까지 만든 제안의

<xsl:value-of select="not(normalize-space($prefix))" /> 
+0

나는 이것을 when 절로 사용해 보았습니다. 그것은 거짓 긍정을 다루고있었습니다. –

8

매개 변수에 기본값을 지정할 수 있습니다. 매개 변수가 전달되지 않으면 매개 변수의 기본값을 확인할 수 있습니다.

예를 들어 기본값을 사용하는 것만으로 간단하게 처리 할 수 ​​있습니다.

<xsl:template match="xs:complexType"> 
    <xsl:param name="prefix" select="'default-value'" /> <!-- SET default --> 

    <xsl:variable name="prefix-no-core"> 
    <xsl:choose> 
     <!-- if no value, default to 'AcRec' --> 
     <xsl:when test="$prefix = 'default-value'"> 
     <xsl:value-of select="'AcRec'" /> 
     </xsl:when> 
     <!-- if 'core', leave as empty string --> 
     <xsl:when test="$prefix = 'core'"> 
     </xsl:when> 
     <!-- if 'AcRec', set the value --> 
     <xsl:when test="$prefix = 'AcRec'"> 
     <xsl:value-of select="$prefix" /> 
     </xsl:when>      
    </xsl:choose> 
    </xsl:variable> 

    <xs:complexType name="{concat($prefix-no-core, @name)}"> 
    ... 
</xsl:template> 

이 같은 전화, 값은 '기본 값'이 될 것 같은

<xsl:apply-templates select="//xs:complexType[@name='AddressType']" /> 

전화를하고, 값이 될 '전달 된 값'됩니다

<xsl:apply-templates select="//xs:complexType[@name='AddressType']"> 
    <xsl:with-param name="prefix" value="'passed-value'"/> 
</xsl:apply-templates> 

편집 : 완성을 위해 원본 예제의 단순화 된 형식은 다음과 같습니다.

<xsl:template match="xs:complexType"> 
    <xsl:param name="prefix" select="'AcRec'"/> 

    <xsl:variable name="prefix-no-core"> 
    <xsl:choose> 
     <!-- if 'core', leave as empty string --> 
     <xsl:when test="$prefix = 'core'"> 
     </xsl:when> 
     <!-- defaults to 'AcRec' --> 
     <xsl:otherwise> 
     <xsl:value-of select="$prefix" /> 
     </xsl:otherwise>      
    </xsl:choose> 
    </xsl:variable> 

    <xs:complexType name="{concat($prefix-no-core, @name)}"> 
    ... 
</xsl:template> 
+0

XML 요소 (예 : 하드 코딩 된 리터럴 값)에서 가져 오지 않은 매개 변수의 기본값이 필요한 경우 여기에서 리터럴 값인 ' –