[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: XML/XML Transformation - Please Help !!!.....
- From: "G. Ken Holman" <gkholman@CraneSoftwrights.com>
- To: "XML-DEV (E-mail)" <xml-dev@lists.xml.org>
- Date: Mon, 30 Apr 2001 17:53:10 +0800
XSL, XSLT and XPath questions would be better posted to the following list:
http://www.mulberrytech.com/xsl/xsl-list
There are a number of subscribers who would enthusiastically respond to
such questions.
There is also an *excellent* FAQ at:
http://www.dpawson.co.uk
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>
> <URLDESCLANGUAGE>en</URLDESCLANGUAGE>
> <URLDESC>Homepage of aaa AS NO</URLDESC>
> <URLDESCLANGUAGE>no</URLDESCLANGUAGE>
> <URLDESC>Homepage of aaa AS SE</URLDESC>
> <URLDESCLANGUAGE>se</URLDESCLANGUAGE>
>
> <URLADR>http:\\www.aaabbb.org</URLADR>
> <URLDESC>Demo of aaabbb.com</URLDESC>
> <URLDESCLANGUAGE>en</URLDESCLANGUAGE>
> <URLDESC>Demo of aaabbb.com NO</URLDESC>
> <URLDESCLANGUAGE>no</URLDESCLANGUAGE>
>..
>..
>..
>
>and I want to convert this to something like:
>..
>..
> <URLLIST>
> <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
element.
I hope this helps.
...................... Ken
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:template match="/">
<URLLIST>
<xsl:for-each select="//URLADR">
<URL>
<xsl:copy-of select="."/>
<!--walk the tree along siblings until next URLADR found-->
<xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>
</URL>
</xsl:for-each>
</URLLIST>
</xsl:template>
<xsl:template match="URLDESC" mode="urldesc"> <!--add description to URL-->
<xsl:copy>
<xsl:attribute name="xml:lang">
<xsl:value-of select="following-sibling::*[1][self::URLDESCLANGUAGE]"/>
</xsl:attribute>
<xsl:value-of select="."/>
</xsl:copy>
<xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>
</xsl:template>
<xsl:template match="URLDESCLANGUAGE" mode="urldesc"> <!--value used-->
<xsl:apply-templates select="following-sibling::*[1]" mode="urldesc"/>
</xsl:template>
<xsl:template match="URL" mode="urldesc"/> <!--stop at next URL-->
<xsl:template match="*" mode="urldesc">
<xsl:message>
<xsl:text/>Unexpected: '<xsl:value-of select="name(.)"/>'<xsl:text/>
</xsl:message>
</xsl:template>
</xsl:stylesheet>
--
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