I am trying to understand functinality of identity template.
<xsl:template match='@* | node()'>
<xsl:apply-templates select='@* | node()'/>
</xsl:template>
The above template if present in start of our XSL document, we call it identity template and its function is to call all the templates i.,e., by matching every thing under @* and node() in match. Then it goes to second line apply- templates where it recursively selects all the templates/nodes. please correct me if i am wrong here.
My question is if we have this template placed in middle of XSLT,but not in starting, and we dont want all the matching action, but only of a certain message stored in a variable like
<xsl:template match='$variable holding entire soap envelope'>
<xsl:apply-templates select='@* | node()'/>
</xsl:template>
Do we still call it identity template and will still it processes same like identity template in first action.?
Sorry if my question is not clear.
Thanks for help.
No, templates overriding to the identity transformation would not themselves be considered to be identity transformations.
The way to use the identity transformation template is to let it copy everything from the input XML to the output XML by default but to override the identity transformation with more specific match
patterns where desired.
For example, consider this SOAP message:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header>
<n:alertcontrol xmlns:n="http://example.org/alertcontrol">
<n:priority>1</n:priority>
<n:expires>2001-06-22T14:00:00-05:00</n:expires>
</n:alertcontrol>
</env:Header>
<env:Body>
<m:alert xmlns:m="http://example.org/alert">
<m:msg>Pick up Mary at school at 2pm</m:msg>
</m:alert>
</env:Body>
</env:Envelope>
If we wanted to transform this message to another that's nearly the same, but perhaps with different m:msg
content, we could use an identity transformation with an override for m:msg
:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://example.org/alert">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:msg">
<xsl:copy>***Pick up Mary at school at 1pm, not 2pm.***</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Applying the above XSLT to the input SOAP message produces an identical SOAP message but with new m:msg
element contents:
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Header>
<n:alertcontrol xmlns:n="http://example.org/alertcontrol">
<n:priority>1</n:priority>
<n:expires>2001-06-22T14:00:00-05:00</n:expires>
</n:alertcontrol>
</env:Header>
<env:Body>
<m:alert xmlns:m="http://example.org/alert">
<m:msg>***Pick up Mary at school at 1pm, not 2pm.***</m:msg>
</m:alert>
</env:Body>
</env:Envelope>
The identity transformation template copies every node so it looks like
<xsl:template match='@* | node()'>
<xsl:copy>
<xsl:apply-templates select='@* | node()'/>
</xsl:copy>
</xsl:template>
As for a match pattern starting with a variable reference like match='$variable'
, I don't think this is even allowed in XSLT 1.0.