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

Re: Compare logical expressions with XPath/XSLT

Subject: Re: Compare logical expressions with XPath/XSLT
From: "David Carlisle d.p.carlisle@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 7 Mar 2019 20:52:16 -0000
Re:  Compare logical expressions with XPath/XSLT
if I understood you correctly you have a fixed number of boolean
variables (3 here) so you can enumerate the truth table of  each
project and want to know that the expression for the sub project is
never true if the expression for the main project is false.

this is not, to put it mildly, well tested but I get

$ saxon9 -it:main prg.xsl
project 1 and 2
false
project 1 and 3
true


from the following which assumes AND and OR always have exactly two
children and that my typing is accurate, neither of which assumption
may be true...

<xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:f="data:,f">

 <xsl:variable name="defs" as="element()">
  <defs>
  <def name="project-1">
    <set>
      <AND>
        <OR>
          <item required="false">A</item>
          <item required="false">B</item>
        </OR>
        <item required="false">C</item>
      </AND>
    </set>
  </def>
  <def name="project-2">
    <set>
      <OR>
        <item required="true">A</item>
        <item required="true">C</item>
      </OR>
    </set>
  </def>
  <def name="project-3">
    <set>
      <item required="false">C</item>
    </set>
  </def>
</defs>
</xsl:variable>

<xsl:template name="main">
 <xsl:message select="'project 1 and 2'"/>
 <xsl:message select="f:check($defs/def[@name='project-1']/set/*,$defs/def[@name='project-2']/set/*)"/>
 <xsl:message select="'project 1 and 3'"/>
 <xsl:message select="f:check($defs/def[@name='project-1']/set/*,$defs/def[@name='project-3']/set/*)"/>
</xsl:template>

<xsl:function name="f:check" as="xs:boolean">
 <xsl:param name="p1" as="element()"/>
 <xsl:param name="p2" as="element()"/>
 <xsl:variable name="tt1" as="xs:boolean*">
  <xsl:sequence select="
  for $a in (true(),false()),$b in (true(),false()),$c in (true(),false())
  return
  f:eval($p1,$a,$b,$c)
  "/>
 </xsl:variable>
 <xsl:variable name="tt2" as="xs:boolean*">
  <xsl:sequence select="
  for $a in (true(),false()),$b in (true(),false()),$c in (true(),false())
  return
  f:eval($p2,$a,$b,$c)
  "/>
 </xsl:variable>
 <xsl:variable name="x" as="xs:boolean*">
  <xsl:for-each select="$tt1">
   <xsl:variable name="pn" select="position()"/>
   <xsl:sequence select="not(.) or $tt2[$pn]"/>
  </xsl:for-each>
 </xsl:variable>
 <xsl:sequence select="every $i in $x satisfies $i"/>
</xsl:function>

<xsl:function name="f:eval" as="xs:boolean">
 <xsl:param name="p" as="element()"/>
 <xsl:param name="a" as="xs:boolean"/>
 <xsl:param name="b" as="xs:boolean"/>
 <xsl:param name="c" as="xs:boolean"/>
 <xsl:choose>
  <xsl:when test="$p/self::item[.='A']">
   <xsl:sequence select="if($p/@required='true') then $a else not($a)"/>
  </xsl:when>
  <xsl:when test="$p/self::item[.='B']">
   <xsl:sequence select="if($p/@required='true') then $b else not($b)"/>
  </xsl:when>
  <xsl:when test="$p/self::item[.='C']">
   <xsl:sequence select="if($p/@required='true') then $c else not($c)"/>
  </xsl:when>
  <xsl:when test="$p/self::AND">
   <xsl:sequence select="f:eval($p/*[1],$a,$b,$c) and
f:eval($p/*[2],$a,$b,$c)"/>
  </xsl:when>
  <xsl:when test="$p/self::OR">
   <xsl:sequence select="f:eval($p/*[1],$a,$b,$c) or f:eval($p/*[2],$a,$b,$c)"/>
  </xsl:when>
  <xsl:otherwise>
   <xsl:sequence select="false()(:shouldn't happen:)"/>
  </xsl:otherwise>
 </xsl:choose>
</xsl:function>

</xsl:stylesheet>

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.