[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

XPath 2.0 expression that detects a cycle of reference

Subject: XPath 2.0 expression that detects a cycle of references?
From: "Costello, Roger L. costello@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 25 Mar 2016 16:04:48 -0000
 XPath 2.0 expression that detects a cycle of reference
Hi Folks,

This XML document has id-idref references that create a circular dependency:

<document>
    <item id="HF">
        <title>Huckleberry Finn</title>
        <for-more-info idref="MT"/>
    </item>
    <item id="MT">
        <name>Mark Twain</name>
        <for-more-info idref="HF"/>
    </item>
</document>

I want an XPath expression which returns true if there is an <item> element
that contains a <for-more-info> element with an idref reference that
ultimately loops back around to the <item> element. For the above XML
document, the XPath expression should return true. Here's an XPath expression
that seems to work as desired:

for $i in /document/item return
      for $j in $i/for-more-info return
            for $k in /document/item[@id eq $j/@idref] return
                  if ($k/for-more-info/@idref eq $i/@id) then true() else ()

That XPath expression works fine when the cycle is between two <item> elements
but it doesn't work when the cycle is between three or more <item> elements.
Is there a way to write an XPath 2.0 expression to detect a cycle, regardless
of how big the cycle is?  /Roger

This XML has a cycle between three <item> elements:

<document>
    <item id="HF">
        <title>Huckleberry Finn</title>
        <for-more-info idref="MT"/>
    </item>
    <item id="MT">
        <name>Mark Twain</name>
        <for-more-info idref="SP"/>
    </item>
    <item id="SP">
        <publisher>Springer</publisher>
        <for-more-info idref="HF"/>
    </item>
</document>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.