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

B

Bostonasian

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

Joseph Kesselman

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...)
 
B

Bostonasian

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".
 
J

Joseph Kesselman

Bostonasian said:
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.
 
B

Bostonasian

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.

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).
 
R

roy axenov

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.')
 
B

Bostonasian

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.')

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

Joseph Kesselman

Bostonasian said:
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.
 
P

Peter Flynn

Bostonasian said:
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top