14

To conform with the <boolean> spec of Xml-RPC I need to transform my xs:boolean from true|false to 1|0.

I solved this using xsl:choose

<xsl:template match="Foo">
    <member>
        <name>Baz</name>
        <value>
            <boolean>
                <xsl:choose>
                    <xsl:when test=".='true'">1</xsl:when>
                    <xsl:otherwise>0</xsl:otherwise>
                </xsl:choose>
            </boolean>
        </value>
    </member>
</xsl:template>

but was wondering if there is a less brittle way of controlling how boolean values are rendered when transformed with xslt 1.0.

Filburt
  • 17,626
  • 12
  • 64
  • 115

1 Answers1

17

Use:

number(boolean(.))

By the definition of the standard XPath function number() it produces exactly {0, 1} when applied, respectively, on {true(), false()}

Beware! If you use this construction on strings, the result will be true for both 'false' and 'true', because, for string parameters, boolean() is true if and only if its length is non-zero.

So, if you want to convert strings, not booleans, then use this expression:

number(not(. = 'false'))

Below is an XSLT-based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

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

 <xsl:template match="text()">
  <xsl:value-of select="number(not(. = 'false'))"/>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following XML document:

<t>
 <x>true</x>
 <y>false</y>
</t>

the wanted, correct result is produced:

<t>
   <x>1</x>
   <y>0</y>
</t>
R. Schreurs
  • 8,587
  • 5
  • 43
  • 62
Dimitre Novatchev
  • 240,661
  • 26
  • 293
  • 431
  • Excellent als always - but my prefered version `number(boolean(.))` doesn't seem to work ... it always returns `1`. I'll go for the string comparison which will allow me to use a more generic template in my solution. – Filburt Aug 17 '11 at 14:45
  • It seems `number(boolean('false'))` will return `1` whereas `number(boolean(false))` will return `0` so the remaining question to ultimate happiness is how to retrieve the non-literal value of a xs:boolean element ;-) – Filburt Aug 17 '11 at 14:53
  • @Filburt: In case you have really a boolean then use: `number(.)`. The reason there ares still questions hanging around is that you haven't provided a complete XML document and the wanted complete result. When people are guessing these, even 10% success rate is huge. – Dimitre Novatchev Aug 17 '11 at 15:59
  • I admit that I can have everything if I don't give a fully fleshed out example (including source and desired result) but I did explicitly state my source values type is `xs:boolean` ... and I notice there's a typo in my post: `xsl:boolean` isn't really a type. I did dig around before posting my question and tried `number(.)` which yields `NaN`. Anyway you gave 100% and I don't want to seem ungrateful asking why xslt 1.0 won't allow 150% ;-) – Filburt Aug 17 '11 at 17:15
  • XSLT 1.0/XPath 1.0 is not strongly typed and most values in most cases are of type string (that may be castable to other types). And, unfortunately, `boolean(string(false()))` is `true()` -- not false. Therefore, one will have to use `number(not(. = 'false'))` to convert `{'true', 'false'}` to `{1, 0}` – Dimitre Novatchev Aug 17 '11 at 19:16