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

XSLT:测试参数以了解是否已设置

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

    我有一种情况,参数不会被设置,但我尝试捕获和;处理这个问题不起作用。

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

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

    <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    16 年前

    这是一个技巧,但我在测试空参数之前,首先用normalize-space()函数包装参数,从而取得了成功。

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

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

    以下输入:

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

    <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    16 年前

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

    我真的很想知道为什么not($param)和$param=“”在WHEN测试中不起作用,但我可以在xsl:value-of语句中得到not($param)的值。