need some help with xsl / xml

Discussion in 'XML' started by Mr_Noob, Dec 17, 2007.

  1. Mr_Noob

    Mr_Noob Guest

    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?
     
    Mr_Noob, Dec 17, 2007
    #1
    1. Advertising

  2. Mr_Noob

    Pavel Lepin Guest

    Mr_Noob <> wrote in
    <>:
    > <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>
    </systems>

    > 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>

    --
    ....also, I submit that we all must honourably commit seppuku
    right now rather than serve the Dark Side by producing the
    HTML 5 spec.
     
    Pavel Lepin, Dec 17, 2007
    #2
    1. Advertising

  3. Mr_Noob

    Mr_Noob Guest


    > 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
     
    Mr_Noob, Dec 17, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    0
    Views:
    684
    Guest
    Feb 25, 2004
  2. Adam Wasi
    Replies:
    0
    Views:
    335
    Adam Wasi
    Sep 1, 2003
  3. Frank
    Replies:
    1
    Views:
    745
    Dimitre Novatchev
    Sep 12, 2003
  4. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  5. Domino
    Replies:
    5
    Views:
    383
    dorayme
    Nov 5, 2006
Loading...

Share This Page