OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: XML/XML Transformation - Please Help !!!.....

XSL, XSLT and XPath questions would be better posted to the following list:


There are a number of subscribers who would enthusiastically respond to 
such questions.

There is also an *excellent* FAQ at:


At 01/04/30 13:58 +0600, Thushara Perera wrote:
>Could someone PLEASE tell me how to do the XML to XML transformation as 
>described below ?
>Part of my XML file is:
>  <URLADR>http:\\www.aaa.com</URLADR>
>  <URLDESC>Homepage of aaa AS</URLDESC>
>  <URLDESC>Homepage of aaa AS NO</URLDESC>
>  <URLDESC>Homepage of aaa AS SE</URLDESC>
>  <URLADR>http:\\www.aaabbb.org</URLADR>
>  <URLDESC>Demo of aaabbb.com</URLDESC>
>  <URLDESC>Demo of aaabbb.com NO</URLDESC>
>and I want to convert this to something like:
>     <URL>
>       <URLADR><http://www.aaa.com>http://www.aaa.com</URLADR>
>       <URLDESC xml:lang="en">Homepage of aaa AS></URLDESC>
>       <URLDESC xml:lang="no">Homepage of aaa AS NO></URLDESC>
>       <URLDESC xml:lang="se">Homepage of aaa AS SE></URLDESC>
>     </URL>
>     <URL>
>       <URLADR><http://www.hubshop.org>http://www.aaabbb.org</URLADR>
>       <URLDESC xml:lang="en">Demo of aaabbb.com></URLDESC>
>       <URLDESC xml:lang="no">Demo of aaabbb.com NO></URLDESC>
>     </URL>
>   </URLLIST>
>Please tell me how to write the XSL file for this.

You are trying to convert a non-tree structure into a tree structure (at 
the information level, it is of course a tree at the syntax level).  This 
requires you to "walk" the tree to find the information you need.

The code below will accomplish your desired result ... note that it acts on 
each URL element using inherent tree-oriented XSLT, but then must walk the 
tree, element node by element node, determining the content for the result 

I hope this helps.

...................... Ken

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

<xsl:output indent="yes"/>

<xsl:template match="/">
     <xsl:for-each select="//URLADR">
         <xsl:copy-of select="."/>
         <!--walk the tree along siblings until next URLADR found-->
         <xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>

<xsl:template match="URLDESC" mode="urldesc"> <!--add description to URL-->
     <xsl:attribute name="xml:lang">
       <xsl:value-of select="following-sibling::*[1][self::URLDESCLANGUAGE]"/>
     <xsl:value-of select="."/>
   <xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>

<xsl:template match="URLDESCLANGUAGE" mode="urldesc"> <!--value used-->
   <xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>

<xsl:template match="URL" mode="urldesc"/> <!--stop at next URL-->

<xsl:template match="*" mode="urldesc">
     <xsl:text/>Unexpected: '<xsl:value-of select="name(.)"/>'<xsl:text/>


G. Ken Holman                      mailto:gkholman@CraneSoftwrights.com
Crane Softwrights Ltd.               http://www.CraneSoftwrights.com/x/
Box 266, Kars, Ontario CANADA K0A-2E0     +1(613)489-0999   (Fax:-0995)
Web site:     XSL/XML/DSSSL/SGML/OmniMark services, training, products.
Book:  Practical Transformation Using XSLT and XPath ISBN 1-894049-06-3
Article: What is XSLT? http://www.xml.com/pub/2000/08/holman/index.html
Next public instructor-led training:      2001-05-01,05-14,05-15,05-16,
-                       05-17,05-21,05-22,06-18,06-21,07-20,07-21,09-19

Training Blitz: 3-days XSLT/XPath, 2-days XSLFO in Ottawa 2001-06-18/22