fbpx
Wikipedia

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 edit

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 edit

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

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="@*|node()">  <xsl:copy>  <xsl:apply-templates select="@*|node()"/>  </xsl:copy>  </xsl:template> </xsl:stylesheet> 

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:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="@*|*|processing-instruction()|comment()">  <xsl:copy>  <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>  </xsl:copy>  </xsl:template> </xsl:stylesheet> 

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).

XSLT 3.0 edit

XSLT 3.0[2] specifies an on-no-match attribute of the xsl:mode instruction that allows the identity transform to be declared rather than implemented as an explicit template rule. Specifically:

<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:mode on-no-match="shallow-copy" /> </xsl:stylesheet> 

is essentially equivalent to the earlier template rules. See the XSLT 3.0 standard's description of shallow-copy[3] for details.

Finally, note that markup details, such as the use of CDATA sections or the order of attributes, are 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.

cdata-section-elements specifies a list of the names of elements whose text node children should be output using CDATA sections. [1] For example:

<xsl:output method="xml" encoding="utf-8" cdata-section-elements="element-name-1 element-name-2"/> 

Using XQuery edit

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 document-node()  return  document {  local:copy($node/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 edit

Two simple and illustrative "copy all" transforms.

Using XSLT edit

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  <xsl:template match="/">  <xsl:copy-of select="."/>  </xsl:template> </xsl:stylesheet> 

Using XProc edit

<p:pipeline name="pipeline" xmlns:p="http://www.w3.org/ns/xproc">  <p:identity/> </p:pipeline> 

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 edit

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

Remove named element transform edit

Using XSLT edit

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"/> 

Using XQuery edit

 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 edit

<p:pipeline name="pipeline" xmlns:p="http://www.w3.org/ns/xproc">  <p:identity/>  <p:delete match="PersonSSNID"/> </p:pipeline> 

See also edit

Further reading edit

  • 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 edit

  1. ^ a b W3.org - XSL Transformations Version 1.0 - Copying
  2. ^ W3.org - XSL Transformations Version 3.0
  3. ^ W3.org - XSL Transformations Version 3.0 - Built-in Templates: Shallow Copy

identity, transform, this, article, needs, additional, citations, verification, please, help, improve, this, article, adding, citations, reliable, sources, unsourced, material, challenged, removed, find, sources, news, newspapers, books, scholar, jstor, decemb. This article needs additional citations for verification Please help improve this article by adding citations to reliable sources Unsourced material may be challenged and removed Find sources Identity transform news newspapers books scholar JSTOR December 2022 Learn how and when to remove this template message 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 1 Examples of recursive transforms 1 1 Using XSLT 1 1 1 XSLT 3 0 1 2 Using XQuery 2 Non recursive transforms 2 1 Using XSLT 2 2 Using XProc 3 More complex examples 3 1 Remove named element transform 3 1 1 Using XSLT 3 1 2 Using XQuery 3 1 3 Using XProc 4 See also 5 Further reading 6 ReferencesExamples of recursive transforms editThe 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 edit The most frequently cited example of the identity transform for XSLT version 1 0 is the copy xsl transform as expressed in XSLT This transformation uses the xsl copy command 1 to perform the identity transformation lt xsl stylesheet version 1 0 xmlns xsl http www w3 org 1999 XSL Transform gt lt xsl template match node gt lt xsl copy gt lt xsl apply templates select node gt lt xsl copy gt lt xsl template gt lt xsl stylesheet gt 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 lt xsl stylesheet version 1 0 xmlns xsl http www w3 org 1999 XSL Transform gt lt xsl template match processing instruction comment gt lt xsl copy gt lt xsl apply templates select text processing instruction comment gt lt xsl copy gt lt xsl template gt lt xsl stylesheet gt 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 XSLT 3 0 edit XSLT 3 0 2 specifies an on no match attribute of the xsl mode instruction that allows the identity transform to be declared rather than implemented as an explicit template rule Specifically lt xsl stylesheet version 3 0 xmlns xsl http www w3 org 1999 XSL Transform gt lt xsl mode on no match shallow copy gt lt xsl stylesheet gt is essentially equivalent to the earlier template rules See the XSLT 3 0 standard s description of shallow copy 3 for details Finally note that markup details such as the use of CDATA sections or the order of attributes are 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 cdata section elements specifies a list of the names of elements whose text node children should be output using CDATA sections 1 For example lt xsl output method xml encoding utf 8 cdata section elements element name 1 element name 2 gt Using XQuery edit XQuery can define recursive functions The following example XQuery function copies the input directly to the output without modification nbsp Wikibooks has a book on the topic of XQuery Filtering Nodes 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 nbsp Wikibooks has a book on the topic of XQuery Typeswitch Transformations 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 document node return document local copy node 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 editTwo simple and illustrative copy all transforms Using XSLT edit lt xsl stylesheet version 1 0 xmlns xsl http www w3 org 1999 XSL Transform gt lt xsl template match gt lt xsl copy of select gt lt xsl template gt lt xsl stylesheet gt Using XProc edit lt p pipeline name pipeline xmlns p http www w3 org ns xproc gt lt p identity gt lt p pipeline gt 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 editGenerally the identity transform is used as a base on which one can make local modifications Remove named element transform edit Using XSLT edit 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 lt xsl template match node gt lt xsl copy gt lt xsl apply templates select node gt lt xsl copy gt lt xsl template gt lt remove all social security numbers gt lt xsl template match PersonSSNID gt Using XQuery edit 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 edit lt p pipeline name pipeline xmlns p http www w3 org ns xproc gt lt p identity gt lt p delete match PersonSSNID gt lt p pipeline gt See also editData mapping XML pipelineFurther reading editXSLT 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 109References edit a b W3 org XSL Transformations Version 1 0 Copying W3 org XSL Transformations Version 3 0 W3 org XSL Transformations Version 3 0 Built in Templates Shallow Copy Retrieved from https en wikipedia org w index php title Identity transform amp oldid 1125843581, wikipedia, wiki, book, books, library,

article

, read, download, free, free download, mp3, video, mp4, 3gp, jpg, jpeg, gif, png, picture, music, song, movie, book, game, games.