469,362 Members | 2,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,362 developers. It's quick & easy.

Sample XML from Schema: Handling Recursion

comp.text.xml -

I've developed a simple XSLT program that generates a sample XML file
from a schema (note, it won't work in every instance, but works for
this program). My problem: how do I tell if an element encountered in
the schema is recursive? Without knowing this, the sample XML is
generated indefinitely. Here is a simple example:

<schema>
<complexType name="Package">
<element name="PackageID" type="string"/>
<element name="Quantity" type="integer"/>
<element ref="Package" minOccurs="0" maxOccurs="unbounded" />
</complexType>
<element name="Package" type="Package"/>
</schema>

My program simply prints elements and their children recursively. If
an @type or @ref is found, it finds the corresponding simpleType or
complexType and continues from there. In the example above, the
Package element (complexType) would continue to print infinitely. Is
there a way, using XSLT, to determine if an element is recursive, and
thus avoid "re-printing" it?

Thanks,
Josh
Jul 20 '05 #1
2 3919
In the example above, the
Package element (complexType) would continue to print infinitely. Is
there a way, using XSLT, to determine if an element is recursive, and
thus avoid "re-printing" it?

Hi,

I don't know if there is a standard way to deal with it, but I came up with this:

<xsl:if test="0=count(//complexType[@name = current()/@ref][element/@ref=current()/../@name])"/>
will be true if it is recursive.

Using this stylesheet:
<xsl:template match="/">
<html>
<head>
</head>
<body>
<xsl:apply-templates select="//complexType"/>
</body></html>
</xsl:template>

<xsl:template match="complexType">
<h1><xsl:value-of select="local-name()"/> - name: <xsl:value-of select="@name"/></h1>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="*">
<p>
<xsl:value-of select="local-name()"/> - ref: <xsl:value-of select="@ref"/> - recursive:
<xsl:value-of select="0!=count(//complexType[@name = current()/@ref][element/@ref=current()/../@name])"/>
</p>
</xsl:template>
an input XML schema like this:

<complexType name="test">
<element ref="d" />
<element ref="test" />
</complexType>
<complexType name="a">
<element ref="b" />
<element ref="a" />
</complexType>
<complexType name="b">
<element ref="f" />
<element ref="a" />
</complexType>
<complexType name="c">
<element ref="a" />
</complexType>
<complexType name="d">
<element ref="e" />
</complexType>

will be outputed as:

complexType - name: test
element - ref: d - recursive: false

element - ref: test - recursive: true

complexType - name: a
element - ref: b - recursive: true

element - ref: a - recursive: true

complexType - name: b
element - ref: f - recursive: false

element - ref: a - recursive: true

complexType - name: c
element - ref: a - recursive: false

complexType - name: d
element - ref: e - recursive: false
I hope you can use this somehow.

regards,

--
Joris Gillis (http://www.ticalc.org/cgi-bin/acct-v...i?userid=38041)
Ceterum censeo XML omnibus esse utendum
Jul 20 '05 #2
> Hi,

I don't know if there is a standard way to deal with it, but I came up with this:


Joris -

Thanks for the help! I'm tweaking it right now to work with my schema.

Josh
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ruthless | last post: by
2 posts views Thread by Michael Wein | last post: by
8 posts views Thread by Jakob Møbjerg Nielsen | last post: by
4 posts views Thread by Ganesh Muthuvelu | last post: by
1 post views Thread by George2 | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.