This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: binary-or
- From: Rubén <rubenm at telepolis dot com>
- To: Rubén <xsl-list at lists dot mulberrytech dot com>
- Date: Thu, 26 Sep 2002 17:49:51 +0200
- Subject: Re: [xsl] binary-or
- References: <8335400853.20020925223435@telepolis.com>
- Reply-to: xsl-list at lists dot mulberrytech dot com
Thanks very much for your responses!
I'm gonna use all of them at the project I'm working in my company and earn a
lot of money!! Mwhahahahah!
just joking but.. does any of you have any problem with using their templates
(crediting the author of course)?
============================== Original Message ==============================
From: Rubén
To: Rubén
Date: Wednesday, September 25, 2002, 10:34:35 PM
Subject: [xsl] 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
=========================== End Of Original Message ==========================
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list