Sample XML from Schema: Handling Recursion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Josh Martin

    #1

    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="unbo unded" />
    </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
  • Joris Gillis

    #2
    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="complexT ype">
    <h1><xsl:valu e-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!=coun t(//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

    Comment

    • Josh Martin

      #3
      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

      Comment

      Working...