This site is work in progress and therefore incomplete yet.

 p:hash (3.0) 

Computes a hash code for a value.

Summary

<p:declare-step type="p:hash">
  <input port="source" primary="true" content-types="xml html" sequence="false"/>
  <output port="result" primary="true" content-types="text xml html" sequence="false"/>
  <option name="algorithm" as="xs:QName" required="true"/>
  <option name="value" as="xs:string" required="true"/>
  <option name="match" as="xs:string" required="false" select="'/*/node()'"/>
  <option name="parameters" as="map(xs:QName,item()*)?" required="false" select="()"/>
  <option name="version" as="xs:string?" required="false" select="()"/>
</p:declare-step>

The p:hash step generates a hash code for some value and injects this into the document appearing on the source port.

Ports:

Type

Port

Primary?

Content types

Seq?

Description

input

source

true

xml html

false

The source document to record the computed hash code in.

output

result

true

text xml html

false

Result document, derived from the source document, intended to contain the hash code. See the match option and the description below.

Options:

Name

Type

Req?

Default

Description

algorithm

xs:QName

true

 

The hash computation algorithm to use. See the description below.

value

xs:string

true

 

The string value to calculate the hash code from.

match

xs:string (XSLT selection pattern)

false

/*/node()

An XSLT selection pattern that tells p:hash where to insert the hash code in the source document. All node(s) matched are replaced with the computed hash code (the nodes themselves, not just their contents).

If this option matches an attribute, the value of the attribute is changed.

If this option matches the document-node /, the entire document is replaced with the computed hash code. The result will be a text document.

parameters

map(xs:QName,item()*)?

false

()

Parameters controlling the hash code computation. Keys, values and their meaning depend on the XProc processor used.

version

xs:string?

false

()

Specifies the version of the hash computation algorithm used. If not specified, a default version is used, which depends on the value of the algorithm option. See the description below.

Description

A hash code in the digital world is a, relatively simple, value computed from some, possibly lengthy, input data. The same input data always results in the same hash code. Hash codes are also called hash values, (hash) digests, (digital) fingerprints, or simply hashes. Hash codes are used for various purposes, see for instance Wikipedia.

The p:hash step computes the hash code of the string value of the value option and inserts this into the document appearing on the source port. The result appears on the result port.

The algorithm used for computing the hash code must be specified using the algorithm option. There are 3 predefined values, that must be supported by all XProc processors:

algorithm option value

Default version

Algorithm used

crc

32

Cyclic Redundancy Check

md

5

Message-digest

sha

1

Secure Hash Algorithm

You can specify the algorithm version using the version option. If you don’t specify this option and use one of the predefined algorithm option values, the default version from the table above is used.

Examples

Basic usage

The following example illustrates what happens when we compute a hash value and use the default value for the match option (/*/node()): all child nodes of the root element (in this example just the <hash> element) are replaced with the computed hash value.

Source document:

<hash-value>
   <hash>Will be replaced by the hash value!</hash>
</hash-value>

Pipeline document:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">

  <p:input port="source"/>
  <p:output port="result"/>

  <p:hash algorithm="crc" value="Hi there!"/>

</p:declare-step>

Result document:

<hash-value>b5c57055b5c57055b5c57055</hash-value>

The following example shows the different values computed using the different standard algorithms. These are placed in attribute values.

Source document:

<hash-values crc="" md="" sha=""/>

Pipeline document:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">

  <p:input port="source"/>
  <p:output port="result"/>

  <p:hash algorithm="crc" value="Hi there!" match="/*/@crc"/>
  <p:hash algorithm="md" value="Hi there!" match="/*/@md"/>
  <p:hash algorithm="sha" value="Hi there!" match="/*/@sha"/>

</p:declare-step>

Result document:

<hash-values crc="b5c57055"
             md="396199333edbf40ad43e62a1c1397793"
             sha="95e2b07e12754e52c37cfd485544d4f444597bff"/>

Hash code as text

If the match option matches the document-node /, the resulting document will be a text document containing the computed hash code only. Notice that in this case the input document doesn’t matter.

Source document:

<anything/>

Pipeline document:

<p:declare-step xmlns:p="http://www.w3.org/ns/xproc" version="3.0">

  <p:input port="source"/>
  <p:output port="result"/>

  <p:hash algorithm="crc" value="Hi there!" match="/"/>

</p:declare-step>

Result text document:

b5c57055

Additional details

  • p:hash preserves all document-properties of the document(s) appearing on its source port.

    The exception is when the match option matches the document-node /. In that case the resulting document will be of type text, the content-type document-property will become text/plain and a serialization document-property is removed. Any other document-property is preserved.

  • If an XProc processor supports any other algorithm, its code (as supplied to the algorithm option) will be in a namespace.

  • If the match option matches an attribute called xml:base, the base URI of this attribute’s parent element is amended accordingly. This is a side-effect of changing an attribute with a pre-defined meaning (and in this case probably never useful).

Errors raised

Error code

Description

XC0036

It is a dynamic error if the requested hash algorithm is not one that the processor understands or if the value or parameters are not appropriate for that algorithm.

Reference information

This description of the p:hash step is for XProc version: 3.0. This is a required step (an XProc 3.0 processor must support this).

The formal specification for the p:hash step can be found here.

The p:hash step is part of categories:

The p:hash step is also present in version: 3.1.