Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Extracting the first child node of a parent node

Reply
Thread Tools

Extracting the first child node of a parent node

 
 
ofuuzo1@yahoo.no
Guest
Posts: n/a
 
      03-16-2008
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" xmlnssl="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
 
Reply With Quote
 
 
 
 
Peter Flynn
Guest
Posts: n/a
 
      03-16-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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" xmlnssl="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/
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-17-2008
Peter Flynn wrote:
> (E-Mail Removed) 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
xmlnssl="http://www.w3.org/1999/XSL/Transform"
xmlnsai="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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
If a class Child inherits from Parent, how to implementChild.some_method if Parent.some_method() returns Parent instance ? metal Python 8 10-30-2009 10:31 AM
select nodes with child node A and child node B gplott@yahoo.com XML 1 11-01-2006 09:27 PM
problem with child text node when constraining other child node types Bryan Ax XML 6 05-17-2006 09:08 PM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
Pass from parent to child, then update parent with child value... Noel Dolan Javascript 0 07-18-2004 05:52 PM



Advertisments