[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Processing schema attributes with leading zeros?


Subject: Re: [xsl] Processing schema attributes with leading zeros?
From: Michael Kay <mike@xxxxxxxxxxxx>
Date: Fri, 19 Oct 2012 17:36:46 +0100

Presumably it's a schema-aware transformation, in which case the typed value of the month attribute is the integer 6. When you do select="." to construct the new attribute, the effect is to select the old attribute and atomize it, which gives the integer 6, and then to convert the result to a string, which gives you the string "6".

You might do better with select="string(.)", which should give you the string value of the old attribute node; but processors are allowed to reconstruct the string value from the typed value if they choose.

Basically, by typing this attribute as an integer, you are saying that leading zeroes aren't significant, which contradicts what you are implying in your question, which is that they are significant.

The use of an XSD pattern to constrain a value to something other than its canonical lexical form is always problematic with schema-aware XSLT and XQuery. There's no way the processor can try to construct a representation of an integer that matches a supplied pattern; regular expressions just don't work that way.

Michael Kay
Saxonica

On 19/10/2012 16:49, dvint@xxxxxxxxx wrote:
I have the following elemnt content:
       <issdate year="2012" month="06" day="15"/>

When I process it with the following templates I get this result:
       <issueDate year="2012" month="6" day="15"/>

I'm trying to understand is why the leading 0 on the month was stripped.

The schema has this definition:
     <xs:element name="issdate" type="issdateType"/>
     <xs:complexType name="issdateType">
         <xs:attributeGroup ref="DATE"/>
     </xs:complexType>
     <xs:attributeGroup name="DATE">
         <xs:attribute ref="year" use="required"/>
         <xs:attribute ref="month" use="required"/>
         <xs:attribute ref="day" use="required"/>
     </xs:attributeGroup>
     <xs:attribute name="year" type="YEAR"/>
     <xs:simpleType name="YEAR">
         <xs:restriction base="xs:gYear">
             <xs:pattern value="\d{4}"/>
         </xs:restriction>
     </xs:simpleType>
     <xs:attribute name="month" type="MONTH"/>
     <xs:simpleType name="MONTH">
         <xs:restriction base="xs:positiveInteger">
             <xs:maxInclusive value="12"/>
             <xs:pattern value="\d{2}"/>
         </xs:restriction>
     </xs:simpleType>
     <xs:attribute name="day" type="DAY"/>
     <xs:simpleType name="DAY">
         <xs:restriction base="xs:positiveInteger">
             <xs:maxInclusive value="31"/>
             <xs:pattern value="\d{2}"/>
         </xs:restriction>
     </xs:simpleType>

This is processed with a template that has this:

<xsl:template mathc="issdate">

<!-- code to map old names to new names and set $newName removed, in this
case
     issdate is mapped to issueDate
-->

<xsl:element name="{$newName}">
<xsl:call-template name="processAttributes"/>
	<xsl:apply-templates/>

</xsl:element>
</xsl:template>


<xsl:template name="prosessAttributes"> <xsl:for-each select="@*"> <!-- code to rename attributes removed --> <xsl:attribute name="{name()}" select="."/> </xsl:for-each> </xsl:template>

So basically my code is renaming the element and then just copying the
attribute values through for this particular element. Why am I loosing the
leading 0? I'd really like to avoid having to know what the content might
be in all casses for attributes.

..dan


Current Thread
Keywords