Identity transform

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

In metadata, the identity transform is a data transformation that copies the source data into the destination data without change.

The identity transformation is considered an essential process in creating a reusable transformation library. By creating a library of variations of the base identity transformation, a variety of data transformation filters can be easily maintained. These filters can be chained together in a format similar to UNIX shell pipes.

Contents

Example using XSLT

The most frequently cited example of the identity transform is the "copy.xsl" transform as expressed in XSLT. This transformation uses the xsl copy command to perform the identity transformation:

<xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

This template works by first matching all attributes (@*) and then all nodes (node()) and then applying the copy transformation to all sub-nodes of the current data element. This recursively descends the data element hierarchy and outputs all structures in the same structure they were found in the original file.

Example Using XQuery

XQuery allows you to define recursive functions. The following example function copies the input directly to the output without modification.

declare function local:copy($element as element()) {
  element {node-name($element)}
    {$element/@*,
     for $child in $element/node()
        return if ($child instance of element())
          then local:copy($child)
          else $child
    }
};

Remove Named Element Transform Using XSLT

The identity transformation can be modified to copy everything from an input tree to an output tree except a given node. For example the following will copy everything from the input to the output except the social security number:

  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
 
  <!-- remove all social security numbers -->
  <xsl:template match="PersonSSNID"/>

Remove Named Element Using XQuery

 declare function local:copy-filter-elements($element as element() , $element-name as xs:string*) as element() {
   element {node-name($element) }
             { $element/@*,
               for $child in $element/node()[not(name(.)=$element-name)]
                  return if ($child instance of element())
                    then local:copy-filter-elements($child,$element-name)
                    else $child
           }
 };

To call this you would add the following:

$filtered-output := local:copy-filter-elements($input, 'PersonSSNID')

See also

References

  • The original reference to the copy transform in the w3c recommendation document [1]
  • The identity operation is also a required component of an XML Pipeline using the w3c XProc working draft [2] (accessed Nov 21, 2006)
  • Use of the identity transform is covered in the book XSLT Cookbook, O'Reilly Media, Inc., December 1, 2002, by Sal Mangano, ISBN 0-596-00372-2
  • Priscilla Walmsley, XQuery, O'Reilly Media, Inc., Chapter 8 Functions - Recursive Functions - page 109
Personal tools

Served in 0.075 secs.