Extracting the first child node of a parent node

O

ofuuzo1

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
 
P

Peter Flynn

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
 
M

Martin Honnen

Peter said:
(e-mail address removed) wrote:
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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]">
 

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,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top