Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
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 May 14th, 2007, 04:30 AM
Authorized User
 
Join Date: Apr 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting Source Xml into Target Xml Using XSL.

Hi ,

i have a source xml . i need a xsl for this xml . My target xml should contain only the local-names() of source .It means xsl should remove all the name space binding s.

In my source xml i have elements as well as attributes . My xsl should generate target xml with out any namespace bindings.For attributes also i need to consider only the local-names().

A. Sasi
__________________
A. Sasi
 
Old May 14th, 2007, 06:50 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Write a stylesheet containing two template rules:

 <xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="@*">
  <xsl:attribute name="{local-name()}"
    <xsl:value-of select="."/>
  </xsl:attribute>
 </xsl:template>

Note that this will remove any namespaces on attributes such as xml:space and xsi:type where the namespace may be essential to the meaning.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old May 14th, 2007, 09:37 AM
Authorized User
 
Join Date: Apr 2007
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Michael,
Thanks very much for the help . Its working .
Bottom line is this style sheet will remove all the namespace bindings for elements as well as attributes of elements.

i have a small doubt on how the style sheet works. Can you please tell me .

1)<xsl:template match="*">
  <xsl:element name="{local-name()}">
    <xsl:apply-templates select="@*"/>
    <xsl:apply-templates/>
  </xsl:element>
 </xsl:template>

match="*" means for all the elements.

why there are two xsl:apply-templates?
2)<xsl:template match="@*">
  <xsl:attribute name="{local-name()}"
    <xsl:value-of select="."/>
  </xsl:attribute>
 </xsl:template>

This is take the local-name() for all the attributes of a element .
Where are we specifying for all the elements apply this ?

These are my doubts . i asked you because i want to know how it works internally .

Thanks & Regards,
SAsi.A

A. Sasi
 
Old May 14th, 2007, 10:54 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The line

<xsl:apply-templates select="@*"/>

applies template rules to all the attribute nodes. It's unusual to use apply-templates for attribute nodes, but it's perfectly legal, and it's useful when you want to apply generic processing. This will be matched by the second template rule with match="@*".

The other <xsl:apply-templates/> processes all child nodes (which doesn't include attributes). These will be matched by the first template rule (recursively) for elements, and by the built-in template rules for text, comment, and PI nodes. (Actually this stylesheet will delete comments and PIs, if you don't want that, add template rules to match them and copy them).

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Difficulties converting XML to XML using XSLT Reznik XSLT 7 June 3rd, 2008 05:45 AM
Converting xml to xsl leena XSLT 1 August 3rd, 2007 06:04 AM
How to: Xlate program in XML to target language? Philibuster XSLT 2 August 24th, 2006 10:25 AM
Converting XML to XML (making element mandatory) boondocksaint20 XSLT 8 April 28th, 2006 10:54 AM
xml and xsl templates as input to xslt gives xml rameshnarayan XSLT 5 August 3rd, 2005 01:58 AM





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