Subject: Solution: for Recursively merge (aggregate) and traverse DOM trees (XML files)
From: Laky Tang <tulaky@xxxxxxxxx>
Date: Sat, 24 Jun 2006 21:39:08 -0700 (PDT)
|
Thanks Michael Kay! Your suggestion worked. And the
"<?xml versionfiltered="1.0".... part must have crept
in while I was copy pasting it thru openoffice writer!
For anybody that might be interested, here is the XSLT
that works for my example.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
xmlns:xlink="http://www.w3.org/1999/xlink">
<xsl:template match="a">
<html>
<head>
<title>Sample</title>
</head>
<body>
<h1>
<center>
Sample
</center>
</h1>
<p/>
<table border="2" bgcolor="yellow">
<tr>
<th>P</th>
<th>Q</th>
<th>text</th>
</tr>
<xsl:apply-templates/>
</table>
</body>
</html>
</xsl:template>
<xsl:template match="a" mode="child">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="b">
<!--xsl:for-each select="//b"-->
<xsl:choose>
<xsl:when test="@url">
<xsl:apply-templates select="document(@url)"
mode="child"/>
</xsl:when>
<xsl:otherwise>
<tr>
<td>
<xsl:value-of select="@p"/>
</td>
<td>
<xsl:value-of select="@q"/>
</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:otherwise>
</xsl:choose>
<!--/xsl:for-each-->
</xsl:template>
</xsl:stylesheet>
Date: Tue, 20 Jun 2006 08:15:44 +0100
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Subject: RE: Recursively merge (aggregate) and
traverse DOM trees (XML files)?
Message-ID: <005101c69439$58ad58c0$6401a8c0@turtle>
Your stylesheet needs to include within it an
instruction such as
<xsl:apply-templates select="document(@url)"/>
to dereference the links and recursively process the
referenced
document.
I don't know what this is supposed to be:
<?xml versionfiltered="1.0" encoding="UTF-8"?>
but it isn't XML.
Michael Kay
http://www.saxonica.com/
> -----Original Message-----
> From: Laky Tang [mailto:tulaky@xxxxxxxxx]
> Sent: 20 June 2006 03:46
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: Recursively merge (aggregate) and
traverse DOM
> trees (XML files)?
>
> I am trying to recursively merge a tree of XML files
and then
> apply some templates to it. I googled and also
searched this
> mailing list but did not find any help. I hope this
is
> possible using XSLT. Can somebody please point me in
the
> right direction?
>
> Here is a sample of the input files and desired
output. I
> tried a sample.xsl (see below)which only goes thru
the root
> file, but need help in making it recursively go thru
all the
> linked files as well.
>
> Thanks in advance,
> Laky, Tang
> -----------------
> sample1.xml :
> <?xml versionfiltered="1.0" encoding="UTF-8"?>
> <?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<a> <b p
> ="key1" q="2"> xxx </b> <b url = "sample2.xml"></b>
<b p
> ="key10" q ="27">yyyy</b> </a>
>
> sample2.xml :
> <?xml versionfiltered="1.0" encoding="UTF-8"?> <a>
<b p
> ="key4" q="22"> xxx2 </b> <b url =
"sample3.xml"></b> <b p
> ="key7" q ="37">yyyy2</b> <b url =
"sample4.xml"></b> </a>
>
> sample3.xml :
> <?xml versionfiltered="1.0" encoding="UTF-8"?> <a>
<b p
> ="key5" q="23"> xxx3 </b> <b p ="key6" q
="26">yyyy3</b> </a>
>
> sample4.xml :
> <?xml versionfiltered="1.0" encoding="UTF-8"?> <a>
<b p
> ="key8" q="24"> xxx4 </b> <b p ="key9" q
="25">yyyy4</b> </a>
>
> sample.xsl :
> <?xml versionfiltered="1.0" encoding="UTF-8"?>
> <xsl:stylesheet versionfiltered="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:fo="http://www.w3.org/1999/XSL/Format"
> xmlns:xs="http://www.w3.org/2001/XMLSchema"
> xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
>
xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes"
> xmlns:xlink="http://www.w3.org/1999/xlink">
> <xsl:template match="/">
> <html>
> <head>
> <title>Sample</title>
> </head>
> <h1>
> <center>
> </center>
> </h1>
> <p/>
> <xsl:for-each xml:space="default" select="param">
> <xsl:value-of select="."/> </xsl:for-each> <table
border="2"
> bgcolor="yellow"> <tr> <th>P</th> <th>Q</th>
<th>text</th>
> </tr> <xsl:for-each select="//b"> <tr> <td>
<xsl:value-of
> select="@p"/> </td> <td> <xsl:value-of select="@q"/>
</td>
> <td> <xsl:value-of select="."/> </td> </tr>
</xsl:for-each>
> </table> </html> </xsl:template> </xsl:stylesheet>
>
>
> Desired output (preferably as a html table):
>
> P Q text
> key1 2 xxx
> key4 22 Xxx2
> key5 23 xxx3
> key6 26 yyy3
> Key7 37 yyy2
> key8 24 xxx4
> key9 25 yyy4
> key10 27 yyyy
>
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
|