Re: [xsl] Use of data() function (was: Re: [xsl] [OT])

Subject: Re: [xsl] Use of data() function (was: Re: [xsl] [OT])
From: "Andrew Welch" <andrew.j.welch@xxxxxxxxx>
Date: Fri, 29 Feb 2008 16:15:45 +0000
On 29/02/2008, Mukul Gandhi <gandhi.mukul@xxxxxxxxx> wrote:
>  What exactly is meant by "effective boolean value" ?

Hi Mukul,

It's described here:

http://www.w3.org/TR/xpath20/#dt-ebv

>  How should this be exactly interpreted:
>  <xsl:if test="data(@married)">

I think test="@married" is the same as test="exists(@married)" whereas
data(@married) returns the typed value of @married and converts that
to true/false.

Here's a transform to demonstrate:

<xsl:stylesheet
    xmlns:xs="http://www.w3.org/2001/XMLSchema";
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"; version="2.0">

    <!-- The schema is written directly within xsl:import-schema -->
    <xsl:import-schema>
        <xs:schema>
            <xs:element name="person" type="person"/>

            <xs:complexType name="person">
                <xs:simpleContent>
                    <xs:extension base="xs:string">
                        <xs:attribute name="married" type="xs:boolean"/>
                    </xs:extension>
                </xs:simpleContent>
            </xs:complexType>
        </xs:schema>
    </xsl:import-schema>

    <xsl:variable name="input">
        <person xsl:type="person" married="false">typed singleton</person>
        <person married="false">singleton</person>
    </xsl:variable>

    <xsl:template match="/" name="main">
        <xsl:for-each select="$input/person">
            <xsl:value-of select="concat('&#xa;',.)"/>
            <xsl:if test="data(@married)"> - married</xsl:if>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

the output is:

typed singleton
singleton - married

As you can see "married" is output even though it's value if false.

I'm not sure this is a great example - if it can be made more real
world let me know and I'll add it to http://schema-aware.com

-- 
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/

Current Thread