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

Re[2]: binary-or

Subject: Re[2]: binary-or
From: Rubén <rubenm@xxxxxxxxxxxxx>
Date: Thu, 26 Sep 2002 17:48:19 +0200
xslt modulus
well.. it's shorter but not much shorter :-P and it needs one additional
xslt:choose in the last xslt:otherwise to deal with cases where the second
operand is longer than the first one, without that check it computes: "0101 or
01011010 = NaN1NaN11111" ('' mod 2 = NaN)

that was just a little criticism :-), admittedly, your implementation is
cleverer than mine, I liked that you avoid using concat() which is really
unnecessary, thanks a lot!


============================== Original Message ==============================
From: Rubén
To: Dion Houston
Date: Wednesday, September 25, 2002, 10:58:00 PM
Subject:  binary-or

Hey Ruben!

How's this for a much shorter version... haven't had a chance to test it exhaustively, but it appears to work:

<xslt:template name="BinaryOr">
        <xslt:param name="op1" select="'0'"/>
        <xslt:param name="op2" select="'0'"/>
                
        <xslt:if test="$op1 != '' or $op2 != ''">
                <xslt:call-template name="BinaryOr">
                        <xslt:with-param name="op1" select="substring($op1, 1, string-length($op1)-1)"/>
                        <xslt:with-param name="op2" select="substring($op2, 1, string-length($op2)-1)"/>
                </xslt:call-template>
                <xslt:choose>
                        <xslt:when test="$op2 mod 2">1</xslt:when>
                        <xslt:otherwise><xslt:value-of select="$op1 mod 2"/></xslt:otherwise>
                </xslt:choose>
        </xslt:if>
</xslt:template>


This is a recursive template that starts from the left most character,
iterating successively.  At each position it'll take a modulus by 2 on the
second param.  If it is 1, then it displays one, otherwise it returns the
modulus of the first param.

HTH!

Dion

-----Original Message-----
From: Rubén [mailto:rubenm@xxxxxxxxxxxxx] 
Sent: Wednesday, September 25, 2002 1:35 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  binary-or


Hello list! I couldn't find a "binary-or" (or "mask-or") operation in XPath 1.1 so I tried to
emulate it with a template. I wrote a recursive implementation but I'm not very
happy with the result. I wonder if there is a simpler (and maybe more efficient)
solution. Any suggestions?

This is the template:

<!--
This template accepts two parameters of type string and performs a "binary-or" operation assuming that parameters represent binary numbers.
Examples:
  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'0011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '1011'

  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'000011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '001011'

  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'110011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '111011'
-->
<xsl:template name="or">
<xsl:param name="operand1" select="''"/>
<xsl:param name="operand2" select="''"/>
<xsl:choose>
   <xsl:when test="string-length($operand1)=0"><xsl:value-of select="$operand2"/></xsl:when>
   <xsl:when test="string-length($operand2)=0"><xsl:value-of select="$operand1"/></xsl:when>
   <xsl:otherwise>
   <xsl:variable name="first" select="number(number(substring($operand1, string-length($operand1))) or number(substring($operand2, string-length($operand2))))"/>
   <xsl:variable name="rest">
      <xsl:call-template name="or">
         <xsl:with-param name="operand1" select="substring($operand1, 1, string-length($operand1)-1)"/>
         <xsl:with-param name="operand2" select="substring($operand2, 1, string-length($operand2)-1)"/>
      </xsl:call-template>
   </xsl:variable>
   <xsl:value-of select="concat($rest, $first)"/>
   </xsl:otherwise>
</xsl:choose>
</xsl:template>

Thanks a lot in advance.








                       * * * TEST FILES (Not really needed I think) * * *
===============================================================================================================
test-or.xml
===============================================================================================================
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test-or.xsl"?>
<test-or>
<test>
   <op1>0000</op1>
   <op2></op2>
</test>
<test>
   <op1>1111</op1>
   <op2></op2>
</test>
<test>
   <op1>1010</op1>
   <op2></op2>
</test>
<test>
   <op1>0101</op1>
   <op2></op2>
</test>
<test>
   <op1></op1>
   <op2>0000</op2>
</test>
<test>
   <op1></op1>
   <op2>1111</op2>
</test>
<test>
   <op1></op1>
   <op2>1010</op2>
</test>
<test>
   <op1></op1>
   <op2>0101</op2>
</test>
<test>
   <op1>0000</op1>
   <op2>0000</op2>
</test>
<test>
   <op1>1111</op1>
   <op2>1111</op2>
</test>
<test>
   <op1>1010</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>0101</op2>
</test>
<test>
   <op1>1010</op1>
   <op2>0101</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>11110101</op1>
   <op2>0101</op2>
</test>
<test>
   <op1>00000101</op1>
   <op2>0101</op2>
</test>
<test>
   <op1>11110101</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>00000101</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>10100101</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>01010101</op1>
   <op2>1010</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>11111010</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>00001010</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>10101010</op2>
</test>
<test>
   <op1>0101</op1>
   <op2>01011010</op2>
</test>
</test-or>

===============================================================================================================
test-or.xsl
===============================================================================================================
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/test-or">
<results>
   <xsl:apply-templates select="test"/>
</results>
</xsl:template>

<xsl:template match="test">
<result>
<xsl:value-of select="op1"/> or <xsl:value-of select="op2"/> = <xsl:call-template name="or"><xsl:with-param name="operand1" select="op1"/><xsl:with-param name="operand2" select="op2"/></xsl:call-template>
</result>
</xsl:template>

<!--
This template accepts two parameters of type string and performs a "binary-or" operation assuming that parameters represent binary numbers.
Examples:
  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'0011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '1011'

  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'000011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '001011'

  <xsl:call-template name="or"><xsl:with-param name="operand1" select="'110011'"/><xsl:with-param name="operand2" select="'1010'"/></xsl:call-template>
  -> '111011'
-->
<xsl:template name="or">
<xsl:param name="operand1" select="''"/>
<xsl:param name="operand2" select="''"/>
<xsl:choose>
   <xsl:when test="string-length($operand1)=0"><xsl:value-of select="$operand2"/></xsl:when>
   <xsl:when test="string-length($operand2)=0"><xsl:value-of select="$operand1"/></xsl:when>
   <xsl:otherwise>
   <xsl:variable name="first" select="number(number(substring($operand1, string-length($operand1))) or number(substring($operand2, string-length($operand2))))"/>
   <xsl:variable name="rest">
      <xsl:call-template name="or">
         <xsl:with-param name="operand1" select="substring($operand1, 1, string-length($operand1)-1)"/>
         <xsl:with-param name="operand2" select="substring($operand2, 1, string-length($operand2)-1)"/>
      </xsl:call-template>
   </xsl:variable>
   <xsl:value-of select="concat($rest, $first)"/>
   </xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

=========================== End Of Original Message ==========================


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread
  • RE: binary-or, (continued)
    • Michael Kay - Thu, 26 Sep 2002 06:37:18 -0400 (EDT)
      • Rubén - Thu, 26 Sep 2002 11:36:21 -0400 (EDT)
    • Rubén - Thu, 26 Sep 2002 11:43:54 -0400 (EDT)
    • Dion Houston - Wed, 25 Sep 2002 16:56:22 -0400 (EDT)
      • Rubén - Thu, 26 Sep 2002 11:42:27 -0400 (EDT) <=
    • McNally, David - Wed, 25 Sep 2002 17:33:05 -0400 (EDT)
      • Rubén - Thu, 26 Sep 2002 11:37:05 -0400 (EDT)
    • McNally, David - Thu, 26 Sep 2002 12:29:28 -0400 (EDT)

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.