2009-07-13 39 views
7

我有一種情況,param將不會被設置,但我嘗試捕捉&處理該sitation不起作用。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)" /> 

...值打印出來爲真。但在我的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

我需要支持其他前綴的可能性。 – 2009-07-13 17:23:55

+0

試過了--xsl:否則會給我誤報,將前綴應用到它不應該使用的地方。 – 2009-07-13 19:21:26

+0

你什麼時候不想應用前綴?再次,給我們一個完整的用例。如果prefix = core,則不需要前綴。如果未設置前綴,則需要AcDec作爲前綴。另一種情況是什麼?您是否想要使用提供的前綴,或者是否需要爲每個「允許」前綴手動添加一個案例?如果是這樣,那麼調用者會提供一個您不認識的前綴? – 2009-07-13 19:56:01

1

這是一個黑客攻擊,但是在測試一個空參數之前,首先用normalize-space()函數包裝了參數,從而取得了成功。

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

我試過這是一個when子句 - 它處理誤報。 – 2009-07-13 19:12:48

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元素(即硬編碼的文字值),請在這裏使用'文字值' – 2012-09-25 22:28:58

相關問題