Sample XML from Schema: Handling Recursion

J

Josh Martin

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
 
J

Joris Gillis

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

Josh Martin

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
 

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,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top