World Library  
Flag as Inappropriate
Email this Article

Identity transform

Article Id: WHEBN0004081099
Reproduction Date:

Title: Identity transform  
Author: World Heritage Encyclopedia
Language: English
Subject: Data transformation, Transformation language, Source-to-source compiler, Computer programming, XSLT
Collection:
Publisher: World Heritage Encyclopedia
Publication
Date:
 

Identity transform

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.

Examples of recursive transforms

The "copy with recursion" permits, changing little portions of code, produce entire new and different output, filtering or updating the input. Understanding the "identity by recursion" we can understand the filters.

Using XSLT

The most frequently cited example of the identity transform (for XSLT version 1.0) is the "copy.xsl" transform as expressed in [1] to perform the identity transformation:


  
    
      
    
  

This template works by matching all attributes (@*) and other nodes (node()), copying each node matched, then applying the identity transformation to all attributes and child nodes of the context node. This recursively descends the element tree and outputs all structures in the same structure they were found in the original file, within the limitations of what information is considered significant in the XPath data model. Since node() matches text, processing instructions, root, and comments, as well as elements, all XML nodes are copied.

A more explicit version of the identity transform is:


  
    
      
    
  

This version is equivalent to the first, but explicitly enumerates the types of XML nodes that it will copy. Both versions copy data that is unnecessary for most XML usage (e.g., comments).

Finally, note that markup details, such as the use of CDATA sections or the order of attributes, is not necessarily preserved in the output, since this information is not part of the XPath data model. To show CDATA markup in the output, the XSLT stylesheet that contains the identity transform template (not the identity transform template itself) should make use of the xsl:output attribute called cdata-section-elements.

[1] For example:


Using XQuery

XQuery can define recursive functions. The following example XQuery 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
    }
};

The same function can also be achieved using a typeswitch-style transform.

xquery version "1.0";

(: copy the input to the output without modification :)
declare function local:copy($input as item()*) as item()* {
for $node in $input
   return 
      typeswitch($node)
        case element()
           return
              element {name($node)} {

                (: output each attribute in this element :)
                for $att in $node/@*
                   return
                      attribute {name($att)} {$att}
                ,
                (: output all the sub-elements of this element recursively :)
                for $child in $node
                   return local:copy($child/node())

              }
        (: otherwise pass it through.  Used for text(), comments, and PIs :)
        default return $node
};

The typeswitch transform is sometime preferable since it can easily be modified by simply adding a case statement for any element that needs special processing.

Non-recursive transforms

Two simple and illustrative "copy all" transforms.

Using XSLT


  
    
  

Using XProc


  

Here one important note about the XProc identity, is that it can take either one document like this example or a sequence of document as input.

More complex examples

Generally the identity transform is used as a base on which one can make local modifications.

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:

  
    
      
    
  

  
  

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 one would add:

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

Using XProc


  
  

See also

Further reading

  • 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

References

  1. ^ a b W3.org - XSL Transformations Version 1.0 - Copying
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and USA.gov, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for USA.gov and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
 
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
 
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.
 



Copyright © World Library Foundation. All rights reserved. eBooks from World Library are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.