user3202862
user3202862

Reputation: 217

xslt next element value based on previous element

I have got the following XMl. i want to read the start and end dates based on the type value. when the type is "O" then read the start and end date for O and same for type "L". Date is optional.

     <Person id="1">
        <Date>
            <Type value="O"/>
            <Start value="2009-04-01"/>
            <End value="2012-03-31"/>
        </Date>
        <Date>
            <Type value="L"/>
            <Start value="2009-01-31"/>
        </Date>
        </Person>
        <Person id="2">
        <Date>
            <Type value="O"/>
            <Start value="2009-01-11"/>
        </Date>
        </Person>

i have got the following XSLT code. But it's reading and combing both the start/end dates that are present in both.

    <xsl:choose>
    <xsl:when test="Date/Type[@value = 'O']">
        <xsl:value-of select="Date/Start/@value"/><xsl:value-of select="$separator" />
        <xsl:value-of select="Date/End/@value"/><xsl:value-of select="$separator" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$separator" /><xsl:value-of select="$separator" />
    </xsl:otherwise>
</xsl:choose>
        <xsl:choose>
    <xsl:when test="Date/Type[@value = 'L']">
        <xsl:value-of select="Date/Start/@value"/><xsl:value-of select="$separator" />
        <xsl:value-of select="Date/End/@value"/><xsl:value-of select="$separator" />
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="$separator" /><xsl:value-of select="$separator" />
    </xsl:otherwise>
</xsl:choose> 

the desire output is csv

    "2009-04-01","2012-03-31","2009-01-31",""
    "2009-01-11","","",""

Upvotes: 0

Views: 1091

Answers (1)

Joel M. Lamsen
Joel M. Lamsen

Reputation: 7173

try this XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="root/Person">
            <xsl:if test="position() &gt; 1">
                <xsl:text>&#xA;</xsl:text>
            </xsl:if>
            <xsl:choose>
                <xsl:when test="Date[Type[@value='O']]">
                    <xsl:apply-templates select="Date[Type[@value='O']]"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>&quot;&quot;,&quot;&quot;</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:choose>
                <xsl:when test="Date[Type[@value='L']]">
                    <xsl:apply-templates select="Date[Type[@value='L']]"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>,&quot;&quot;,&quot;&quot;</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

    <xsl:template match="root/Person/Date[Type[@value='O']]">
        <xsl:value-of select="if (Start) then concat('&quot;', Start/@value, '&quot;') else concat('&quot;', '&quot;')"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="if (End) then concat('&quot;', End/@value, '&quot;') else concat('&quot;', '&quot;')"/>
    </xsl:template>

    <xsl:template match="root/Person/Date[Type[@value='L']]">
        <xsl:text>,</xsl:text>
        <xsl:value-of select="if (Start) then concat('&quot;', Start/@value, '&quot;') else concat('&quot;', '&quot;')"/>
        <xsl:text>,</xsl:text>
        <xsl:value-of select="if (End) then concat('&quot;', End/@value, '&quot;') else concat('&quot;', '&quot;')"/>
    </xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions