Formatting vertically liner xml doc to hierarchy format dynamically in any depth.

Discussion in 'XML' started by Bostonasian, Feb 14, 2007.

  1. Bostonasian

    Bostonasian Guest

    I don't even know where to begin this as hierarchy is always confusing
    to construct dynamically in any form(query, xslt,etc). However it is
    very easy to follow when it's presented. I've done breaking a linear
    doc to 2 levels of hierarchy before, but that case had fixed level(its
    depth didn't go any further than 2).

    I have xml doc in attribute oriented format as following :
    <Root>
    <Survey ID="1" ControlType="Label" ParentID="1" Text="1. Commute"/>
    <Survey ID="2" ControlType="Label" ParentID="1" Text="How do you
    commute?"/>
    <Survey ID="3" ControlType="DropDown" ParentID="2" Text="Car"/>
    <Survey ID="4" ControlType="DropDown" ParentID="2" Text="Subway"/>
    <Survey ID="5" ControlType="DropDown" ParentID="2" Text="Commuter
    Rail"/>
    <Survey ID="5" ControlType="DropDown" ParentID="2" Text="Bus"/>
    <Survey ID="7" ControlType="DropDown" ParentID="2" Text="Walk"/>
    <Survey ID="8" ControlType="Label" ParentID="1" Text="How long is
    your commute?"/>
    <Survey ID="9" ControlType="DropDown" ParentID="8" Text="-30min"/>
    <Survey ID="10" ControlType="DropDown" ParentID="8" Text="30min - 1
    hour"/>
    <Survey ID="11" ControlType="DropDown" ParentID="8" Text="1 hour+"/>
    <Survey ID="12" ControlType="Label" ParentID="12" Text="2. Your
    company"/>
    <Survey ID="13" ControlType="TextBox" ParentID="12" Text="Company
    name"/>
    <Survey ID="14" ControlType="TextBox" ParentID="12" Text="Employer"/>
    <Survey ID="15" ControlType="TextBox" ParentID="15" Text="3. Other
    comment"/>
    </Root>

    What I need to do is to format above in heirarchy form. If attribute
    "ID" and "ParentID" are equal, it means that that node comes on top
    level. So far, the depth of the hierarchy doesn't go any deeper than 3
    levels. But I can't count on it. So ultimately what I want to have is
    following :

    <Root>
    <Survey ID="1" ControlType="Label" Text="1. Commute">
    <Survey ID="2" ControlType="Label" Text="How do you commute?">
    <Survey ID="4" ControlType="DropDown" Text="Subway"/>
    <Survey ID="5" ControlType="DropDown" Text="Commuter Rail"/>
    <Survey ID="5" ControlType="DropDown" Text="Bus"/>
    <Survey ID="7" ControlType="DropDown" Text="Walk"/>
    </Survey>
    <Survey ID="8" ControlType="Label" Text="How long is your commute?">
    <Survey ID="9" ControlType="DropDown" Text="-30min"/>
    <Survey ID="10" ControlType="DropDown" Text="30min - 1 hour"/>
    <Survey ID="11" ControlType="DropDown" Text="1 hour+"/>
    </Survey>
    </Survey>
    <Survey ID="12" ControlType="Label" Text="2. Your company">
    <Survey ID="13" ControlType="TextBox" Text="Company name"/>
    <Survey ID="14" ControlType="TextBox" Text="Employer"/>
    </Survey>
    <Survey ID="15" ControlType="TextBox" Text="3. Other comment"/>
    </Root>

    Has anyone does anything like this? If anyone know how to format
    hierarchy in any depth, that'd be great, but I'd like know 3 levels at
    least.
    Thanks in advance.
    Bostonasian, Feb 14, 2007
    #1
    1. Advertising

  2. Re: Formatting vertically liner xml doc to hierarchy format dynamicallyin any depth.

    Why not just use the fact that XML is inherently tree-structured, and
    let the tree hierarchy be your data hierarchy, rather than trying to
    link things with IDs? MUCH easier to process for both machines and humans.

    <Root>
    <Survey ControlType="Label" Text="1. Commute">
    <Survey ControlType="Label" Text="How do you
    commute?">
    <Survey ControlType="DropDown" Text="Car"/>
    <Survey ControlType="DropDown" Text="Subway"/>
    </Survey>

    .... and so on.

    (And actually, I'd argue that specifying that the list is a dropdown
    belongs at the level of the LIST rather than on the choices. The first
    step in using hierarchy is to define a hierarchy that expresses the
    concepts you actually want to work with...)
    Joseph Kesselman, Feb 14, 2007
    #2
    1. Advertising

  3. Bostonasian

    Bostonasian Guest

    On Feb 14, 11:41 am, Joseph Kesselman <>
    wrote:
    > Why not just use the fact that XML is inherently tree-structured, and
    > let the tree hierarchy be your data hierarchy, rather than trying to
    > link things with IDs? MUCH easier to process for both machines and humans.
    >
    > <Root>
    > <Survey ControlType="Label" Text="1. Commute">
    > <Survey ControlType="Label" Text="How do you
    > commute?">
    > <Survey ControlType="DropDown" Text="Car"/>
    > <Survey ControlType="DropDown" Text="Subway"/>
    > </Survey>
    >
    > ... and so on.
    >
    > (And actually, I'd argue that specifying that the list is a dropdown
    > belongs at the level of the LIST rather than on the choices. The first
    > step in using hierarchy is to define a hierarchy that expresses the
    > concepts you actually want to work with...)


    Unfortunately the format on the top is what I get from third party and
    I do have to work with it. I have no control over "Let's use this
    format, screw the one you have".
    Bostonasian, Feb 14, 2007
    #3
  4. Re: Formatting vertically liner xml doc to hierarchy format dynamicallyin any depth.

    Bostonasian wrote:
    > Unfortunately the format on the top is what I get from third party


    OK, in that case the first step may be to write a stylesheet that
    restructures it along more reasonable lines. <smile/> This is a
    straightforward grouping problem: For each element, gather all the
    elements which refer to it as their parent and move them into it as
    children, processing them recursively to gather their children in turn.

    Once things are in that form, other processing should be a lot more
    straightforward.


    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Feb 14, 2007
    #4
  5. Bostonasian

    Bostonasian Guest

    On Feb 14, 1:13 pm, Joseph Kesselman <>
    wrote:
    > Bostonasian wrote:
    > > Unfortunately the format on the top is what I get from third party

    >
    > OK, in that case the first step may be to write a stylesheet that
    > restructures it along more reasonable lines. <smile/> This is a
    > straightforward grouping problem: For each element, gather all the
    > elements which refer to it as their parent and move them into it as
    > children, processing them recursively to gather their children in turn.
    >
    > Once things are in that form, other processing should be a lot more
    > straightforward.
    >
    > --
    > Joe Kesselman / Beware the fury of a patient man. -- John Dryden


    Actually I have that approach already in my mind.
    The reason I posted this is because I am having trouble with
    explicitly manifesting detail solution(XSLT code to be exact).
    Bostonasian, Feb 14, 2007
    #5
  6. Bostonasian

    roy axenov Guest

    On Feb 14, 6:48 pm, "Bostonasian" <> wrote:
    > On Feb 14, 11:41 am, Joseph Kesselman
    > <> wrote:
    >
    > > Why not just use the fact that XML is inherently
    > > tree-structured, and let the tree hierarchy be your
    > > data hierarchy, rather than trying to link things with
    > > IDs? MUCH easier to process for both machines and
    > > humans.

    >
    > > <Root>
    > > <Survey ControlType="Label" Text="1. Commute">
    > > <Survey ControlType="Label" Text="How do you
    > > commute?">
    > > <Survey ControlType="DropDown" Text="Car"/>
    > > <Survey ControlType="DropDown" Text="Subway"/>
    > > </Survey>

    >
    > Unfortunately the format on the top is what I get from
    > third party and I do have to work with it. I have no
    > control over "Let's use this format, screw the one you
    > have".


    I would heartily recommend contacting the third party in
    question and mildly suggesting to them to hire a borking
    XML expert instead of saddling some DBA type with the task
    of designing XML schemata.

    (Oh, I just can see that: 'Hey Fred, we need to uh do urgh
    some um XML stuff. Here, take this XML Expertise In Six
    Productive Lunch-Breaks. One of my pointy-haired flunkies
    told me it was really great.')

    --
    roy axenov
    roy axenov, Feb 14, 2007
    #6
  7. Bostonasian

    Bostonasian Guest

    On Feb 14, 2:41 pm, "roy axenov" <> wrote:
    > On Feb 14, 6:48 pm, "Bostonasian" <> wrote:
    >
    >
    >
    > > On Feb 14, 11:41 am, Joseph Kesselman
    > > <> wrote:

    >
    > > > Why not just use the fact that XML is inherently
    > > > tree-structured, and let the tree hierarchy be your
    > > > data hierarchy, rather than trying to link things with
    > > > IDs? MUCH easier to process for both machines and
    > > > humans.

    >
    > > > <Root>
    > > > <Survey ControlType="Label" Text="1. Commute">
    > > > <Survey ControlType="Label" Text="How do you
    > > > commute?">
    > > > <Survey ControlType="DropDown" Text="Car"/>
    > > > <Survey ControlType="DropDown" Text="Subway"/>
    > > > </Survey>

    >
    > > Unfortunately the format on the top is what I get from
    > > third party and I do have to work with it. I have no
    > > control over "Let's use this format, screw the one you
    > > have".

    >
    > I would heartily recommend contacting the third party in
    > question and mildly suggesting to them to hire a borking
    > XML expert instead of saddling some DBA type with the task
    > of designing XML schemata.
    >
    > (Oh, I just can see that: 'Hey Fred, we need to uh do urgh
    > some um XML stuff. Here, take this XML Expertise In Six
    > Productive Lunch-Breaks. One of my pointy-haired flunkies
    > told me it was really great.')
    >
    > --
    > roy axenov


    Yes I did try that. But for whatever reason, their turn around time
    takes longer than me creating XSLT to transform it. Yes I know, it's
    all bureaucratic BS and I am already frustrated with this even before
    coding. And again, if that was all possible and have grace period time
    to negotiate those issue, I wouldn't have posted this here.
    Bostonasian, Feb 14, 2007
    #7
  8. Re: Formatting vertically liner xml doc to hierarchy format dynamicallyin any depth.

    Bostonasian wrote:
    > The reason I posted this is because I am having trouble with
    > explicitly manifesting detail solution(XSLT code to be exact).


    Well, I gave you an algorithm, but since you're still lost I'll rephrase
    it into a completely untested sketch.

    <xsl:stylesheet ... and so on ... >
    <xsl:template match="/">
    <xsl:apply-templates select="/Root/Survey[@ID=1]"/>
    </xsl:template>

    <xsl:template match="Survey">
    <xsl:copy>
    <xsl:apply-template select="@*"/>
    <xsl:apply-template select="/Root/Survey[@ParentID=current()/@ID]"/>
    </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node">
    <xsl:copy>
    <xsl:apply-template select="@*|node"/>
    </xsl:copy>
    </xsl:template>

    </xsl:stylesheet>

    If you don't immediately understand how this works, I strongly recommend
    going back and doing a few basic XSLT tutorials. This really is a
    third-homework-assignment level of complexity; maybe earlier depending
    on what the prereqs were.

    --
    Joe Kesselman / Beware the fury of a patient man. -- John Dryden
    Joseph Kesselman, Feb 14, 2007
    #8
  9. Bostonasian

    Peter Flynn Guest

    Re: Formatting vertically liner xml doc to hierarchy format dynamicallyin any depth.

    Bostonasian wrote:
    > Yes I did try that. But for whatever reason, their turn around time
    > takes longer than me creating XSLT to transform it. Yes I know, it's
    > all bureaucratic BS and I am already frustrated with this even before
    > coding. And again, if that was all possible and have grace period
    > time to negotiate those issue, I wouldn't have posted this here.


    Just make sure you charge them for the time in your invoice.
    If they're that stupid, they'll pay anyway because they won't
    realise their error.

    ///Peter
    Peter Flynn, Feb 15, 2007
    #9
    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. John Wallace
    Replies:
    0
    Views:
    456
    John Wallace
    Jul 22, 2003
  2. Scott M.

    Re: Format XML Doc from Javascript

    Scott M., Aug 10, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    2,681
    gani21
    Oct 8, 2009
  3. Matt
    Replies:
    3
    Views:
    480
    Tor Iver Wilhelmsen
    Sep 17, 2004
  4. Praveen Chhangani

    Converting a org.jdom DOC to org.w3c DOC

    Praveen Chhangani, Aug 5, 2003, in forum: XML
    Replies:
    2
    Views:
    949
    Johannes Koch
    Aug 7, 2003
  5. News123
    Replies:
    0
    Views:
    284
    News123
    Feb 6, 2010
Loading...

Share This Page