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

OMG*_*ies 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>
Run Code Online (Sandbox Code Playgroud)

我在第一次测试中也尝试过$ prefix ='' - 两种方法都没有效果.但如果我使用:

<xsl:value-of select="not($prefix)" />
Run Code Online (Sandbox Code Playgroud)

...该值打印为true.但在我的xsl:choose中使用它不会产生任何输出.

dav*_*pcj 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>
Run Code Online (Sandbox Code Playgroud)

像这样调用,值为'default-value':

<xsl:apply-templates select="//xs:complexType[@name='AddressType']" />
Run Code Online (Sandbox Code Playgroud)

这样调用,值为'pass-value':

<xsl:apply-templates select="//xs:complexType[@name='AddressType']">
    <xsl:with-param name="prefix" value="'passed-value'"/>
</xsl:apply-templates>
Run Code Online (Sandbox Code Playgroud)

编辑:为了完整性,原始示例的简化形式是:

<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>
Run Code Online (Sandbox Code Playgroud)


OMG*_*ies 0

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

<xsl:apply-templates select="//xs:complexType[@name='AddressType']" />
Run Code Online (Sandbox Code Playgroud)

这必须改为:

<xsl:apply-templates select="//xs:complexType[@name='AddressType']">
  <xsl:with-param name="prefix" select="'AcRec'" />
</xsl:apply-templates>
Run Code Online (Sandbox Code Playgroud)

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