Basic XSLT question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul  Smith

    #1

    Basic XSLT question

    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="SDSUCCE SS">
    <tablemetadat a>
    <tablemetadatai tem name="TABLE1" description="Ta ble 1"/>
    </tablemetadata>
    <columnmetadata >
    <columnmetadata item name="COL1" description="Co l 1" pos="2"/>
    <columnmetadata item name="COL2" description="Co l 2" pos="1"/>
    </columnmetadata>
    <othertag>
    <othertagitem name="Type" value="Somethin g"/>
    <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="SDSUCCE SS">
    <tablemetadat a>
    <tablemetadatai tem name="TABLE1" description="Ta ble 1"/>
    </tablemetadata>
    <columnmetadata >
    <columnmetadata item name="COL1" description="Co l 1" pos="2"/>
    <columnmetadata item name="COL2" description="Co l 2" pos="1"/>
    </columnmetadata>
    <othertag>
    <othertagitem name="Type" value="Somethin g"/>
    <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:styleshe et xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output 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>
  • Richard Tobin

    #2
    Re: Basic XSLT question

    In article <2b9e76b9.03081 10907.2ce6fc2e@ posting.google. com>,
    Paul Smith <pdg5001@yahoo. co.uk> wrote:
    [color=blue]
    >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.[/color]

    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
    --
    Spam filter: to mail me from a .com/.net site, put my surname in the headers.

    FreeBSD rules!

    Comment

    Working...