Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 20th, 2005, 08:46 AM
Josh Martin
Guest
 
Posts: n/a
Default 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
  #2  
Old July 20th, 2005, 08:47 AM
Joris Gillis
Guest
 
Posts: n/a
Default Re: Sample XML from Schema: Handling Recursion

[color=blue]
> 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?[/color]


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
  #3  
Old July 20th, 2005, 08:47 AM
Josh Martin
Guest
 
Posts: n/a
Default Re: Sample XML from Schema: Handling Recursion

> Hi,[color=blue]
>
> I don't know if there is a standard way to deal with it, but I came up with this:
>[/color]

Joris -

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

Josh
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles