无法使用 xsl:when 将 true/false 转换为 1/0

Unable to convert true/false to 1/0 using xsl:when

我正在尝试使用 XSL 将 XML 属性从值 "true"/"false" 转换为值“1”/“0”,但我无法进行转换。通过在线阅读,我应该能够使用 <xsl:when><xsl:if> 来完成此操作。有些引用将我的属性包装在 string 中,有些则没有。

举个例子XML:

<root>
  <record name="a" isMutable="true">
  <record name="b" isMutable="false">
</root>

以及示例 XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet>
  <xsl:template match="root">
    <top>
      <xsl:apply-templates select="/root/record"/>
    </top>
  </xsl:template>
  <xsl:template match="/root/record">
    <xsl:element name="element">
      <xsl:choose>
        <xsl:when test="string(@isMutable) = 'true'">
          <xsl:attribute name="when_value">1</xsl:attribute>
        </xsl:when>
        <xsl:when test="string(@isMutable) = 'false'">
          <xsl:attribute name="when_value">0</xsl:attribute>
        </xsl:when>
        <xsl:otherwise>
          <xsl:attribute name="plain_value"><xsl:value-of select="@isMutable"/></xsl:attribute>
          <xsl:attribute name="bool_value"><xsl:value-of select="boolean(@isMutable = 'true')"/></xsl:attribute>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

我希望获得 when_value="0"when_value="1" 的属性,但我得到的是:

<top>
  <element plain_value="true" bool_value=""/>
  <element plain_value="false" bool_value=""/>
</top>

我可以在 plain_value 属性中看到我的属性值正在被提取。我还尝试了尝试使用 boolean 的替代解决方案,但您可以看到这会产生空白结果。谁能指出我的示例 XSL 代码中的语法错误?谢谢。

我无法理解您想要获得的确切输出。查看以下样式表是否有帮助:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="/root">
    <top>
        <xsl:apply-templates select="record"/>
    </top>
</xsl:template>

<xsl:template match="record">
    <element name="{@name}" boolean_value="{@isMutable}" numeric_value="{number(@isMutable='true')}"/>
</xsl:template>

</xsl:stylesheet>

当应用于格式正确的 (!) 测试输入时:

<root>
  <record name="a" isMutable="true"/>
  <record name="b" isMutable="false"/>
</root>

结果将是:

<?xml version="1.0" encoding="UTF-8"?>
<top>
   <element name="a" boolean_value="true" numeric_value="1"/>
   <element name="b" boolean_value="false" numeric_value="0"/>
</top>

如您所见,boolean_value 属性的值是从原始 isMutable 属性复制的,而 numeric_value 属性显示相同的值转换为数字 - 或者1 或 0。


为了完整起见,如果您想使用 xsl:choose:

,您可以按照以下方式做同样的事情
<xsl:template match="record">
    <element name="{@name}" boolean_value="{@isMutable}">
        <xsl:attribute name="numeric_value">
            <xsl:choose>
                <xsl:when test="@isMutable='true'">1</xsl:when>
                <xsl:otherwise>0</xsl:otherwise>
            </xsl:choose>
        </xsl:attribute>
    </element>
</xsl:template>

您的 XSLT 比它需要的要复杂得多(尽管从外观上看,它 应该 可以工作,除了缺少命名空间之外。

这就是您所需要的:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@isMutable">
    <xsl:attribute name="when_value">
      <xsl:value-of select="number(. = 'true')"/>
    </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>

或者如果您想处理 @isMutable 可能不是 truefalse 的情况:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="@isMutable[. = 'true' or . = 'false']">
    <xsl:attribute name="when_value">
      <xsl:value-of select="number(. = 'true')"/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="@isMutable">
     <xsl:attribute name="plain_value">
       <xsl:value-of select="." />
     </xsl:attribute>
     <xsl:attribute name="bool_value">
       <xsl:value-of select=". = 'true'" />
     </xsl:attribute>
  </xsl:template>
</xsl:stylesheet>