Basic XSLT question

P

Paul Smith

Apologies, I am new to XSLT. I am having trouble with something that I
would expect to be quite straightforward.

How do I leave XML tags as they are in a transformation? From the XML
below, I want to apply a stylesheet such that the XML is exactly the
same apart from the only <row> should be the first one from the XML
below. I can output some text for position()=1 but I can't output the
original XML too. So the only thing I get in my whole output document
is "Found it"!

Any help would be greatly appreciated.


Original XML:
<?xml version="1.0" encoding="UTF-8"?>
<xmlresults status="SDSUCCESS">
<tablemetadata>
<tablemetadataitem name="TABLE1" description="Table 1"/>
</tablemetadata>
<columnmetadata>
<columnmetadataitem name="COL1" description="Col 1" pos="2"/>
<columnmetadataitem name="COL2" description="Col 2" pos="1"/>
</columnmetadata>
<othertag>
<othertagitem name="Type" value="Something"/>
<othertagitem name="Direction" value="ASC"/>
</parameterdata>
<data>
<row>
<dataitem name="COL1" value="ABC"/>
<dataitem name="COL2" value="123"/>
</row>
<row>
<dataitem name="COL1" value="DEF"/>
<dataitem name="COL2" value="456"/>
</row>
</data>
</xmlresults>

Required Output from XSL transformation (note the second <row> is
missing):
<?xml version="1.0" encoding="UTF-8"?>
<xmlresults status="SDSUCCESS">
<tablemetadata>
<tablemetadataitem name="TABLE1" description="Table 1"/>
</tablemetadata>
<columnmetadata>
<columnmetadataitem name="COL1" description="Col 1" pos="2"/>
<columnmetadataitem name="COL2" description="Col 2" pos="1"/>
</columnmetadata>
<othertag>
<othertagitem name="Type" value="Something"/>
<othertagitem name="Direction" value="ASC"/>
</parameterdata>
<data>
<row>
<dataitem name="COL1" value="ABC"/>
<dataitem name="COL2" value="123"/>
</row>
</data>
</xmlresults>

My XSL (for what it's worth):
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="html" indent="yes"/>

<xsl:template match="data">
<xsl:for-each select="row">
<xsl:choose>
<xsl:when test="position() = 1">
Found It
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
 
R

Richard Tobin

Paul Smith said:
How do I leave XML tags as they are in a transformation? From the XML
below, I want to apply a stylesheet such that the XML is exactly the
same apart from the only <row> should be the first one from the XML
below.

You need a template that copies everything else. This will copy everything
that doesn't have a more specific template:

<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>

Your template for data will work, but it would be simpler to just have a
template for the non-first rows that does nothing:

<xsl:template match="row[position() > 1]">
</xsl:template>

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top