<!--XSLT Processor: Saxonica--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><!--DO NOT EDIT: GENERATED BY merge-function-specs.xsl--><spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" id="spec-top" w3c-doctype="rec" status="ext-review"><header><title>EXPath Binary Module 4.0</title><version/><w3c-designation>REC-binary-40</w3c-designation><w3c-doctype>W3C Editor's Draft</w3c-doctype><pubdate><day>01</day><month>January</month><year>2000</year></pubdate><publoc>
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/binary-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/binary-40/</loc>
        </publoc><altlocs><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="binary-40.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Specification in XML format using HTML5 vocabulary</loc><loc xmlns:xlink="http://www.w3.org/1999/xlink" href="function-catalog.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML function catalog</loc></altlocs><latestloc doc="EXPath Binary Module 4.0">
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/binary-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/binary-40/</loc>
        </latestloc><prevrec doc="EXPath Binary Module">
            <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://expath.org/spec/binary" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://expath.org/spec/binary</loc>
            
        </prevrec><authlist><author role="4.0"><name>Jirka Kosek</name><email xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.saxonica.com/" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">http://kosek.com/</email></author><author role="4.0"><name>John Lumley</name><email xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.saxonica.com/" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">http://www.saxonica.com/</email></author></authlist><errataloc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://www.w3.org/XML/2017/qt-errata/xpath-functions-31-errata.html" xlink:type="simple"/><translationloc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://www.w3.org/2003/03/Translations/byTechnology?technology=xpath-functions-40" xlink:type="simple"/><abstract><p>This document defines an API for XPath 4.0 to handle the manipulation of binary data.
                It defines extension functions to process data from, and generate date for, binary
                resources, including extracting subparts, searching, basic binary operations and
                conversion between binary and structured forms of XDM numbers and strings.</p><p>The document is an update of the original <bibref ref="expath-bin"/> specification,
                developed by the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/community/expath/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">EXPath Community
                    Group</loc>, defined for <bibref ref="xpath20"/> and published in 2013.</p><p>The principal semantic alteration is use of functional argument defaults available in
                XPath 4.0.</p><p>These functions are defined for use in <bibref ref="xpath-40"/> and <bibref ref="xquery-40"/> and <bibref ref="xslt-40"/> and other related XML standards.
                The signatures and summaries of functions defined in this document are available at:
                    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://qt4cg.org/specifications/EXPath/binary-40/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://qt4cg.org/specifications/EXPath/binary-40/</loc>.</p><p>A summary of changes since published version 1.0 (the XPath 2.0 version) is provided
                at <specref ref="changelog"/>.</p></abstract><status><p>This version of the specification is work in progress. It is produced by the QT4
                Working Group, officially the W3C XSLT 4.0 Extensions Community Group. Individual
                functions specified in the document may be at different stages of review, reflected
                in their <term>History</term> notes. Comments are invited, in the form of GitHub
                issues at <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="https://github.com/qt4cg/qtspecs" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">https://github.com/qt4cg/qtspecs</loc>.</p><note role="dedication" id="dedication"><p>The publications of this community group 
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="../xquery-40/xpath-40.html#dedication" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">are dedicated</loc> to our co-chair,
Michael Sperberg-McQueen (1954–2024).</p></note></status><langusage><language id="EN">English</language></langusage><revisiondesc><p/></revisiondesc></header><body><div1 id="intro"><head>Introduction</head><changes><change>Use the arrows to browse significant changes since the 1.0 version of this
                    specification.</change><change>Sections with significant changes are marked Δ in the table of contents. New
                    functions introduced in this version are marked ➕ in the table of
                    contents.</change></changes><p>The purpose of this document is to define functions to manipulate binary data for
                inclusion in XPath 4.0, XQuery 4.0, and XSLT 4.0. The binary data is represented by
                the type <code nobreak="false">xs:base64Binary</code> as defined by <xspecref spec="XS2" ref="base64Binary"/>.</p><p>The exact syntax used to call these functions and operators is specified in <bibref ref="xpath-40"/>, <bibref ref="xquery-40"/> and <bibref ref="xslt-40"/>. </p><p>This document defines several classes of functions:</p><ulist><item><p>Functions to create binary 'constants' and convert between the binary forms
                        and sequences of octets. </p></item><item><p>Functions to perform basic operations on binary values, such as joining,
                        selecting and searching.</p></item><item><p>Functions to perform bitwise operations.</p></item><item><p>Functions to pack or unpack numeric values from within or into binary
                        data.</p></item><item><p>Functions to decode or encode strings from within or into binary data&gt;.</p></item></ulist><p>References to specific sections of some other specifications are indicated by
                cross-document links in this document. Each such link consists of a pointer to a
                specific section followed a superscript specifying the linked document. The
                superscripts have the following meanings: FILE40 <bibref ref="expath-file-40"/>,
                FO40 <bibref ref="xpath-functions-40"/> and XS2 <bibref ref="xmlschema-2"/></p><div2 id="error.management"><head>Error management</head><p>Error conditions are identified by a code (a <code nobreak="false">QName</code>.) When such an
                    error condition is reached in the evaluation of an expression, a dynamic error
                    is thrown, with the corresponding error code (as if the standard XPath function
                    <code nobreak="false">error()</code> had been called.)</p><p>In this specification these codes use the
                    <code nobreak="false">http://expath.org/ns/binary</code> namespace and a 'descriptive string'
                    local part, e.g. <code nobreak="false">bin:index-out-of-range</code>, rather than the
                    <code nobreak="false">http://www.w3.org/2005/xqt-errors</code> namespace and alpha-numeric local
                    part, e.g. <code nobreak="false">err:FOCH0004</code>, used in <bibref ref="xpath-functions-40"/>. These error codes were chosen originally in the 1.0 version of 2013.</p></div2><div2 id="type"><head>Binary type</head><changes><change>Binary 'data' arguments to the functions are now declared to be either
                        <code nobreak="false">xs:hexBinary</code> or <code nobreak="false">xs:base64Binary</code>, but binary
                        function results remain of type <code nobreak="false">xs:base64Binary</code>. This should
                        not cause any backward incompatibilities as casting back and forth between
                        the two representations has been possible since at least version
                        2.0</change></changes><p>The principal binary type within this module is <code nobreak="false">xs:base64Binary</code> as
                    defined by <xspecref spec="XS2" ref="base64Binary"/>.</p><p>In general, for the functions defined in this specification, if the result return
                    is binary data, it will <emph>always</emph> be of type
                    <code nobreak="false">xs:base64Binary</code>. Any arguments to a function which are considered
                    to be binary data can be either of type <code nobreak="false">xs:base64Binary</code> or
                    <code nobreak="false">xs:hexBinary</code>. </p><p>Conversion to and from <code nobreak="false">xs:hexBinary</code> can be performed by casting with
                    <code nobreak="false">xs:hexBinary()</code> and <code nobreak="false">xs:base64Binary()</code>.</p><note><p>As these types are normally implemented as wrappers around byte array
                        structures containing the data, and differ only when being serialized to or
                        parsed from text, such casting in-process should not involve data
                        copying.</p></note><p>An item of type <code nobreak="false">xs:base64Binary</code> can be <emph>empty</emph>, i.e.
                    contain no data, (in the same way that items of type <code nobreak="false">xs:string</code> can
                    contain no characters.) Where 'data' arguments to functions that return binary
                    data are optional (i.e. <code nobreak="false"><emph>$arg as type</emph>?</code>) and any of
                    those optional arguments is set to the empty sequence, in general an empty
                    sequence is returned, rather than an empty item of type
                    <code nobreak="false">xs:base64Binary</code>. </p></div2><div2 id="testing"><head>Test suite</head><p>A suite of test-cases for all the functions defined in this module, in <bibref ref="qt3"/> format, is defined at <bibref ref="tests.binary"/>.</p></div2><div2 id="conformance"><head>Conformance</head><p>This specification follows the general remarks on and terminology for conformance
                    given in <xspecref spec="FO40" ref="conformance"/></p><p>In this document, text labeled as an example or as a note is provided for
                    explanatory purposes and is not normative.</p></div2><div2 id="namespace-prefixes"><head>Namespaces and prefixes</head><p>The functions defined in this document are contained exclusively in the namespace
                    <code nobreak="false">http://expath.org/ns/binary</code> and referenced using a
                    <code nobreak="false">xs:QName</code> binding to that namespace.</p><p>This document uses the prefix <code nobreak="false">bin</code> to refer to this namespace.
                    User-written applications can choose a different prefix to refer to the
                    namespace, so long as it is bound to the correct URI. In accordance with current
                    practice, it is recommended that the prefix <code nobreak="false">bin</code> be reserved for use
                    with this namespace.</p></div2><div2 id="func-signatures"><head>Function signatures and descriptions</head><p>Each function (or group of functions having the same name) is defined in this
                    specification using a standard proforma, full details of which can be found in
                        <xspecref spec="FO40" ref="func-signatures"/>. In particular in this update
                    (trailing) optional arguments for functions (introduced in XPath 4.0) are used
                    where appropriate in the signatures, rather than multiple-arity signatures as
                    previously. </p></div2></div1><div1 id="use-cases"><head>Use cases</head><p>Development of this specification was driven by requirements which some XML
                developers regularly encounter in examining or generating data which is presented in
                binary, or other non-textual forms. Some typical use cases include:</p><ulist><item><p>Getting the dimensions of an image file.</p></item><item><p>Extracting image metadata.</p></item><item><p>Processing images embedded as base64 encodings within a SOAP message.</p></item><item><p>Processing legacy text files which use different encodings in separate
                        sections.</p></item><item><p>Generating PDF files from SVG graphical data.</p></item></ulist><div2 id="example-JPEG"><head>Example – finding JPEG size</head><p>As an example, the following code reads the binary form of a JPEG image file,
                    searches for the 'Start of Frame/DCT' segment, and unpacks the relevant binary
                    sections to integers of height and width:</p><example><eg xml:space="preserve">
&lt;xsl:variable name="binary" select="file:read-binary(@href)" as="xs:base64Binary"/&gt;
&lt;xsl:variable name="location" select="bin:find($binary,0,bin:hex('FFC0'))"/&gt;
&lt;size width="{bin:unpack-unsigned-integer($binary,$location+5,2,'most-significant-first')}"
      height="{bin:unpack-unsigned-integer($binary,$location+7,2,'most-significant-first')}"/&gt;
               
      → &lt;size width="377" height="327"/&gt;</eg></example><p>(The <code nobreak="false">'most-significant-first'</code> argument ensures the numeric
                    conversion is 'big-endian', which is the format in JPEG.)</p></div2><div2 id="example-ASN1"><head>Example – reading and writing variable length ASN.1 integers</head><changes><change>The functions in this example have been moved into a differing namespace
                        prefix (<code nobreak="false">asn:</code>) to avoid suggesting that they are part of the
                        supported function set.</change></changes><p><bibref ref="asn1"/> defines several formats for identifying and encoding
                    arbitrary-sized telecommunications data as streams of octets. Many of these
                    forms specify the length of data as part of their encoding. For example, in the
                    Basic Encoding Rules, an integer is represented as the following series of
                    octets:</p><ulist><item><p>Type – 1 octet – in this case the value <code nobreak="false">0x02</code></p></item><item><p>Length – ≥ 1 octet – the number of octets in the integer value. The
                            length field itself can be variable in length – to accommodate VERY
                            large integers (requiring more than 127 octets to represent, e.g.
                            2048-bit crypto keys.)</p></item><item><p>Payload – ≥ 0 octets – the octets of the integer value in
                            most-significant-first order.</p></item></ulist><example><p>To generate such a representation for an integer from XSLT/XPath, the
                        following code might be used:</p><eg xml:space="preserve">
 &lt;xsl:function name="asn:int-octets" as="xs:integer*"&gt;
    &lt;xsl:param name="value" as="xs:integer"/&gt;
    &lt;xsl:sequence
            select="if($value ne 0) then (bin:int-octets($value idiv 256),$value mod 256) else ()"/&gt;
 &lt;/xsl:function&gt;
 &lt;xsl:function name="asn:encode-ASN-integer" as="xs:base64Binary"&gt;
     &lt;xsl:param name="int" as="xs:integer"/&gt;
     &lt;xsl:variable name="octets" select="bin:int-octets($int)"/&gt;
     &lt;xsl:variable name="length-octets"
         select="let $l := count($octets) return
         (if($l le 127) then $l 
         else (let $lo := bin:int-octets($l) return (128+count($lo),$lo)))"/&gt;
     &lt;xsl:sequence select="bin:from-octets((2,$length-octets,$octets))"/&gt;
 &lt;/xsl:function&gt;</eg><p>The function <code nobreak="false">asn:int-octets</code> returns a sequence of all the
                        'significant' octets of the integer (i.e. eliminating leading 'zeroes') in
                        most-significant order. Examples of the encoding are: </p><eg xml:space="preserve">
 asn:encode-ASN-integer(0) → "AgA="
 asn:encode-ASN-integer(1234) → "AgIE0g=="
 asn:encode-ASN-integer(123456789123456789123456789123456789) → "Ag8XxuPAMviQRa10ZoQEXxU="
               
 asn:encode-ASN-integer(123456789.. 900 digits... 123456789) → "AoIBdgaTo....EBF8V"</eg><p>The first example requires no octets to encode zero, hence its octets are
                        <code nobreak="false">2,0</code>. Both the second and third examples can be represented in
                        less than 128 octets (2 and 15 respectively), so length is encoded as a
                        single octet. The first three octets of the result for the last example,
                        which encodes a 900-digit integer, are: <code nobreak="false">2,130,1</code> indicating that
                        the data is represented by (130-128) * 256 + 1 = 513 octets and the length
                        required two octets to encode.</p><p>Decoding is a matter of compound use of the integer decoding function:</p><eg xml:space="preserve">
 &lt;xsl:function name="asn:decode-ASN-integer" as="xs:integer"&gt;
     &lt;xsl:param name="in" as="xs:base64Binary"/&gt;
     &lt;xsl:sequence
         select="let $lo := bin:unpack-unsigned-integer($in,1,1,'BE') return (
         if($lo le 127) then bin:unpack-unsigned-integer($in,2,$lo,'BE') 
            else (let $lo2 := $lo - 128, $lo3 := bin:unpack-unsigned-integer($in,2,$lo2,'BE') return
            bin:unpack-unsigned-integer($in,2+$lo2,$lo3,'BE')))"
      /&gt;
 &lt;/xsl:function&gt;               </eg><p>(all numbers in ASN are 'big-endian') and the examples from above
                        reverse:</p><eg xml:space="preserve">
 asn:decode-ASN-integer(xs:base64Binary("AgA=")) → 0
 asn:decode-ASN-integer(xs:base64Binary("AgIE0g==")) → 1234
 asn:encode-ASN-integer(xs:base64Binary("Ag8XxuPAMviQRa10ZoQEXxU=")) 
     → 123456789123456789123456789123456789              
 asn:encode-ASN-integer(xs:base64Binary("AoIBdgaTo....EBF8V")) 
     → 123456789.. 900 digits... 123456789                </eg></example></div2></div1><div1 id="loading"><head>Loading and saving binary data</head><changes><change>The <bibref ref="xpath-functions-40"/> function
                    <code nobreak="false">fn:binary-resource</code> has been added to the list of useful
                    functions.</change></changes><p>This module defines no specific functions for reading and writing binary data from
                resources, but other specifications provide some suitable mechanisms.</p><p><bibref ref="xpath-functions-40"/> provides a function to retrieve binary
                resources:</p><example role="signature"><proto role="example" name="fn:binary-resource" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="filesource" type="xs:string?"/></proto></example><p>The EXPath File Module <bibref ref="expath-file-40"/> provides three functions
                suitable for use in file-based situations:</p><example role="signature"><proto role="example" name="file:read-binary" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="file" type="xs:string"/><arg name="offset" type="xs:integer?" default="0"/><arg name="size" type="xs:integer?" default="()"/></proto></example><p>which reads binary data from an existing file, with an optional offset and size.</p><example role="signature"><proto role="example" name="file:write-binary" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:base64Binary"/></proto></example><p>which writes binary data into a new or existing file.</p><example role="signature"><proto role="example" name="file:append-binary" return-type="empty-sequence()" returnEmptyOk="no" returnSeq="no" prefix="fn" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no" isOp="no"><arg name="file" type="xs:string"/><arg name="value" type="xs:base64Binary"/></proto></example><p>which appends binary data onto the end of an extant file.</p></div1><div1 id="constants"><head>Defining 'constants' and conversions</head><p>Users of the package may need to define binary 'constants' within their code or
                examine the basic octets. The following functions support these:</p><div2 id="func-bin-hex"><head>bin:hex</head><glist><gitem><label>Summary</label><def><p>Returns the binary form of the set of octets written as a sequence of (ASCII) hex
                digits ([0-9A-Fa-f]).</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="hex" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:string?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>
                <code nobreak="false">$in</code> will be effectively zero-padded from the left to generate an
                integral number of octets, i.e. an even number of hexadecimal digits.</p><p>Byte order in the result follows (per-octet) character order in the string.</p><p> If <code nobreak="false">$in</code> is an empty string, then the result will be a
                <code nobreak="false">xs:base64Binary</code> with no embedded data.</p><p>If <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="non-numeric-character"/> is raised if <code nobreak="false">$in</code>
                cannot be parsed as a hexadecimal number.</p></def></gitem><gitem><label>Notes</label><def><p role="note">When the input string has an even number of characters, this function behaves
                similarly to the double cast
                <code nobreak="false">xs:base64Binary(xs:hexBinary(<emph>$string</emph>))</code>.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:hex('11223F4E') → "ESI/Tg=="</eg></td></tr><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:hex('1223F4E') → "ASI/Tg=="</eg></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-bin-binary"><head>bin:bin</head><glist><gitem><label>Summary</label><def><p>Returns the binary form of the set of octets written as a sequence of (8-wise)
                (ASCII) binary digits ([01]).</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="bin" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:string?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>
                <code nobreak="false">$in</code> will be effectively zero-padded from the left to generate an
                integral number of octets (i.e. the number of characters in <code nobreak="false">$in</code> will be
                a multiple of 8).</p><p>Byte order in the result follows (per-octet) character order in the string.</p><p> If <code nobreak="false">$in</code> is an empty string, then the result will be a
                <code nobreak="false">xs:base64Binary</code> with no embedded data.</p><p>If <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="non-numeric-character"/> is raised if <code nobreak="false">$in</code>
                cannot be parsed as a binary number.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:bin('1101000111010101') → "0dU="</eg></td></tr><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:bin('1000111010101') → "EdU="</eg></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-bin-octal"><head>bin:octal</head><glist><gitem><label>Summary</label><def><p>Returns the binary form of the set of octets written as a sequence of (ASCII) octal
                digits ([0-7]).</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="octal" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:string?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>
                <code nobreak="false">$in</code> will be effectively zero-padded from the left to generate an
                integral number of octets.</p><p>Byte order in the result follows (per-octet) character order in the string.</p><p> If <code nobreak="false">$in</code> is an empty string, then the result will be a
                <code nobreak="false">xs:base64Binary</code> with no embedded data.</p><p>If <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="non-numeric-character"/> is raised if <code nobreak="false">$in</code>
                cannot be parsed as an octal number.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:octal('11223047') → "JSYn"</eg></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-bin-to-octets"><head>bin:to-octets</head><glist><gitem><label>Summary</label><def><p>Returns binary data as a sequence of integer octets.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="to-octets" return-type="xs:integer*" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>If <code nobreak="false">$in</code> is a zero length binary data then the empty sequence is
                returned.</p><p>Octets are returned as integers from 0 to 255.</p></def></gitem></glist></div2><div2 id="func-bin-from-octets"><head>bin:from-octets</head><glist><gitem><label>Summary</label><def><p>Converts a sequence of octets into binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="from-octets" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:integer*"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Octets are integers from 0 to 255.</p><p>If <code nobreak="false">$in</code> is the empty sequence, the function returns zero-sized binary
                data.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="octet-out-of-range"/> is raised if one of the octets lies
                outside the range 0 – 255. </p></def></gitem></glist></div2></div1><div1 id="basic-operations"><head>Basic operations</head><changes><change>The function <code nobreak="false">find-all</code> in the example for <code nobreak="false">bin:find</code>
                    has been moved into a differing namespace prefix (<code nobreak="false">f:</code>) to avoid
                    suggesting that it is part of the supported function set.</change></changes><div2 id="func-bin-length"><head>bin:length</head><glist><gitem><label>Summary</label><def><p>Returns the size of binary data, measured in octets.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="length" return-type="xs:integer" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns the size of binary data in octets.</p></def></gitem><gitem><label>Formal Equivalent</label><def><p>The effect of the function is equivalent to the result of the following XPath expression.</p><eg xml:space="preserve">count(bin:to-octets($in))</eg></def></gitem></glist></div2><div2 id="func-bin-part"><head>bin:part</head><glist><gitem><label>Summary</label><def><p>Returns a specified part of binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="part" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="offset" type="xs:integer"/><arg name="size" type="xs:integer?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns a section of binary data starting at the <code nobreak="false">$offset</code> octet. If
                <code nobreak="false">$size</code> is defined, the size of the returned binary data is
                <code nobreak="false">$size</code> octets. If <code nobreak="false">$size</code> is absent, all remaining data from
                <code nobreak="false">$offset</code> is returned.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>The values of <code nobreak="false">$offset</code> and <code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be non-negative integers.</p><p>It is a dynamic error if <code nobreak="false">$offset</code> + <code nobreak="false">$size</code> is larger than the
                size of the binary data in <code nobreak="false">$in</code>.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + $size</code> is larger than the size of the binary data
                of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p></def></gitem><gitem><label>Notes</label><def><p role="note">Note that <code nobreak="false">fn:subsequence()</code> and <code nobreak="false">fn:substring()</code>
                <!--<bibref ref="fo11"/>--> both use <code nobreak="false">xs:double</code> for offset and size –
                this is a legacy from XPath 1.0.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr><td colspan="2" rowspan="1"><p>Testing whether <code nobreak="false">$data</code> variable starts with binary content consistent
                    with a PDF file:</p></td></tr><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:part($data, 0, 4) eq bin:hex("25504446")</eg></td></tr><tr><td colspan="2" rowspan="1"><p><code nobreak="false">25504446</code> is the magic number for PDF files: it is the US-ASCII
                    encoded hexadecimal value for <code nobreak="false">%PDF</code>.
                    <code nobreak="false">bin:encode-string</code><!--<specref ref="encode-string"/>--> can be used
                    to convert a string to its binary representation.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-bin-join"><head>bin:join</head><glist><gitem><label>Summary</label><def><p>Returns the binary data created by concatenating the binary data items in a
                sequence.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="join" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)*"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>The function returns an <code nobreak="false">xs:base64Binary</code> created by concatenating the
                items in the sequence <code nobreak="false">$in</code>, in order.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns a binary
                item containing no data bytes.</p></def></gitem><gitem><label>Formal Equivalent</label><def><p>The effect of the function is equivalent to the result of the following XPath expression.</p><eg xml:space="preserve">bin:from-octets($in !
            bin:to-octets(.))</eg></def></gitem></glist></div2><div2 id="func-bin-insert-before"><head>bin:insert-before</head><glist><gitem><label>Summary</label><def><p>Inserts additional binary data at a given point in other binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="insert-before" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="offset" type="xs:integer"/><arg name="extra" type="xs:base64Binary?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns binary data consisting sequentially of the data from <code nobreak="false">$in</code> upto
                and including the <code nobreak="false">$offset - 1</code> octet, followed by all the data from
                <code nobreak="false">$extra</code>, and then the remaining data from <code nobreak="false">$in</code>.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>The value of <code nobreak="false">$offset</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p><p>If the value of <code nobreak="false">$extra</code> is the empty sequence, the function returns
                <code nobreak="false">$in</code>.</p><p>If <code nobreak="false">$offset eq 0</code> the result is the binary concatenation of
                <code nobreak="false">$extra</code> and <code nobreak="false">$in</code>, i.e. equivalent to
                <code nobreak="false">bin:join(($extra,$in))</code>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset</code> is larger than the size of the binary data of
                <code nobreak="false">$in</code>.</p></def></gitem><gitem><label>Notes</label><def><p role="note">Note that when <code nobreak="false">$offset gt 0 and $offset lt bin:size($in)</code> the function is
                equivalent to:</p><eg role="note" xml:space="preserve">bin:join((bin:part($in,0,$offset - 1),$extra,bin:part($in,$offset)))</eg></def></gitem></glist></div2><div2 id="func-bin-pad-left"><head>bin:pad-left</head><glist><gitem><label>Summary</label><def><p>Returns the binary data created by padding <code nobreak="false">$in</code> with <code nobreak="false">$size</code>
                octets from the left. The padding octet values are <code nobreak="false">$octet</code> or zero if
                omitted.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="pad-left" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="size" type="xs:integer"/><arg name="octet" type="xs:integer?" default="0"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>The function returns an <code nobreak="false">xs:base64Binary</code> created by padding the input
                with <code nobreak="false">$size</code> octets <emph>in front of</emph> the input. If
                <code nobreak="false">$octet</code> is specified, the padding octets each have that value, otherwise
                they are initialized to 0.</p><p><code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Formal Equivalent</label><def><p>The effect of the function is equivalent to the result of the following XPath expression, except in error cases.</p><eg xml:space="preserve">bin:join((bin:from-octets((1 to $size) ! $octet), $in))</eg></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p><p><errorref spec="BIN40" code="octet-out-of-range"/> is raised if <code nobreak="false">$octet</code>
                lies outside the range 0 – 255. </p></def></gitem></glist></div2><div2 id="func-bin-pad-right"><head>bin:pad-right</head><glist><gitem><label>Summary</label><def><p>Returns the binary data created by padding <code nobreak="false">$in</code> with <code nobreak="false">$size</code>
                blank octets from the right. The padding octet values are <code nobreak="false">$octet</code> or
                zero if omitted.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="pad-right" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="size" type="xs:integer"/><arg name="octet" type="xs:integer?" default="0"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>The function returns an <code nobreak="false">xs:base64Binary</code> created by padding the input
                with <code nobreak="false">$size</code> blank octets <emph>after</emph> the input. If
                <code nobreak="false">$octet</code> is specified, the padding octets each have that value, otherwise
                they are initialized to 0.</p><p><code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Formal Equivalent</label><def><p>The effect of the function is equivalent to the result of the following XPath expression, except in error cases.</p><eg xml:space="preserve">bin:join((bin:from-octets($in,(1 to $size) ! $octet)))</eg></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p><p><errorref spec="BIN40" code="octet-out-of-range"/> is raised if <code nobreak="false">$octet</code>
                lies outside the range 0 – 255. </p></def></gitem></glist></div2><div2 id="func-bin-find"><head>bin:find</head><glist><gitem><label>Summary</label><def><p>Returns the first location in <code nobreak="false">$in</code> of <code nobreak="false">$search</code>, starting at
                the <code nobreak="false">$offset</code> octet.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="find" return-type="xs:integer?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="offset" type="xs:integer"/><arg name="search" type="(xs:hexBinary | xs:base64Binary)"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>The function returns the first location of the binary search sequence in the input,
                or if not found, the empty sequence.</p><p>If <code nobreak="false">$search</code> is empty <code nobreak="false">$offset</code> is returned.</p><p>The value of <code nobreak="false">$offset</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>The returned location is zero based.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset</code> is larger than the size of the binary data of
                <code nobreak="false">$in</code>.</p></def></gitem><gitem><label>Notes</label><def><p role="note">Finding all the matches can be accomplished with simple recursive application:</p><eg role="note" xml:space="preserve">
&lt;xsl:function name="f:find-all" as="xs:integer*"&gt;
     &lt;xsl:param name="data" as="xs:base64Binary?"/&gt;
     &lt;xsl:param name="offset" as="xs:integer"/&gt;
     &lt;xsl:param name="pattern" as="xs:base64Binary"/&gt;
     &lt;xsl:sequence
         select="if(bin:length($pattern) = 0) then ()
         else let $found := bin:find($data,$offset,$pattern) return
         if($found) then ($found,
             if($found + 1 lt bin:length($data)) then f:find-all($data,$found + 1,$pattern) else ())
             else ()"/&gt;
&lt;/xsl:function&gt;</eg></def></gitem></glist></div2></div1><div1 id="text-encoding"><head>Text decoding and encoding</head><div2 id="func-bin-decode-string"><head>bin:decode-string</head><glist><gitem><label>Summary</label><def><p>Decodes binary data as a string in a given encoding.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="decode-string" return-type="xs:string?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="encoding" type="xs:string?" default="'utf-8'"/><arg name="offset" type="xs:integer?" default="0"/><arg name="size" type="xs:integer?" default="()"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>If <code nobreak="false">$offset</code> and <code nobreak="false">$size</code> are provided, the <code nobreak="false">$size</code>
                octets from <code nobreak="false">$offset</code> are decoded. If <code nobreak="false">$offset</code> alone is
                provided, octets from <code nobreak="false">$offset</code> to the end are decoded, otherwise the
                entire octet sequence is used.</p><p>The <code nobreak="false">$encoding</code> argument is the name of an encoding. The values for this
                attribute follow the same rules as for the <code nobreak="false">encoding</code> attribute in an XML
                declaration. The only values which every implementation is
                    <rfc2119>required</rfc2119> to recognize are <code nobreak="false">utf-8</code> and
                <code nobreak="false">utf-16</code>.</p><p>If <code nobreak="false">$encoding</code> is omitted, <code nobreak="false">utf-8</code> encoding is assumed.</p><p>The values of <code nobreak="false">$offset</code> and <code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be non-negative integers.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p><p><code nobreak="false">$offset</code> is zero based.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/>is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + $size</code> is larger than the size of the binary data
                of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p><p><errorref spec="BIN40" code="unknown-encoding"/> is raised if <code nobreak="false">$encoding</code> is
                invalid or not supported by the implementation.</p><p><errorref spec="BIN40" code="conversion-error"/> is raised if there is an error or
                malformed input during decoding the string. Additional information about the error
                may be passed through suitable error reporting mechanisms – this is
                implementation-dependant.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr><td colspan="2" rowspan="1"><p>Testing whether <code nobreak="false">$data</code> variable starts with binary content consistent
                    with a PDF file:</p></td></tr><tr><td colspan="2" rowspan="1"><eg xml:space="preserve">bin:decode-string($data, 'UTF-8', 0, 4) eq '%PDF'</eg></td></tr><tr><td colspan="2" rowspan="1"><p>The first four characters of a PDF file are <code nobreak="false">'%PDF'</code>.</p></td></tr></tbody></table></def></gitem></glist></div2><div2 id="func-bin-encode-string"><head>bin:encode-string</head><glist><gitem><label>Summary</label><def><p>Encodes a string into binary data using a given encoding.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="encode-string" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:string?"/><arg name="encoding" type="xs:string?" default="'utf-8'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>The <code nobreak="false">$encoding</code> argument is the name of an encoding. The values for this
                attribute follow the same rules as for the <code nobreak="false">encoding</code> attribute in an XML
                declaration. The only values which every implementation is
                    <rfc2119>required</rfc2119> to recognize are <code nobreak="false">utf-8</code> and
                <code nobreak="false">utf-16</code>.</p><p>If <code nobreak="false">$encoding</code> is omitted, <code nobreak="false">utf-8</code> encoding is assumed.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="unknown-encoding"/> is raised if <code nobreak="false">$encoding</code> is
                invalid or not supported by the implementation.</p><p><errorref spec="BIN40" code="conversion-error"/>is raised if there is an error or
                malformed input during encoding the string. Additional information about the error
                may be passed through suitable error reporting mechanisms – this is
                implementation-dependant.</p></def></gitem></glist></div2></div1><div1 id="numeric-packing"><head>Packing and unpacking of encoded numeric values</head><div2 id="number-rep"><head>Numeric representation</head><div3 id="endianness"><head>Number 'endianness'</head><p>Packing and unpacking numeric values within binary data can be performed in
                        'most-significant-first' ('big-endian') or 'least-significant-first'
                        ('little-endian') octet order. The default is
                            <emph>'most-significant-first'</emph>. The relevant functions have an
                        optional parameter <code nobreak="false">$octet-order</code> whose string value controls the
                        order. Least-significant-first order is indicated by any of the values
                        <code nobreak="false">least-significant-first</code>, <code nobreak="false">little-endian</code> or
                        <code nobreak="false">LE</code>. Most-significant-first order is indicated by any of the
                        values <code nobreak="false">most-significant-first</code>, <code nobreak="false">big-endian</code> or
                        <code nobreak="false">BE</code>.</p></div3><div3 id="integer"><head>Integer representation</head><p/><p>Integers within binary data are represented, or assumed to be represented, as
                        an integral number of octets. Integers where <code nobreak="false">$length</code> is greater
                        than 8 octets (and thus not representable as a <code nobreak="false">long</code>) might be
                        expected in some situations, e.g. encryption. Whether the range of integers
                        is limited to <code nobreak="false">±2^63</code> is <xtermref spec="FO40" ref="implementation-dependent">implementation-dependent</xtermref>.</p></div3><div3 id="floating"><head>Representation of floating point numbers</head><p>Care should be taken with the packing and unpacking of floating point numbers
                        (<code nobreak="false">xs:float</code> and <code nobreak="false">xs:double</code>). The binary
                        representations are expected to correspond with those of the IEEE
                        single/double-precision 32/64-bit floating point types <bibref ref="ieee754"/>. Consequently they will occupy 4 or 8 octets when packed.</p><p>Positive and negative infinities are supported. <code nobreak="false">INF</code> maps to
                        <code nobreak="false">0x7f80
            0000</code> (float), <code nobreak="false">0x7ff0 0000 0000 0000</code>
                        (double). <code nobreak="false">-INF</code> maps to <code nobreak="false">0xff80 0000</code> (float),
                        <code nobreak="false">0xfff0 0000 0000 0000</code> (double).</p><p>Negative zero (<code nobreak="false">0x8000 0000 0000 0000</code> double,
                        <code nobreak="false">0x8000 0000</code> float) encountered during unpacking will yield
                        negative zero forms (e.g. <code nobreak="false">-xs:double(0.0)</code>) and negative zeros
                        will be written as a result of packing.</p><p><bibref ref="xmlschema-2"/> provides only one form of <code nobreak="false">NaN</code> which
                        corresponds to a 'quiet' <code nobreak="false">NaN</code> with zero payload of <bibref ref="ieee754"/> with forms <code nobreak="false">0x7fc0 0000</code> (float),
                        <code nobreak="false">0x7ff8 0000 0000 0000</code> (double). These are the bit forms that
                        will be packed.</p><p> 'Signalling' <code nobreak="false">NaN</code> values (<code nobreak="false">0x7f80 0001</code> →
                        <code nobreak="false">0x7fbf ffff</code> or <code nobreak="false">0xff80 0001</code> →
                        <code nobreak="false">0xffbf ffff</code>, <code nobreak="false">0x7ff0 0000 0000
            0001</code> → <code nobreak="false">0x7ff7 ffff ffff ffff</code> or
                        <code nobreak="false">0xfff0 0000 0000
            0001</code> → <code nobreak="false">0xfff7 ffff ffff ffff</code>) encountered
                        during unpacking will be replaced by 'quiet' <code nobreak="false">NaN</code>. Any low-order
                        payload in a unpacked 'quiet' <code nobreak="false">NaN</code> is also zeroed. </p></div3></div2><div2 id="func-bin-pack-double"><head>bin:pack-double</head><glist><gitem><label>Summary</label><def><p>Returns the 8-octet binary representation of a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#double" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">double</loc> value.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="pack-double" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:double"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The binary representation will correspond with that of the IEEE double-precision
                64-bit floating point type <bibref ref="ieee754"/>. For more details see <specref ref="floating"/>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem></glist></div2><div2 id="func-bin-pack-float"><head>bin:pack-float</head><glist><gitem><label>Summary</label><def><p>Returns the 4-octet binary representation of a <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#float" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">float</loc> value.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="pack-float" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:float"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The binary representation will correspond with that of the IEEE single-precision
                32-bit floating point type <bibref ref="ieee754"/>. For more details see <specref ref="floating"/>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem></glist></div2><div2 id="func-bin-pack-integer"><head>bin:pack-integer</head><glist><gitem><label>Summary</label><def><p>Returns the <emph>twos-complement</emph> binary representation of an integer value
                treated as <code nobreak="false">$size</code> octets long. Any 'excess' high-order bits are
                discarded.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="pack-integer" return-type="xs:base64Binary" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="xs:integer"/><arg name="size" type="xs:integer"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>Specifying a <code nobreak="false">$size</code> of zero yields an empty binary data.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p></def></gitem><gitem><label>Notes</label><def><p role="note">If the integer being packed has a maximum precision of <code nobreak="false">$size</code> octets,
                then signed/unsigned versions are not necessary. If the data is considered unsigned,
                then the most significant bit of the bottom <code nobreak="false">$size</code> octets has a normal
                positive (<code nobreak="false">2^(8 *$size - 1)</code>) meaning. If it is considered to be a signed
                value, then the MSB and all the higher order, discarded bits will be '1' for a
                negative value and '0' for a positive or zero. If this function were to check the
                'sizing' of the supplied integer against the packing size, then any values of MSB
                and the discarded higher order bits other than 'all 1' or 'all 0' would constitute
                an error. <emph>This function does not perform such checking.</emph></p></def></gitem></glist></div2><div2 id="func-bin-unpack-double"><head>bin:unpack-double</head><glist><gitem><label>Summary</label><def><p>Extract <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#double" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">double</loc> value
                stored at the particular offset in binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="unpack-double" return-type="xs:double" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/><arg name="offset" type="xs:integer"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Extract the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#double" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">double</loc> value
                stored in the 8 successive octets from the <code nobreak="false">$offset</code> octet of the binary
                data of <code nobreak="false">$in</code>.</p><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The value of <code nobreak="false">$offset</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>The binary representation is expected to correspond with that of the IEEE
                double-precision 64-bit floating point type <bibref ref="ieee754"/>. For more
                details see <specref ref="floating"/>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + 8</code> (octet-length of <code nobreak="false">xs:double</code>) is
                larger than the size of the binary data of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem></glist></div2><div2 id="func-bin-unpack-float"><head>bin:unpack-float</head><glist><gitem><label>Summary</label><def><p>Extract <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#float" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">float</loc> value stored
                at the particular offset in binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="unpack-float" return-type="xs:float" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/><arg name="offset" type="xs:integer"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Extract the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xmlschema-2/#float" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">float</loc> value
                stored in the 4 successive octets from the <code nobreak="false">$offset</code> octet of the binary
                data of <code nobreak="false">$in</code>.</p><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The value of <code nobreak="false">$offset</code>
                <rfc2119>must</rfc2119> be a non-negative integer.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>The binary representation is expected to correspond with that of the IEEE
                single-precision 32-bit floating point type <bibref ref="ieee754"/>. For more
                details see <specref ref="floating"/>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + 4</code> (octet-length of <code nobreak="false">xs:float</code>) is
                larger than the size of the binary data of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem></glist></div2><div2 id="func-bin-unpack-integer"><head>bin:unpack-integer</head><glist><gitem><label>Summary</label><def><p>Returns a signed integer value represented by the <code nobreak="false">$size</code> octets starting
                from <code nobreak="false">$offset</code> in the input binary representation. Necessary sign
                extension is performed (i.e. the result is negative if the high order bit is
                '1').</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="unpack-integer" return-type="xs:integer" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/><arg name="offset" type="xs:integer"/><arg name="size" type="xs:integer"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The values of <code nobreak="false">$offset</code> and <code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be non-negative integers.</p><p><code nobreak="false">$offset</code> is zero based.</p><p>Specifying a <code nobreak="false">$size</code> of zero yields the integer <code nobreak="false">0</code>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + $size</code> is larger than the size of the binary data
                of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem><gitem><label>Notes</label><def><p role="note">For discussion on integer range see <specref ref="integer"/>.</p></def></gitem></glist></div2><div2 id="func-bin-unpack-unsigned-integer"><head>bin:unpack-unsigned-integer</head><glist><gitem><label>Summary</label><def><p>Returns an unsigned integer value represented by the <code nobreak="false">$size</code> octets
                starting from <code nobreak="false">$offset</code> in the input binary representation.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="unpack-unsigned-integer" return-type="xs:integer" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)"/><arg name="offset" type="xs:integer"/><arg name="size" type="xs:integer"/><arg name="octet-order" type="xs:string?" default="'most-significant-first'"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Most-significant-octet-first number representation is assumed unless the
                <code nobreak="false">$octet-order</code> parameter is specified. Acceptable values for
                <code nobreak="false">$octet-order</code> are described in <specref ref="endianness"/>.</p><p>The values of <code nobreak="false">$offset</code> and <code nobreak="false">$size</code>
                <rfc2119>must</rfc2119> be non-negative integers.</p><p>The <code nobreak="false">$offset</code> is zero based.</p><p>Specifying a <code nobreak="false">$size</code> of zero yields the integer <code nobreak="false">0</code>.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="index-out-of-range"/> is raised if <code nobreak="false">$offset</code> is
                negative or <code nobreak="false">$offset + $size</code> is larger than the size of the binary data
                of <code nobreak="false">$in</code>.</p><p><errorref spec="BIN40" code="negative-size"/> is raised if <code nobreak="false">$size</code> is
                negative.</p><p><errorref spec="BIN40" code="unknown-significance-order"/> is raised if the value
                <code nobreak="false">$octet-order</code> is unrecognized.</p></def></gitem><gitem><label>Notes</label><def><p role="note">For discussion on integer range see <specref ref="integer"/>.</p></def></gitem></glist></div2></div1><div1 id="bitwise"><head>Bitwise operations</head><div2 id="func-bin-or"><head>bin:or</head><glist><gitem><label>Summary</label><def><p>Returns the "bitwise or" of two binary arguments.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="or" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="a" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="b" type="(xs:hexBinary | xs:base64Binary)?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns "bitwise or" applied between <code nobreak="false">$a</code> and <code nobreak="false">$b</code>.</p><p>If either argument is the empty sequence, an empty sequence is returned.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="differing-length-arguments"/> is raised if the input
                arguments are of differing length.</p></def></gitem></glist></div2><div2 id="func-bin-xor"><head>bin:xor</head><glist><gitem><label>Summary</label><def><p>Returns the "bitwise xor" of two binary arguments.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="xor" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="a" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="b" type="(xs:hexBinary | xs:base64Binary)?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns "bitwise exclusive or" applied between <code nobreak="false">$a</code> and
                <code nobreak="false">$b</code>.</p><p>If either argument is the empty sequence, an empty sequence is returned.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="differing-length-arguments"/> is raised if the input
                arguments are of differing length.</p></def></gitem></glist></div2><div2 id="func-bin-and"><head>bin:and</head><glist><gitem><label>Summary</label><def><p>Returns the "bitwise and" of two binary arguments.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="and" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="a" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="b" type="(xs:hexBinary | xs:base64Binary)?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns "bitwise and" applied between <code nobreak="false">$a</code> and <code nobreak="false">$b</code>.</p><p>If either argument is the empty sequence, an empty sequence is returned.</p></def></gitem><gitem><label>Error Conditions</label><def><p><errorref spec="BIN40" code="differing-length-arguments"/> is raised if the input
                arguments are of differing length.</p></def></gitem></glist></div2><div2 id="func-bin-not"><head>bin:not</head><glist><gitem><label>Summary</label><def><p>Returns the "bitwise not" of a binary argument.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="not" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>Returns "bitwise not" applied to <code nobreak="false">$in</code>.</p><p>If the argument is the empty sequence, an empty sequence is returned.</p></def></gitem></glist></div2><div2 id="func-bin-shift"><head>bin:shift</head><glist><gitem><label>Summary</label><def><p>Shift bits in binary data.</p></def></gitem><gitem><label>Signature</label><def><example role="signature"><proto isOp="no" prefix="bin" name="shift" return-type="xs:base64Binary?" returnEmptyOk="no" returnSeq="no" returnVaries="no" isSchema="no" isDatatype="no" isSpecial="no"><arg name="in" type="(xs:hexBinary | xs:base64Binary)?"/><arg name="by" type="xs:integer"/></proto></example></def></gitem><gitem><label>Properties</label><def><p>This function is <xtermref spec="FO40" ref="dt-deterministic">deterministic</xtermref>, <xtermref spec="FO40" ref="dt-context-independent">context-independent</xtermref>,  and <xtermref spec="FO40" ref="dt-focus-independent">focus-independent</xtermref>. </p></def></gitem><gitem><label>Rules</label><def><p>If <code nobreak="false">$by</code> is positive then bits are shifted <code nobreak="false">$by</code> times to the
                left.</p><p>If <code nobreak="false">$by</code> is negative then bits are shifted <code nobreak="false">-$by</code> times to the
                right.</p><p>If <code nobreak="false">$by</code> is zero, the result is identical to <code nobreak="false">$in</code>.</p><p>If <code nobreak="false">|$by|</code> is greater than the bit-length of <code nobreak="false">$in</code> then an
                all-zeros result, of the same length as <code nobreak="false">$in</code>, is returned.</p><p><code nobreak="false">|$by|</code> can be greater than 8, implying multi-byte shifts.</p><p>The result always has the same size as <code nobreak="false">$in</code>.</p><p>The shifting is logical: zeros are placed into discarded bits.</p><p>If the value of <code nobreak="false">$in</code> is the empty sequence, the function returns an empty
                sequence.</p></def></gitem><gitem><label>Notes</label><def><p role="note">Bit shifting across byte boundaries implies 'big-endian' treatment, i.e. the leftmost
                (high-order) bit when shifted left becomes the low-order bit of the preceding
                byte.</p></def></gitem><gitem><label>Examples</label><def role="example"><table role="medium"><tbody><tr class="testdiv"><th valign="top" rowspan="1" colspan="1">Expression:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">bin:shift(bin:hex("000001"), 17)</eg></td></tr><tr><th valign="top" rowspan="1" colspan="1">Result:</th><td valign="top" rowspan="1" colspan="1"><eg xml:space="preserve">bin:hex("020000")</eg></td></tr></tbody></table></def></gitem></glist></div2></div1></body><back><div1 id="biblio"><head>References</head><div2 id="normative-biblio"><head>Normative references</head><blist><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath-bin" key="EXPath Binary 1.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        <loc href="http://expath.org/spec/binary" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Binary Module 1.0</loc>. Jirka
                        Kosek and John Lumley, editors. EXPath Module. 3 December 2013.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath-file" key="EXPath File 1.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        <loc href="http://expath.org/spec/file" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">File Module 1.0</loc>. Christian
                        Grün, Matthias Brantner and Gabriel Petrovay, editors. EXPath Module 20
                        February 2015.</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath-file-40" key="EXPath File 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ieee754" key="IEEE 754-1985" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">IEEE Standard for Binary Floating-Point
                        Arithmetic. See <loc href="http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://standards.ieee.org/reading/ieee/std_public/description/busarch/754-1985_desc.html</loc></bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="ieee754-2019" key="IEEE 754-2019" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">IEEE. <emph>IEEE Standard for
                            Floating-Point Arithmetic.</emph>
                    </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath20" key="XML Path Language (XPath) 2.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-functions-40" key="XQuery and XPath Functions and Operators 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-40" key="XML Path Language (XPath) 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        
                    </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xslt-40" key="XSL Transformations (XSLT) Version 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        <emph>CITATION: T.B.D.</emph>
                    </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xpath-datamodel-40" key="XQuery and XPath Data Model (XDM) 4.0" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xquery-40" key="XQuery 4.1: An XML Query Language" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        <emph>CITATION: T.B.D.</emph>
                    </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="xmlschema-2" key="XML Schema Part 2: Datatypes Second Edition" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"> XML
                        Schema Part 2: Datatypes Second Edition, Oct. 28 2004. Available at: <loc href="http://www.w3.org/TR/xmlschema-2/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xmlschema-2/</loc>
                    </bibl></blist></div2><div2 id="non-normative-biblio"><head>Non-normative references</head><blist><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="asn1" key="ASN.1" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"> OSI networking and system aspects – Abstract Syntax
                        Notation One (ASN.1) – see <loc href="http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">ASN.1 encoding rules: Specification of Basic Encoding Rules (BER),
                            Canonical Encoding Rules (CER) and Distinguished Encoding Rules (DER)
                        </loc>. ITU-T X.690 (07/2002) </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="tests.binary" key="Test-suite" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">The test suite for this module, using
                        QT3 format, is in the EXPath repository <loc href="http://github.com/expath/expath-cg/tree/master/tests/qt3/binary" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://github.com/expath/expath-cg</loc> in the directory
                        tests/qt3/binary/</bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="qt3" key="QT3" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">
                        <loc href="http://dev.w3.org/2011/QT3-test-suite/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query Test
                            Suite</loc>. W3C 21 June 2013. </bibl><bibl xmlns:xlink="http://www.w3.org/1999/xlink" id="expath" key="EXPath" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">EXPath: Collaboratively Defining Open Standards
                        for Portable XPath Extensions. <loc href="http://expath.org/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://expath.org/</loc>.</bibl></blist></div2></div1><div1 id="error-summary"><head>Error summary</head><p>The error text provided with these errors is non-normative.</p><error-list><error spec="BIN40" code="differing-length-arguments" label="Arguments of different length" id="error.differentLengthArguments"><p>The two arguments to a bitwise operation are of differing lengths.</p></error><error spec="BIN40" code="index-out-of-range"><p>Attempting to retrieve data outside the meaningful range of a binary data
                        type.</p></error><error spec="BIN40" code="negative-size"><p>Size of binary portion, required numeric size or padding is negative.</p></error><error spec="BIN40" code="octet-out-of-range"><p>Attempting to pack binary value with octet outside range 0-255.</p></error><error spec="BIN40" code="non-numeric-character"><p>Wrong character in binary 'numeric constructor' string.</p></error><error spec="BIN40" code="unknown-encoding"><p>The specified encoding is not supported.</p></error><error spec="BIN40" code="conversion-error"><p>Error in converting to/from a string.</p></error><error spec="BIN40" code="unknown-significance-order"><p>Unknown octet-order value.</p></error></error-list></div1><inform-div1 id="glossary"><head>Glossary</head><?glossary?></inform-div1><inform-div1 id="other-functions"><head>Other Functions</head><p>This Appendix describes some sources of functions or operators that fall outside the
                scope of the function library defined in this specification. It includes both
                function specifications and function implementations. Inclusion of a function in
                this appendix does not constitute any kind of recommendation or endorsement; neither
                is omission from this appendix to be construed negatively. This Appendix does not
                attempt to give any information about licensing arrangements for these function
                specifications or implementations.</p><div2 id="functions-in-other-w3c-specs"><head>XPath Functions and Operators Defined in Other W3C Recommendations</head><p>A number of W3C Recommendations make use of XPath, and in some cases such
                    Recommmendations define additional functions to be made available when XPath is
                    used in a specific host language.</p><div3 id="functions-defined-in-XPathFO"><head>Functions and Operators Defined in XPath and XQuery Functions and
                        Operators</head><p>Of particular interest to this specification, <bibref ref="xpath-functions-40"/> defines<ulist><item><p>Comparison operators
                                    on <code nobreak="false">xs:hexBinary</code> and <code nobreak="false">xs:base64Binary</code>
                                    values, defining the semantics of the <code nobreak="false">eq</code>,
                                    <code nobreak="false">ne</code>, <code nobreak="false">lt</code> and <code nobreak="false">ge</code> operators
                                    applied to binary data. Each returns a boolean
                                    value.</p></item><item><p>A function to retrieve the value of a
                                    binary resource</p></item></ulist></p><table role="data"><thead><tr><th rowspan="1" colspan="1">Function name</th><th rowspan="1" colspan="1">Availability</th><th rowspan="1" colspan="1">Notes</th></tr></thead><tbody><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FO40" ref="func-binary-equal"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Returns <code nobreak="false">true</code> if both binary values contain the same
                                    octet sequence.</td></tr><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FO40" ref="func-binary-less-than"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Returns <code nobreak="false">true</code> if the first argument is less than the
                                    second.</td></tr><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FO40" ref="func-binary-resource"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Returns a resource as <code nobreak="false">xs:base64Binary</code>.</td></tr></tbody></table></div3></div2><div2 id="functions-defined-by-community-groups"><head>Functions Defined by Community Groups</head><div3 id="functions-defined-in-EXPathFile"><head>Functions Defined in EXPath File</head><p>Of particular interest to this specification, <bibref ref="expath-file-40"/>
                        defines the following functions for input and output of
                        <code nobreak="false">xs:base64Binary</code> values:</p><table role="data"><thead><tr><th rowspan="1" colspan="1">Function name</th><th rowspan="1" colspan="1">Availability</th><th rowspan="1" colspan="1">Notes</th></tr></thead><tbody><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FILE40" ref="func-file-read-binary"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Returns the content of a file in its Base64 representation.</td></tr><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FILE40" ref="func-file-write-binary"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Writes a Base64 item as binary data to a file.</td></tr><tr><td rowspan="1" colspan="1">
                                    <xspecref spec="FILE40" ref="func-file-append-binary"/>
                                </td><td rowspan="1" colspan="1">XPath4.0+</td><td rowspan="1" colspan="1">Appends a Base64 item as binary data to a file.</td></tr></tbody></table></div3></div2></inform-div1><inform-div1 id="impl-def"><head>Checklist of implementation-defined features</head><?imp-def-features?></inform-div1><inform-div1 id="changelog" diff="chg" at="2024-11-22"><head>Changes since version 1.0</head><div2 id="changes-summary"><head>Summary of Changes</head><?change-log?></div2><div2 id="changes-to-function-signatures"><head>Changes to Function Signatures</head><olist><item><p>The function signatures of all the specified signatures now use the
                            'optional argument' syntax of XPath 4.0 where appropriate, rather than
                            giving several signatures of differing arity. Other than that, no
                            intended change to the semantics of the functions are assumed.</p></item></olist></div2><div2 id="editorial-changes"><head>Editorial Changes</head><p>These changes are not highlighted in the change-marked version of the
                    specification.</p><olist><item><p>The example functions in <specref ref="example-ASN1"/> have been moved
                            into a differing namespace prefix (<code nobreak="false">asn:</code>) to avoid
                            suggesting that they are part of the supported function set. This is in
                            accordance with the principle that the namespace
                            <code nobreak="false">http://expath.org/ns/binary</code> is reserved solely for use in
                            QNames for functions specified in this module.</p></item></olist></div2></inform-div1><inform-div1 id="back-compatibility" diff="chg" at="A"><head>Compatibility with Previous Versions</head><p>This section summarizes the extent to which this specification is compatible with
                previous versions.</p><p>Version 4.0 of this function library is fully backwards compatible with version 1.0,
                except as noted below:</p><olist><item diff="add" at="2024-11-25"><p>The use of optional arguments in the function signatures means that minor
                        alterations to possible function calls, which would be invalid in 1.0, are
                        now supported. For example:</p><eg xml:space="preserve">bin:decode-string($string,'utf-8',0,())</eg><p>would be invalid in 1.0, as the fourth argument <code nobreak="false">$size</code> is defined
                        to be of type <code nobreak="false">xs:integer</code>. It is valid for 4.0 as the empty
                        sequence denotes default behaviour, that is decoding all octets after
                        <code nobreak="false">$offset</code>
                    </p><p>The functions <code nobreak="false">bin:decode-string</code>,<code nobreak="false">bin:encode-string</code>,
                        <code nobreak="false">bin:pack-double</code>, <code nobreak="false">bin:pack-float</code>,
                        <code nobreak="false">bin:pack-integer</code>, <code nobreak="false">bin:pad-left</code>,
                        <code nobreak="false">bin:pad-right</code>, <code nobreak="false">bin:part</code>,
                        <code nobreak="false">bin:unpack-double</code>, <code nobreak="false">bin:unpack-float</code>,
                        <code nobreak="false">bin:unpack-integer</code> and <code nobreak="false">bin:unpack-unsigned-integer</code>
                        all have similar incompatibilities.</p></item></olist></inform-div1></back></spec>