Identity transform
≈ 4 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.
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.
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). is essentially equivalent to the earlier template rules. See the XSLT 3.0 standard's description of shallow-copy 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.
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 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.
Using XSLT
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. 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')
See also
Data mapping XML pipeline
References
Text derived from https://en.wikipedia.org/w/index.php?oldid=1125843581 under CC BY-SA 4.0; modifications have been made.