PDA

View Full Version : <xs:element name="date" type="xs:date"/>


angiras
01-30-2003, 09:01 AM
I need a schema with a type date with this format = day/month/year

mx xsd

<xs:element name="date" type="xs:date"/>


and my xml

<date>29/01/2003</date>


how can I do it ?

thank you

BrainJar
02-01-2003, 05:46 AM
You can't, the date type in XML Schema is fixed. The only format allowed is YYYY-MM-DD (optionally preceeded with a minus sign and followed by a time zone).

The point of doing that is so you don't have to worry about different conventions like dd/mm/yy or mm/dd/yy. If an XML document follows the schema specification, any application program that processes it will know exactly what to expect. The application can then convert it to some other format.

angiras
02-01-2003, 08:26 AM
yes it is practicle to have this universal format , otherwise one get an headhake with countries dates

thank you

angiras
02-01-2003, 08:43 AM
it works , but how can I format it with xslt ?

XML =

<date>2003-02-01</date>

XSLT =

<xsl:if test="date/text()">
<div class="date">
<xsl:value-of select="date"/>
</div>
</xsl:if>

to get HTML =

<div class="date">01/02/2003</div>


thank you

BrainJar
02-01-2003, 07:38 PM
You can use the substring() function:

<xsl:template match="date">
&nbsp;&nbsp;<div>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:attribute name="class">date</xsl:attribute>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:value-of select="substring(text(), 9, 2)"/>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:text>/</xsl:text>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:value-of select="substring(text(), 6, 2)"/>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:text>/</xsl:text>
&nbsp;&nbsp;&nbsp;&nbsp;<xsl:value-of select="substring(text(), 1, 4)"/>
&nbsp;&nbsp;</div>
</xsl:template>

One word of caution, xs:date does allow more than four digits for the year, and an optional minus sign, so -1000000-02-14 (one millions years B.C.) is a valid xs:date and the above would not work. But you can always define your schema to force the date to fall within a specific range. For example:

<xs:element name="date">
&nbsp;&nbsp;<xs:simpleType>
&nbsp;&nbsp;&nbsp;&nbsp;<xs:restriction base="xs:date">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<xs:minInclusive value="0001-01-01"/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<xs:maxInclusive value="9999-12-31"/>
&nbsp;&nbsp;&nbsp;&nbsp;</xs:restriction>
&nbsp;&nbsp;</xs:simpleType>
</xs:element>

angiras
02-01-2003, 10:13 PM
then I have done:


XSD =

<xs:element name="date">
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:minInclusive value="2003-01-01"/>
<xs:maxInclusive value="2010-01-01"/>
</xs:restriction>
</xs:simpleType>
</xs:element>


XSLT =

<xsl:if test="date/text()">
<div class="date">
<xsl:attribute name="class">date</xsl:attribute>
<xsl:value-of select="substring(text(), 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(text(), 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(text(), 1, 4)"/>
</div>
</xsl:if>


and I get HTML =


//


:-(( I think I've done something wrong

angiras
02-01-2003, 10:20 PM
<xsl:if test="date/text()">
<div class="date">
<xsl:value-of select="substring(date, 9, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(date, 6, 2)"/>
<xsl:text>/</xsl:text>
<xsl:value-of select="substring(date, 1, 4)"/>
</div>
</xsl:if>



It works !


thank you very much :-))