473,320 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

branching XSLT tree

hello.

All XML and XSLT are processed by preprocessor as a trees.

How can i simply display my XML as some kind of tree.

given xml:

<struct>
<node level="1" no="1">
<node level="2" no="2" />
</node>
<node level="1" no="5">
<node level="2" no="8" />
</node>
</struct>

given xslt:
<xsl:template match="struct">
<html>
<body>
<xsl:for-each select="node">
<xsl:sort select="@level"/>
<blockquote><pre>
<xsl:value-of select="name()"/> <br/>
<xsl:value-of select="@no"/> <br/>
<xsl:value-of select="@level"/>
</pre></blockquote>
</xsl:for-each>
</body>
</html>
</xsl:template>

i'd like with a little help of <blockquote> do indentation
and as a result sth like this:

node 1
node 2
node 3
node 4
node 2
node 1

but my xslt does it linear and the nodes of all levels are in the same
position:
[..]
node 4
node 2
node 1

thanx in advance
greetings R
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20
Jul 20 '05 #1
5 2202
Using the first transformation from my answer to your "Looping templates"
question, and editing it a little we get the following:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*[not(self::node) and node]">
<table border="1">
<tr>
<xsl:apply-templates select="node"/>
</tr>
</table>
</xsl:template>

<xsl:template match="node">
<tr>
<td ><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="@no"/></td>
<xsl:if test="node">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="node"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>
When applied on this source.xml:

<struct>
<node level="1" no="1">
<node level="2" no="2" />
<node level="2" no="3">
<node level="3" no="4"/>
</node>
<node level="2" no="5" />
</node>
<node level="1" no="6">
<node level="2" no="7">
<node level="3" no="8"/>
<node level="3" no="9"/>
</node>
<node level="2" no="10" />
<node level="2" no="11" />
</node>
</struct>
the wanted result is produced (as displayed by a browser):

node 1
node 2

node 3
node 4

node 5

node 6
node 7
node 8

node 9

node 10

node 11

Hope this helped.

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


"Ruthless" <ruthless@NO_SPAM.poczta.onet.pl> wrote in message
news:bs**********@nemesis.news.tpi.pl...
hello.

All XML and XSLT are processed by preprocessor as a trees.

How can i simply display my XML as some kind of tree.

given xml:

<struct>
<node level="1" no="1">
<node level="2" no="2" />
</node>
<node level="1" no="5">
<node level="2" no="8" />
</node>
</struct>

given xslt:
<xsl:template match="struct">
<html>
<body>
<xsl:for-each select="node">
<xsl:sort select="@level"/>
<blockquote><pre>
<xsl:value-of select="name()"/> <br/>
<xsl:value-of select="@no"/> <br/>
<xsl:value-of select="@level"/>
</pre></blockquote>
</xsl:for-each>
</body>
</html>
</xsl:template>

i'd like with a little help of <blockquote> do indentation
and as a result sth like this:

node 1
node 2
node 3
node 4
node 2
node 1

but my xslt does it linear and the nodes of all levels are in the same
position:
[..]
node 4
node 2
node 1

thanx in advance
greetings R
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20


Jul 20 '05 #2
thanks again ;D

greetings R

Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomo¶ci
news:bs***********@ID-152440.news.uni-berlin.de...
Using the first transformation from my answer to your "Looping templates"
question, and editing it a little we get the following:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*[not(self::node) and node]">
<table border="1">
<tr>
<xsl:apply-templates select="node"/>
</tr>
</table>
</xsl:template>

<xsl:template match="node">
<tr>
<td ><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="@no"/></td>
<xsl:if test="node">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="node"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>
When applied on this source.xml:

<struct>
<node level="1" no="1">
<node level="2" no="2" />
<node level="2" no="3">
<node level="3" no="4"/>
</node>
<node level="2" no="5" />
</node>
<node level="1" no="6">
<node level="2" no="7">
<node level="3" no="8"/>
<node level="3" no="9"/>
</node>
<node level="2" no="10" />
<node level="2" no="11" />
</node>
</struct>
the wanted result is produced (as displayed by a browser):

node 1
node 2

node 3
node 4

node 5

node 6
node 7
node 8

node 9

node 10

node 11

Hope this helped.

Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html


"Ruthless" <ruthless@NO_SPAM.poczta.onet.pl> wrote in message
news:bs**********@nemesis.news.tpi.pl...
hello.

All XML and XSLT are processed by preprocessor as a trees.

How can i simply display my XML as some kind of tree.

given xml:

<struct>
<node level="1" no="1">
<node level="2" no="2" />
</node>
<node level="1" no="5">
<node level="2" no="8" />
</node>
</struct>

given xslt:
<xsl:template match="struct">
<html>
<body>
<xsl:for-each select="node">
<xsl:sort select="@level"/>
<blockquote><pre>
<xsl:value-of select="name()"/> <br/>
<xsl:value-of select="@no"/> <br/>
<xsl:value-of select="@level"/>
</pre></blockquote>
</xsl:for-each>
</body>
</html>
</xsl:template>

i'd like with a little help of <blockquote> do indentation
and as a result sth like this:

node 1
node 2
node 3
node 4
node 2
node 1

but my xslt does it linear and the nodes of all levels are in the same
position:
[..]
node 4
node 2
node 1

thanx in advance
greetings R
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20
Jul 20 '05 #3
I thought about my example and thought about what might happen if e.g.
element node will be inside some other hierarchy.

For instance - i took my doc.xml and created family.xml

I took <node> as <person>. I've created generations, mariages, singles, and
children(mariages) for new recursive generations.

Still does node has its own hierarchy inside generation hierarchy(i hope i
made myself clear ;))

I tried to branch(display) only the <person>

given xml:

<?xml version="1.0" encoding="iso-8859-2"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<tree>
<generation level="1">
<mariage>
<person>
<first_name>Aaa</first_name>
<last_name>Qwq</last_name>
</person>
<person>
<first_name>Bbb</first_name>
<last_name>aaa</last_name>
</person>
<children>
<generation level="2">
<mariage>
<person>
<first_name>MMM</first_name>
<last_name>qqq</last_name>
</person>
<person>
<first_name>P</first_name>
<last_name>K</last_name>
</person>
<children/>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>
</generation>
</children>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>

</generation>
</tree>

and yours the stylesheet(a bit modified):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*[not(self::person) and person]">
<table border="1">
<tr>
<xsl:apply-templates select="person"/>
</tr>
</table>
</xsl:template>

<xsl:template match="person">
<tr>
<td ><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="first_name"/></td>
<xsl:if test="person">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="person"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>

I tried to display all the persons - but only top generation(level='1') was
displayed

I don't know how to ommit all those unnecessary(from my point of view, as
far as i'm only interested in persons) elements and simply display only
persons as a branching table.

thanks in advance
you already helped me a lot with the understanding the XSLT

greetings R
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20
Jul 20 '05 #4

"Ruthless" <ruthless@NO_SPAM.poczta.onet.pl> wrote in message
news:bs**********@atlantis.news.tpi.pl...
I thought about my example and thought about what might happen if e.g.
element node will be inside some other hierarchy.

For instance - i took my doc.xml and created family.xml

I took <node> as <person>. I've created generations, mariages, singles, and children(mariages) for new recursive generations.

Still does node has its own hierarchy inside generation hierarchy(i hope i
made myself clear ;))

I tried to branch(display) only the <person>

given xml:

<?xml version="1.0" encoding="iso-8859-2"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<tree>
<generation level="1">
<mariage>
<person>
<first_name>Aaa</first_name>
<last_name>Qwq</last_name>
</person>
<person>
<first_name>Bbb</first_name>
<last_name>aaa</last_name>
</person>
<children>
<generation level="2">
<mariage>
<person>
<first_name>MMM</first_name>
<last_name>qqq</last_name>
</person>
<person>
<first_name>P</first_name>
<last_name>K</last_name>
</person>
<children/>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>
</generation>
</children>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>

</generation>
</tree>

and yours the stylesheet(a bit modified):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*[not(self::person) and person]">
<table border="1">
<tr>
<xsl:apply-templates select="person"/>
</tr>
</table>
</xsl:template>

<xsl:template match="person">
<tr>
<td ><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="first_name"/></td>
<xsl:if test="person">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="person"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>

I tried to display all the persons - but only top generation(level='1') was displayed

I don't know how to ommit all those unnecessary(from my point of view, as
far as i'm only interested in persons) elements and simply display only
persons as a branching table.

thanks in advance
you already helped me a lot with the understanding the XSLT


Dear R,

Yes, this is possible.

I started with a transformation, which produces a nice hierarchical html
display of all elements in the xml tree.

Then I masked all elements, whose name is not the same as the value of a
special xsl:param.

Here's the result:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pNodeName" select="'person'"/>

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*">
<tr>
<td >
<xsl:if test="name() = $pNodeName">
<xsl:value-of select="name()"/>
</xsl:if>
<xsl:text> </xsl:text>
</td>
<td>
<xsl:if test="name() = $pNodeName">
<xsl:variable name="vNodeNum">
<xsl:number count="*" level="multiple"/>
</xsl:variable>
<xsl:value-of select="$vNodeNum"/>
</xsl:if>
<xsl:text> </xsl:text>
</td>
<xsl:if test="*">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="*"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>
Hope that this helped.
Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html
Jul 20 '05 #5
once again thanks

greetings R

Użytkownik "Dimitre Novatchev" <dn********@yahoo.com> napisał w wiadomo¶ci
news:bt************@ID-152440.news.uni-berlin.de...

"Ruthless" <ruthless@NO_SPAM.poczta.onet.pl> wrote in message
news:bs**********@atlantis.news.tpi.pl...
I thought about my example and thought about what might happen if e.g.
element node will be inside some other hierarchy.

For instance - i took my doc.xml and created family.xml

I took <node> as <person>. I've created generations, mariages, singles,

and
children(mariages) for new recursive generations.

Still does node has its own hierarchy inside generation hierarchy(i hope i made myself clear ;))

I tried to branch(display) only the <person>

given xml:

<?xml version="1.0" encoding="iso-8859-2"?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>
<tree>
<generation level="1">
<mariage>
<person>
<first_name>Aaa</first_name>
<last_name>Qwq</last_name>
</person>
<person>
<first_name>Bbb</first_name>
<last_name>aaa</last_name>
</person>
<children>
<generation level="2">
<mariage>
<person>
<first_name>MMM</first_name>
<last_name>qqq</last_name>
</person>
<person>
<first_name>P</first_name>
<last_name>K</last_name>
</person>
<children/>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>
</generation>
</children>
</mariage>
<single>
<person>
<first_name>P</first_name>
<last_name>ww</last_name>
</person>
</single>

</generation>
</tree>

and yours the stylesheet(a bit modified):

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*[not(self::person) and person]">
<table border="1">
<tr>
<xsl:apply-templates select="person"/>
</tr>
</table>
</xsl:template>

<xsl:template match="person">
<tr>
<td ><xsl:value-of select="name()"/></td>
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="first_name"/></td>
<xsl:if test="person">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="person"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>

I tried to display all the persons - but only top generation(level='1')

was
displayed

I don't know how to ommit all those unnecessary(from my point of view, as far as i'm only interested in persons) elements and simply display only
persons as a branching table.

thanks in advance
you already helped me a lot with the understanding the XSLT


Dear R,

Yes, this is possible.

I started with a transformation, which produces a nice hierarchical html
display of all elements in the xml tree.

Then I masked all elements, whose name is not the same as the value of a
special xsl:param.

Here's the result:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:param name="pNodeName" select="'person'"/>

<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="*">
<tr>
<td >
<xsl:if test="name() = $pNodeName">
<xsl:value-of select="name()"/>
</xsl:if>
<xsl:text> </xsl:text>
</td>
<td>
<xsl:if test="name() = $pNodeName">
<xsl:variable name="vNodeNum">
<xsl:number count="*" level="multiple"/>
</xsl:variable>
<xsl:value-of select="$vNodeNum"/>
</xsl:if>
<xsl:text> </xsl:text>
</td>
<xsl:if test="*">
<tr>
<td>&#xA0;&#xA0;&#xA0;</td>
<td>
<table border="1">
<xsl:apply-templates select="*"/>
</table>
</td>
</tr>
</xsl:if>
</tr>
</xsl:template>
</xsl:stylesheet>
Hope that this helped.
Dimitre Novatchev.
FXSL developer, XML Insider,

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.554 / Virus Database: 346 - Release Date: 03-12-20
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Sergio del Amo | last post by:
Hi, I use the xslt functions provided by php. I am running in my computer the package xampp(www.apachefriends.org) which includes php/apache/mysql .. In this package the php includes the sablotron...
2
by: Ralf Wahner | last post by:
Dear Masters of XSLT Could I ask you for a clue on the following question? I'd like to use XSLT to transform an XML source file to LaTeX. In the following small example the <para> Element...
5
by: Don Garrett | last post by:
I have an XML document at the root of a directory tree that contains relative URIs to resources in a directory tree. During XSLT processing, these URI's can be used without any problems to...
6
by: Prashanth Ellina | last post by:
Hi, I am an XSLT newbie. I need to solve something which I will express in pseudo-code. function f1() { xmltree = <emptydom> addnamevaluepair(xmltree, "size","100")...
3
by: Jack Fox | last post by:
I've never had the need to work with XML, but I believe I now have an appropriate application. I have time-series data in objects organized as a tree that I want an ASP.NET program to write out to...
6
by: Mike P | last post by:
I am quite new to XML and XSLT, and I know you can apply XSLT to XML to display data in an XML file according to the XSLT file, but is it possible to apply an XSLT file to page/s of HTML, so that...
2
by: sebastian.langer | last post by:
Hi! Certainly somebody had my problem before and could give me just some hints, how to solve it: I have an xml file, which contains scientific data in a tree form. The data should be changed...
18
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shćllîpôpď 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.