need some help with xsl / xml

M

Mr_Noob

Hi all,

here is a sample on my xml file :

<systems>
<controllers>
<id>1</id>
<name>controller1</name>
<memory>4096</memory>
<hosts>
<id>1</id>
<name>host1</name>
<memory>256</memory>
<id>2</id>
<name>host2</name>
<memory>256</memory>
</hosts>
<id>2</id>
<name>controller2</name>
<memory>4096</memory>
</controllers>
</systems>

I am trying to write an xsl stylesheet that would generate the
following output :

insert into controllers (name, memory) values (controller1, 4096);
insert into hosts (id, name, memory, controller_id) values (1, host1,
256, 1);
insert into hosts (id, name, memory, controller_id) values (2, host2,
256, 1);
insert into controllers (name, memory) values (controlle2, 4096);

Here is the xsl i've written so far :

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:eek:utput method="text"/>
<xsl:template match="systems">
<xsl:template match="controllers">
insert into controllers (name, memory) values (<xsl:value-of
select="name"/> , <xsl:value-of select="memory"/>);
<xsl:template match="hosts">
insert into hosts (id, name, memory, controller_id) values (<xsl:value-
of select="id"/> , <xsl:value-of select="name"/>, <xsl:value-of
select="memory"/> , <xsl:value-of select="CONTROLLER_Id?"/>);
</xsl:template>
</xsl:template>
</xsl:template>
</xsl:stylesheet>

well as you can see, i don't know how to do such a thing....

any idea?
 
P

Pavel Lepin

Mr_Noob said:
<systems>
<controllers>
<id>1</id>
<name>controller1</name>
<memory>4096</memory>
<hosts>
<id>1</id>
<name>host1</name>
<memory>256</memory>
<id>2</id>
<name>host2</name>
<memory>256</memory>
</hosts>
<id>2</id>
<name>controller2</name>
<memory>4096</memory>
</controllers>
</systems>

Bloody awful. Why don't you structure your XML documents
properly?

<systems>
<controllers>
<controller id="1" name="foo" mem="4096">
<hosts>
<host id="1" name="baz" mem="256"/>
<host id="2" name="quux" mem="256"/>
</hosts>
</controller>
<controller id="2" name="bar" mem="4096"/>
</controllers>
I am trying to write an xsl stylesheet that would generate
the following output...

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="text"/>
<xsl:template match="controllers|hosts">
<xsl:apply-templates select="id"/>
</xsl:template>
<xsl:template match="controllers/id">
<xsl:text>INSERT INTO controllers </xsl:text>
<xsl:text>(id,name,memory) values (</xsl:text>
<xsl:value-of select="number(text())"/>
<xsl:text>,'</xsl:text>
<xsl:value-of
select="following-sibling::name[1]/text()"/>
<xsl:text>',</xsl:text>
<xsl:value-of
select=
"
number(following-sibling::memory[1]/text())
"/>
<xsl:text>);
</xsl:text>
<xsl:apply-templates
select="following-sibling::hosts[1]"/>
</xsl:template>
<xsl:template match="hosts/id">
<xsl:text>INSERT INTO hosts </xsl:text>
<xsl:text>(id,name,memory,controller_id) </xsl:text>
<xsl:text>values (</xsl:text>
<xsl:value-of select="number(text())"/>
<xsl:text>,'</xsl:text>
<xsl:value-of
select="following-sibling::name[1]/text()"/>
<xsl:text>',</xsl:text>
<xsl:value-of
select=
"
number(following-sibling::memory[1]/text())
"/>
<xsl:text>,</xsl:text>
<xsl:value-of
select=
"
number(../preceding-sibling::id[1]/text())
"/>
<xsl:text>);
</xsl:text>
</xsl:template>
</xsl:stylesheet>
 
M

Mr_Noob

Bloody awful. Why don't you structure your XML documents
properly?

I agree, bloody awful indeed. This XML document is generated
automatically , I have to deal with it :(

Thanks a lot for your help
 

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,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top