Extracting the first child node of a parent node

Discussion in 'XML' started by ofuuzo1@yahoo.no, Mar 16, 2008.

  1. Guest

    Hei,
    I have the following xml file and I have tried to write xslt to
    extract only the values of the first "record" node. It does not
    work. I need some help. I used ---- to represent indent.

    Xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <OAI-PMH xmlns="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd">
    ---<responseDate>2008-02-19T12:54:06Z</responseDate>
    ---<request xmlns="" verb="ListRecords" ......o.no</request>
    ---<ListRecords xmlns="">
    -----<record> <!----the first record node I want to
    extract -->
    -------<header>
    ----------<identifier>oai:frida.uio.no:110517</identifier>
    ----------<datestamp>2004-12-16</datestamp>
    ----------<setSpec>UITT</setSpec>
    -------</header>
    .....
    -----</record> <!-- end of the first record node I want to
    extract --->
    -----<record>
    ..........
    -----</record>
    ...........
    ---</ListRecords>
    </OAI-PMH>

    xslt:

    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    Transform">
    <xsl:template match="/ListRecords" >
    <xsl:apply-templates select="child::node()[1]"/>
    </xsl:template>
    <xsl:template match="record">
    <xsl:value-of select="*" />
    </xsl:template>
    </xsl:stylesheet>


    Thanks in advance.
    Ofuuzo
    , Mar 16, 2008
    #1
    1. Advertising

  2. Peter Flynn Guest

    wrote:
    > Hei,
    > I have the following xml file and I have tried to write xslt to
    > extract only the values of the first "record" node. It does not
    > work. I need some help. I used ---- to represent indent.
    >
    > Xml:
    > <?xml version="1.0" encoding="UTF-8"?>
    > <OAI-PMH xmlns="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd">
    > ---<responseDate>2008-02-19T12:54:06Z</responseDate>
    > ---<request xmlns="" verb="ListRecords" ......o.no</request>
    > ---<ListRecords xmlns="">
    > -----<record> <!----the first record node I want to
    > extract -->
    > -------<header>
    > ----------<identifier>oai:frida.uio.no:110517</identifier>
    > ----------<datestamp>2004-12-16</datestamp>
    > ----------<setSpec>UITT</setSpec>
    > -------</header>
    > .....
    > -----</record> <!-- end of the first record node I want to
    > extract --->
    > -----<record>
    > ..........
    > -----</record>
    > ..........
    > ---</ListRecords>
    > </OAI-PMH>
    >
    > xslt:
    >
    > <?xml version="1.0" encoding="UTF-8" ?>
    > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/
    > Transform">
    > <xsl:template match="/ListRecords" >
    > <xsl:apply-templates select="child::node()[1]"/>
    > </xsl:template>
    > <xsl:template match="record">
    > <xsl:value-of select="*" />
    > </xsl:template>
    > </xsl:stylesheet>


    "/ListRecords" has no meaning because there is no such root element
    type. The root element is OAI-PMH and ListRecords is a child element of
    it, so you can specify the first record of that as a single XPath statement:

    <xsl:template match="/OAI-PMH/ListRecords/record[1]">
    <xsl:apply-templates/>
    </xsl:template>

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
    Peter Flynn, Mar 16, 2008
    #2
    1. Advertising

  3. Peter Flynn wrote:
    > wrote:



    >> <OAI-PMH xmlns="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd">

    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    That root element is in a namespace therefore

    >> ---<responseDate>2008-02-19T12:54:06Z</responseDate>
    >> ---<request xmlns="" verb="ListRecords" ......o.no</request>
    >> ---<ListRecords xmlns="">
    >> -----<record> <!----the first record node I want to
    >> extract -->



    > <xsl:template match="/OAI-PMH/ListRecords/record[1]">

    ^^^^^^^^
    this match pattern will not work.
    You need to bind a prefix to the namespace URI e.g.
    <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:eek:ai="http://www.openarchives.....OAI/2.0/OAI-PMH.xsd"
    version="1.0">

    <xsl:template match="/oai:OAI-PMH/ListRecords/record[1]">

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 17, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bryan Ax
    Replies:
    6
    Views:
    535
    Peter Flynn
    May 17, 2006
  2. Replies:
    1
    Views:
    1,192
    Joe Kesselman
    Nov 1, 2006
  3. Tony Young
    Replies:
    3
    Views:
    309
    Daniel T.
    Mar 26, 2006
  4. Noel Dolan
    Replies:
    0
    Views:
    216
    Noel Dolan
    Jul 18, 2004
  5. Bitswapper
    Replies:
    5
    Views:
    119
    Prasad, Ramit
    Aug 27, 2013
Loading...

Share This Page