XSLT Script for Groups (Addresses in HEX format)

XSLT Script for Groups (Addresses in HEX format)

  1. daniel
    Here's an example of how to display Addresses in Hexadecimal format. We'll start with the Groups script from "Tutorial : Hacking the Database Part 2" and modify it to show both the Application and Group Addresses in Hexadecimal format. Here is some example output from the script :

    --------------------------------------------------------------------

    INSTALLATION: VM-DCXP
    Project: MYPROJ

    Network: 254
    Name: Local Network
    Type:Serial,COM2

    38, "Lighting", FF, "<Unused>"
    38, "Lighting", 1, "Group 1"
    38, "Lighting", 2, "Group 2"
    38, "Lighting", 3, "Group 3"
    38, "Lighting", 4, "Group 4"
    38, "Lighting", 22, "Group 34"
    38, "Lighting", 23, "Group 35"
    38, "Lighting", 24, "Group 36"
    38, "Lighting", 25, "Group 37"
    E0, "Telephony", FF, "<Unused>"
    CA, "Trigger Control", FF, "<Unused>"
    CB, "Enable Control", FF, "<Unused>"
    88, "Heating", FF, "<Unused>"
    FF, "Unused", FF, "<Unused>"

    --------------------------------------------------------------------

    The script itself is in the next message in this thread. You'll notice the inclusion of a small library of functions which allows you to perform various hexadecimal operations.

    Copy and paste this script into your .XSL file

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <?xml-stylesheet type="text/xsl" href="copy002.xsl"?>
    <xsl:stylesheet version="2.0" xmlns:xsl="[URL]http://www.w3.org/1999/XSL/Transform[/URL]" xmlns:fo="[URL]http://www.w3.org/1999/XSL/Format[/URL]" xmlns:xs="[URL]http://www.w3.org/2001/XMLSchema[/URL]" xmlns:fn="[URL]http://www.w3.org/2004/07/xpath-functions[/URL]" xmlns:xdt="[URL]http://www.w3.org/2004/07/xpath-datatypes[/URL]">
    
    <xsl:template match="Installation">
    <xsl:apply-templates select="InstallationDetail"/>
    <xsl:apply-templates select="Project"/>
    </xsl:template>
    
    <xsl:template match="InstallationDetail">
    INSTALLATION: <xsl:value-of select="Hostname" />
    <p/>
    </xsl:template>
    
    <xsl:template match="Project">
    Project: <xsl:value-of select="TagName" />
    <p/>
    <xsl:apply-templates select="Network"/>
    </xsl:template>
    
    <xsl:template match="Network">
    <p/>
    Network: <xsl:value-of select="Address" />
    <br/>
    Name: <xsl:value-of select="TagName" />
    <br/>
    <xsl:apply-templates select="Interface"/>
    
    <xsl:apply-templates select="Application">
    </xsl:apply-templates>
    </xsl:template>
    
    <xsl:template match="Interface">
    Type:<xsl:value-of select="InterfaceType" />,<xsl:value-of select="InterfaceAddress"/>
    <p/>
    </xsl:template>
    
    <xsl:template match="Application">
    <xsl:apply-templates select="Group"/>
    </xsl:template>
    
    <xsl:template match="Group">
    
        <xsl:variable name="apphex">
          <xsl:call-template name="decToHex">
            <xsl:with-param name="decVal" select="../Address"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="grphex">
          <xsl:call-template name="decToHex">
            <xsl:with-param name="decVal" select="Address"/>
          </xsl:call-template>
        </xsl:variable>
    
    <!-- <xsl:value-of select="../Address"/>, -->
    <xsl:value-of select="$apphex"/>,
    "<xsl:value-of select="../TagName"/>",
    <!-- <xsl:value-of select="Address"/>, -->
    <xsl:value-of select="$grphex"/>,
    "<xsl:value-of select="TagName"/>"<br/>
    </xsl:template>
    
    
    <!-- HEXADECIMAL MATH LIBRARY : BEGIN -->
    
    <xsl:template name="singleDecToHex">
      <xsl:param name="num"/>
      <xsl:choose>
        <xsl:when test="$num &amp;lt; 16 and $num &amp;gt;= 0">
          <xsl:variable name="table" select="'0123456789ABCDEF'"/>
          <xsl:value-of select="substring($table,$num + 1,1)"/>
        </xsl:when>
        <xsl:eek:therwise>
          <xsl:message>
    Number to convert to hexadecimal out of range</xsl:message>
        </xsl:eek:therwise>
      </xsl:choose>
    </xsl:template>
    
    <xsl:template name="singleHexToDec">
      <xsl:param name="hex"/>
      <xsl:variable name="table" select="'0123456789ABCDEF'"/>
      <xsl:value-of select="string-length(substring-before($table,$hex))"/>
    </xsl:template>
     
    <xsl:template name="findPower">
        <xsl:param name="value"/>
        <xsl:param name="currPower"/>
        <xsl:param name="multiplier"/>
        <xsl:param name="accumulator"/>
        <xsl:choose>
          <xsl:when test="$value &amp;lt; $accumulator">
            <xsl:value-of select="$accumulator div $multiplier"/>
          </xsl:when>
          <xsl:eek:therwise>
            <xsl:call-template name="findPower">
              <xsl:with-param name="value" select="$value"/>
              <xsl:with-param name="currPower" select="$currPower + 1"/>
              <xsl:with-param name="multiplier" select="$multiplier"/>
              <xsl:with-param name="accumulator"
         select="$accumulator * $multiplier"/>
            </xsl:call-template>
          </xsl:eek:therwise>
        </xsl:choose>
    </xsl:template>
     
    <xsl:template name="decToHex">
        <xsl:param name="decVal"/>
        <xsl:variable name="power">
          <xsl:call-template name="findPower">
            <xsl:with-param name="value" select="$decVal"/>
            <xsl:with-param name="currPower" select="0"/>
            <xsl:with-param name="multiplier" select="16"/>
           <xsl:with-param name="accumulator" select="1"/>
         <!-- everything to 0 power is 1 -->
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="decToHexIter">
          <xsl:with-param name="decVal" select="$decVal"/>
          <xsl:with-param name="hexVal" select="''"/>
          <xsl:with-param name="power" select="$power"/>
        </xsl:call-template>
    </xsl:template>
     
    <xsl:template name="decToHexIter">
        <xsl:param name="decVal"/>
        <xsl:param name="hexVal"/>
        <xsl:param name="power"/>
        <xsl:choose>
          <xsl:when test="$decVal &amp;gt; 0">
            <xsl:variable name="currDecVal"
         select="($decVal - ($decVal mod $power)) div $power"/>
            <xsl:variable name="hexDigit">
              <xsl:call-template name="singleDecToHex">
                <xsl:with-param name="num" select="$currDecVal"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="decToHexIter">
              <xsl:with-param name="decVal"
     select="$decVal - ($currDecVal * $power)"/>
              <xsl:with-param name="hexVal" select="concat($hexVal,$hexDigit)"/>
              <xsl:with-param name="power" select="$power div 16"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:eek:therwise>
            <xsl:value-of select="$hexVal"/>
            <xsl:choose>
              <xsl:when test="$power = 1">
                <xsl:value-of select="0"/>
              </xsl:when>
            </xsl:choose>
          </xsl:eek:therwise>
        </xsl:choose>
    </xsl:template>
     
    <xsl:template name="raiseToPower">
        <xsl:param name="number"/>
        <xsl:param name="power"/>
        <xsl:call-template name="raiseToPowerIter">
          <xsl:with-param name="multiplier" select="$number"/>
          <xsl:with-param name="accumulator" select="1"/>
          <xsl:with-param name="reps" select="$power"/>
        </xsl:call-template>
    </xsl:template>
     
    <xsl:template name="raiseToPowerIter">
        <xsl:param name="multiplier"/>
        <xsl:param name="accumulator"/>
        <xsl:param name="reps"/>
        <xsl:choose>
          <xsl:when test="$reps &amp;gt; 0">
            <xsl:call-template name="raiseToPowerIter">
              <xsl:with-param name="multiplier" select="$multiplier"/>
              <xsl:with-param name="accumulator"
         select="$accumulator * $multiplier"/>
              <xsl:with-param name="reps" select="$reps - 1"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:eek:therwise>
            <xsl:value-of select="$accumulator"/>
          </xsl:eek:therwise>
        </xsl:choose>
    </xsl:template>
     
    <xsl:template name="hexToDec">
        <xsl:param name="hexVal"/>
        <xsl:param name="decVal"/>
        <xsl:variable name="hexLength" select="string-length($hexVal)"/>
        <xsl:choose>
          <xsl:when test="$hexLength &amp;gt; 0">
            <xsl:variable name="hexPos">
              <xsl:call-template name="singleHexToDec">
                <xsl:with-param name="hex" select="substring($hexVal,1,1)"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:variable name="addToDec">
              <xsl:call-template name="raiseToPower">
                <xsl:with-param name="number" select="16"/>
                <xsl:with-param name="power" select="$hexLength - 1"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:call-template name="hexToDec">
              <xsl:with-param name="hexVal" select="substring($hexVal,2)"/>
              <xsl:with-param name="decVal"
         select="$decVal + ($addToDec * $hexPos)"/>
            </xsl:call-template>
          </xsl:when>
          <xsl:eek:therwise>
            <xsl:value-of select="$decVal"/>
          </xsl:eek:therwise>
        </xsl:choose>
    </xsl:template>
    
    <!-- HEXADECIMAL MATH LIBRARY : END -->
    
    </xsl:stylesheet>
    
    Change History

    Tue 1 Mar 2005 - initial version
    Wed 2 Mar 2005 - fixed escaping of > and < operators