Can XSL do this, and if so... how?

D

Dominic Myers

Hi there,

this may be a bit out there but I have a XML data file of a hierarchy and
I'd like to output the file using XSL, the thing is I don't know if it works
like that? I've now got an got a SVG file
http://camshag.co.uk/svg/NHSTrust/index.html which reads the data and
outputs the hierarchy as an image using a shed-load of ecmascript, this
works but I'm wondering if there is a simpler way of doing it? I came across
a script in the XML Journal which did it, but only with preformatted XML,
the XML I have isn't formatted :-(.

Please do check the link above as, apart from anything, I'd welcome
constructive criticism on it.

Thanks in advance,

Dominic
 
M

Marrow

Hi Dominic,

Yes, it is possible - transforming XML data to SVG is, in essence, 'just' an
XML-to-XML transformation in XSLT. ('just' being quantitive based on your
previous XSLT experience ;)). How - would be difficult to answer without
seeing the XML data (which doesn't appear to be available anywhere on your
web site).

I would imagine the most likely difficulties you would encounter in trying
to do this in XSLT would be:-
* calculating the overall size (height and width) or the viewport/whatever -
because this will need to be based in some way on the data;
* determining how to draw the tree lines (the complexity of this may depend
a lot on the structure of your initial XML data).

A couple of examples of XML-to-SVG using XSLT (not related to your
requirements - but may be of some use):-

http://www.topxml.com/code/default.asp?p=3&id=v20020307073628&ms=20&l=svg&sw=categ

http://www.topxml.com/code/default.asp?p=3&id=v20020307075937&ms=20&l=svg&sw=categ

http://www.topxml.com/code/default.asp?p=3&id=v20020313183135&ms=20&l=svg&sw=categ
I came across
a script in the XML Journal which did it, but only with preformatted XML,
the XML I have isn't formatted :-(.

"Preformatted XML"? "XML isn't formatted"? What do you mean by
"formatted"?

Cheers
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 
D

Dominic Myers

Marrow said:
Hi Dominic,

Yes, it is possible - transforming XML data to SVG is...
-- snip --

Cheers for the reply Marrow,

To give an example of the "formatted" text it's be something like:

<employee id="3rd in Command">
<name>Eve Grindstaff</name>
<details>01223 603302</details>
<superior>
<employee id="2nd in Command">
<name>Lance Lenker</name>
<details>01223 608062</details>
<superior>
<employee id="1st in Command">
<name>Maricela Defore</name>
<details>01223 602362</details>
<superior/>
</employee>
</superior>
</employee>
</superior>
</employee>

Whereas the data I have is of this format:

<employee id="3rd in Command">
<name>Eve Grindstaff</name>
<details>01223 603302</details>
<superior id="#2nd in Command"/>
</employee>
<employee id="2nd in Command">
<name>Lance Lenker</name>
<details>01223 608062</details>
<superior id="#1st in Command"/>
</employee>
<employee id="1st in Command">
<name>Maricela Defore</name>
<details>01223 602362</details>
</employee>

That's what I meant by formatted XML, if you see what I mean? ;-)
The XML file is here <http://camshag.co.uk/svg/NHSTrust/NHSTrust.owl>, the
link was a bit obscure, blame it on my CSS!

I used masses of script to format the position of the employees within the
hierarchy but was wondering about the processing abilities of XSL?

Cheers, Dom
 
M

Marrow

Hi Dominic,

OK, I see what you mean - probably the phrase that would suit the first
(formatted) example would be hierarchical. The second example, the one you
are actually having to use, is flat. The process in XSLT to process the
flat into hierarchical isn't that difficult because you have references to
the superior. Although listing it from bottom up as the hierarchy seems a
little odd (as it would enevitably lad to repetition of data)? To structure
it top (most superior) down would look something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="xml" indent="yes"/>

<xsl:key name="kSubordinates" match="employee"
use="substring-after(superior/@id,'#')"/>

<!-- template to match root element -->
<xsl:template match="/*">
<!-- copy root element as is -->
<xsl:copy>
<!-- start with employees that have no superior -->
<xsl:apply-templates select="employee[not(superior)]"/>
</xsl:copy>
</xsl:template>

<xsl:template match="employee">
<!-- copy the employee element -->
<xsl:copy>
<!-- aply templates to attributes and child nodes of this employee so
they get copied -->
<xsl:apply-templates select="@* | node()"/>
<!-- create a <subordinates> element as a child of the <employee>
element -->
<subordinates>
<!-- and find all the employees whose superior is the current
employee -->
<xsl:apply-templates select="key('kSubordinates',@id)"/>
</subordinates>
</xsl:copy>
</xsl:template>

<!-- copy all elements -->
<xsl:template match="*">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>

<!-- stop the <superior> element being copied - not needed in hierarchical
form -->
<xsl:template match="superior"/>

<!-- copy attributes and other nodes -->
<xsl:template match="@* | text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>

But you wouldn't need to do this in the XSLT to produce the final SVG - as
the above logic would be built into the process of generating the final SVG.
But a similar approach would be roughly the pattern the SVG building would
require.

Cheers
Marrow
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top