p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   XSLT (http://p2p.wrox.com/forumdisplay.php?f=86)
-   -   Converting Source Xml into Target Xml Using XSL. (http://p2p.wrox.com/showthread.php?t=57833)

alapati.sasi May 14th, 2007 04:30 AM

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

mhkay May 14th, 2007 06:50 AM

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

alapati.sasi May 14th, 2007 09:37 AM

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

mhkay May 14th, 2007 10:54 AM

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


All times are GMT -4. The time now is 11:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.