[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] XPath 2.0 expression that detects a cycle of references?


Subject: Re: [xsl] XPath 2.0 expression that detects a cycle of references?
From: "Michael Müller-Hillebrand mmh@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Tue, 29 Mar 2016 08:45:43 -0000

> I think you need to write a recursive function to detect cycles in a graph,
and XPath 2.0 does not have sufficient power for this. It can of course be
done in XSLT or XQuery. It can possibly be done in  XPath 3.0, though because
XPath 3.0 functions are anonymous, recursion is tricky.
>
> Michael Kay
> Saxonica

Well, how would that kind of trick look like?

This is my version of a recursive function for Roger's problem (assuming there
can be multiple elements <for-more-info>):

<xsl:function name="my:CycleFound" as="xs:boolean">
  <xsl:param name="this" as="element()?"/>
  <xsl:param name="visited" as="xs:string*"/>
  <xsl:variable name="refId" select="$this/for-more-info/@idref"
as="xs:string*"/>
  <xsl:variable name="refTgt" select="$this/../item[@id = $refId]"
as="element()*"/>

  <xsl:sequence select="
    if (not(exists($refTgt))) then false()
    else if ($refId = $visited) then true()
    else some $e in $refTgt satisfies my:CycleFound($e, ($visited, $e/@id))
    "/>
</xsl:function>

I have no idea how one would have to handle this using XPath 3b& any
suggestions?

Thanks from the curious,

- Michael MH



>
>> Costello, Roger wrote:
>>
>> 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>

[demime 1.01d removed an attachment of type application/pgp-signature which had a name of signature.asc]


Current Thread
Keywords