Recursion of tags in one tag

C

Cedric Vonck

Hello everybody,

I have a question about xml / xsl(t).

I need to transform a following kind of xml to editable file (format
does not matter).

The xml file look like this:

<...> Root
<tag>
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
....
....
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
</tag>
</...> End Root

Subtag1 & Subtag2 can repeat themselves n-times.
I know that this is xml is wellformed but not wellstructured.

The ideal solution should be:

<...> Root
<tag>
<detail>
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
</detail>
<detail>
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
</detail>
</tag>
</...> End Root

If the xml should like this (in the ideal case) I could get the
conversion to work in no time.

Any suggestions how I could write a correct xsl(t) for the first
example???

Thank you
 
J

Joris Gillis

Subtag1 & Subtag2 can repeat themselves n-times.
I know that this is xml is wellformed but not wellstructured.

The ideal solution should be:

<...> Root
<tag>
<detail>
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
</detail>
<detail>
<subtag1>.....</subtag1>
<subtag2>.....</subtag2>
</detail>
</tag>
</...> End Root

If the xml should like this (in the ideal case) I could get the
conversion to work in no time.

Any suggestions how I could write a correct xsl(t) for the first
example???
Hi,

I'm not really sure if this is really what you asked for, but here's an xslt that converts the first document into the second:

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

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

<xsl:template match="*[subtag1]">
<xsl:copy>
<xsl:apply-templates select="node()[not(self::subtag2)] | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="subtag1">
<details>
<xsl:copy-of select=". | following-sibling::subtag2[1]"/>
</details>
</xsl:template>

</xsl:stylesheet>


regards,
 
C

Cedric Vonck

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

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

<xsl:template match="*[subtag1]">
<xsl:copy>
<xsl:apply-templates select="node()[not(self::subtag2)] | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="subtag1">
<details>
<xsl:copy-of select=". | following-sibling::subtag2[1]"/>
</details>
</xsl:template>

</xsl:stylesheet>


regards,

Thank you for your answer, but I should have made my post more
clearer.
The second example with the (<detail>) tag is no problem for me.
It is the first one that poses problems.

So if possible, could anyone explain to me how I have to write a
decent xsl(t) for it??

Thx
 

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,999
Messages
2,570,244
Members
46,838
Latest member
KandiceChi

Latest Threads

Top