tags:

views:

40

answers:

1

This is my xslt code

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="xslTutorial">
    <xsl:apply-templates select="xslTutorial"/>
</xsl:template>

<xsl:template match="xslTutorial">
    <p>
        SUMMERY:<xsl:value-of select="SECTION/SUMMARY"/>
        <br>
        <xsl:choose>
            <xsl:when test="position()=2">
            DATA:<xsl:value-of select="SECTION/DATA"/>
            </xsl:when>
        </xsl:choose>
        </br>           
        </p>    

</xsl:template>
</xsl:stylesheet>

Here is the xml code:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="C:\Users\Semsema\Desktop\Day 3-XML\ex2.xslt"?>
<xslTutorial>
<SECTION>
    <SUMMARY>I need a pen and some paper</SUMMARY>
    <DATA>I need a pen.</DATA>
    <DATA>I need some paper.</DATA>
</SECTION>
<SECTION>
    <DATA>I need bread.</DATA>
    <DATA>I need butter.</DATA>
</SECTION>
</xslTutorial>

and i want the output to be:

SUMMARY: I need a pen and some paper

DATA: I need bread.

DATA: I need butter.

How can i do this? (what the wrong in xslt code?)

+1  A: 

you are not doing an apply-templates on the SECTION, thus any attempt to test "position()=2" will not give you what you want.

Then again, I'm not really sure if this is the output you are after.

<xsl:template match="xslTutorial">
    <xsl:apply-templates select="xslTutorial/SECTION"/>
</xsl:template>

<xsl:template match="xslTutorial/SECTION">
  <p>
    <xsl:if test="SUMMARY">
    SUMMERY:<xsl:value-of select="SUMMARY"/>
    </xsl:if>
    <br/>
    <xsl:choose>
        <xsl:when test="position()=2">
          <xsl:foreach select="DATA">
            DATA:<xsl:value-of select="."/>
          </xsl:foreach>
        </xsl:when>
    </xsl:choose>
    <br/>           
    </p>    

</xsl:template>
scunliffe
Well, there's a lot more wrong than just that, but it's a start. @sama, you really need to pay more attention to this XSLT tutorial you're following because you're missing some pretty fundamental stuff here.
Welbog