XSLT:条件属性值处理

OMG*_*ies 2 xslt

以下内容无法按预期工作:

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

  <xsl:if test="$prefix='core'">
    <xsl:variable name="prefix" select=""/>
  </xsl:if>

  <xs:complexType name="{concat($prefix, @name)}">
    <xsl:apply-templates select="node()" />
  </xs:complexType>
  <xsl:apply-templates select=".//xs:element" />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)

这个想法是,如果前缀变量值是"核心",我不希望它被添加到name属性值.任何其他价值,我想加入.IE:

<xs:complexType name="coreBirthType">
Run Code Online (Sandbox Code Playgroud)

......是不是可以接受的,而下面将是:

<xs:complexType name="BirthType">
Run Code Online (Sandbox Code Playgroud)

但我必须允许这种情况发生:

<xs:complexType name="AcRecHighSchoolType">
Run Code Online (Sandbox Code Playgroud)

我在一个区块中尝试了这个,但是撒克逊抱怨没有找到一个结束节点:

<xsl:choose>
  <xsl:when test="starts-with(.,'core')">
    <xs:complexType name="{@name)}">
  </xsl:when>
  <xsl:otherwise>
    <xs:complexType name="{concat($prefix, @name)}">
  </xsl:otherwise>
</xsl:choose>
  <xsl:apply-templates select="node()" />
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)

处理这个问题的最佳方法是什么?

Pav*_*aev 5

在XSLT中,作为一种没有副作用的纯语言,变量是不可变的.您无法更改变量值.如果声明另一个<xsl:variable>具有相同名称的变量,则定义一个隐藏旧变量的新变量.

这是你如何做到这一点:

<xsl:param name="prefix" />

<xsl:variable name="prefix-no-core">
  <xsl:if test="$prefix != 'core'">
    <xsl:value-of select="$prefix" />
  </xsl:if>
</xsl:variable>

<xs:complexType name="{concat($prefix-no-core, @name)}">
...
Run Code Online (Sandbox Code Playgroud)