Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
| Search | Today's Posts | Mark Forums Read
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2008, 12:21 PM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extracting subset of XML using XSLT

Hi all,

Am trying to do a simple xml extraction using xslt.

The goal is to extract a simplified format of xml from a complex xml structure.

for e.g.

the sample XML is given below

<?xml version="1.0" encoding="UTF-8"?>



<eu>
 <member name='arbitrary'>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
  <state>United Kingdom</state>
 </member>
 </eu>

the output required is

<?xml version="1.0" encoding="UTF-8"?>
    <state>Austria</state>
    <state founding="yes">Belgium</state>
    <state>United Kingdom
</state>

The xslt used is

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>

 <xsl:template match="eu">
  <xsl:apply-templates select="member"/>
 </xsl:template>

 <xsl:template match="member">
   <xsl:apply-templates select="state"/>
 </xsl:template>

 <xsl:template match="state">
  <xsl:copy-of select="."/>
 </xsl:template>

</xsl:stylesheet>

When i use this particular xslt i get the correct value,

however the input xml has an attribute for the root node.

so the actual xml is

<eu version="2001" xmlns="http://europa.eu/ebiz" xmlns:ebiz="http://europa.eu/ebiz" xsi:schemaLocation="http://europa.eu/ebiz" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <member name='arbitrary'>
  <state>Austria</state>
  <state founding="yes">Belgium</state>
  <state>United Kingdom</state>
 </member>
 </eu>

the xslt when applied provides the following output

<?xml version="1.0" encoding="UTF-8"?>

  Austria
  Belgium
  United Kingdom


This is not what I wanted, so am i missing something basic here ?
Can someone please let me know where iam getting this wrong ?

Thanks in advance

 
Old September 3rd, 2008, 12:32 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You need to bind a prefix to the default namespace URI and use that prefix in your XPath expressions and match patterns:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:eb="http://europa.eu/ebiz">
<xsl:output method="xml"/>

 <xsl:template match="eb:eu">
  <xsl:apply-templates select="eb:member"/>
 </xsl:template>

 <xsl:template match="eb:member">
   <xsl:apply-templates select="eb:state"/>
 </xsl:template>

 <xsl:template match="eb:state">
  <xsl:copy-of select="."/>
 </xsl:template>

</xsl:stylesheet>
--
  Martin Honnen
  Microsoft MVP - XML
 
Old September 3rd, 2008, 12:54 PM
Registered User
 
Join Date: Sep 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Martin,

Thanks for your quick response. It worked :-)

 
Old September 3rd, 2008, 02:44 PM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

There is no need for the first two templates, as these are handled automatically by the XSLT processor.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:eb="http://europa.eu/ebiz">
<xsl:output method="xml"/>

<xsl:template match="eb:state">
 <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>


/- Sam Judson : Wrox Technical Editor -/




Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting portion of XML ** Need help asap** aintvoguish XSLT 3 December 20th, 2007 09:12 AM
creating a subset of xml schema using XSLT kapar_p XSLT 8 November 21st, 2006 09:46 AM
Extracting Subset and Modifying Attribute fauster XSLT 1 September 6th, 2006 08:41 AM
Extracting XML from XML ashcarrot XSLT 0 March 15th, 2005 10:52 AM
How do I get a subset of a recordset?? tlyczko Access 2 January 10th, 2005 09:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.