RE: [xsl] Probelm with xsl:value-of in CSV to XML transform

Subject: RE: [xsl] Probelm with xsl:value-of in CSV to XML transform
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Fri, 4 Apr 2008 10:10:27 +0800
> I am converting a CSV into XML, and wanted to transform the 
> following two tags:
>                <classdate>9/03/2008</classdate>
>                <sortdate>2008-03-09</sortdate>

I see two elements there with four tags... And I don't see any
comma-separated values!
> 
> Into:
>                <classdate>
>                   <items>
>                      <item>
>                         <label>9/03/2008</label>
>                         <value>2008-03-09</value
>                      </item>
>                   </items>
>                </classdate>
> 
> Where the <sortdate> value becomes the <value> of the <classdate>
> 
> I am almost there, but can't get the value of the <sortdate> 
> to appear in the <value> tag.
> 
> Here is the relevant section of the XSLT:
> <xsl:when test="name()='classdate'">
>                 <xsl:element name="classdate">
>                   <items>
>                     <item>
>                       <label>
>                         <xsl:value-of select="."/>
>                       </label>
>                       <value>
>                           <xsl:value-of select="sortdate"/>
>                       </value>
>                     </item>
>                   </items>
>                 </xsl:element>
>               </xsl:when>
> 

The sortdate isn't a child of the classdate, it is a sibling. So you want

<xsl:value-of select="following-sibling::classdate[1]"/>

(I've added the [1] because your snippet of the input doesn't tell me if
there are any other following siblings)

The test <xsl:when test="name()='classdate'"> suggests that you haven't yet
learnt how to use template rules, which is the core construct for
structuring XSLT code. When you have conditional logic in the stylesheet
that performs different processing for different elements, create a template
rule for each kind of element and invoke it using xsl:apply-templates.

Also, <xsl:element name="classdate"> is best replaced by <classdate>.

Michael Kay
http://www.saxonica.com/

Current Thread