代码之家  ›  专栏  ›  技术社区  ›  OMG Ponies

xslt:test参数以确定是否已设置

  •  7
  • OMG Ponies  · 技术社区  · 15 年前

    我遇到了这样一种情况:参数不会被设置,但我试图捕捉和处理位置的尝试没有起作用。

    我正在使用以下内容:

    <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>
    

    我在第一个测试中也尝试了$prefix=''-两个都不起作用。但是如果我使用:

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

    …值打印为真。但是在xsl:choose中使用它不会产生任何输出。

    4 回复  |  直到 13 年前
        1
  •  8
  •   davenpcj    13 年前

    您可以为参数指定一个默认值,这样当参数未被传递时,您可以检查它。

    尽管在您的示例中,使用默认值会大大简化操作。

    <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>
    
        2
  •  1
  •   Ishmael    15 年前

    这是一次黑客攻击,但是我成功地在测试空参数之前,首先用normalize-space()函数包装参数。

    <xsl:value-of select="not(normalize-space($prefix))" />
    
        3
  •  1
  •   Chris Marasti-Georg Scott Weinstein    15 年前

    注意:已替换旧答案,如果需要,请检查历史记录。

    以下输入:

    <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"/>
    

    我不知道你还要找什么…

        4
  •  0
  •   OMG Ponies    15 年前

    在迄今为止提出的建议中,唯一有效的解决方案是在调用模板时始终设置参数值。例如,以前我有:

    <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)的值。