I have the following XML and would like to get the value of XML attribute code="MA"
from <FullNameVerifiesToAddress>
node and <FullNameVerifiesToSSN>
node into the node <Summary>
.
<PreciseIDServer>
<Header>
<ReportDate>09042018</ReportDate>
<ReportTime>235641</ReportTime>
</Header>
<Summary>
<TransactionID>1421957889</TransactionID>
<InitialDecision>ACC</InitialDecision>
<FinalDecision>ACC</FinalDecision>
<CrossReferenceIndicatorsGrid>
<FullNameVerifiesToAddress code="MA"/>
<FullNameVerifiesToSSN code="MA"/>
</CrossReferenceIndicatorsGrid>
</Summary>
</PreciseIDServer>
I use the following XSLT right now to get the <ReportTime>
from <Header>
node into <summary>
but I also need the above mentioned attributes in the Summary node.
<xsl:template match="Summary">
<xsl:copy>
<xsl:apply-templates select="@* | ancestor::PreciseIDServer/Header/ReportTime | node()"/>
</xsl:copy>
</xsl:template>
the XML i want as OUTPUT should be something like
<PreciseIDServer>
<Header>
<ReportDate>09042018</ReportDate>
<ReportTime>235641</ReportTime>
</Header>
<Summary>
<TransactionID>1421957889</TransactionID>
<InitialDecision>ACC</InitialDecision>
<FinalDecision>ACC</FinalDecision>
<ReportTime>235641</ReportTime>
<FullNameVerifiesToAddress>MA </FullNameVerifiesToAddress>
<FullNameVerifiesToSSN> MA </FullNameVerifiesToSSN>
<CrossReferenceIndicatorsGrid>
<FullNameVerifiesToAddress code="MA"/>
<FullNameVerifiesToSSN code="MA"/>
</CrossReferenceIndicatorsGrid>
</Summary>
</PreciseIDServer>
I think you need to push some nodes through a second mode as you want to output them once uncopied while you also want to transform them, so a minimal XSLT 3 solution would be
https://xsltfiddle.liberty-development.net/bdxtqL
For XSLT 2 you would need to replace the
xsl:mode
declaration with the identity transformation templateand the text value template
{@code}
with an<xsl:value-of select="@code"/>
.For XSLT 1 you need the same changes as for XSLT 2 but additionally need to put a name on the identity template (i.e. change
<xsl:template match="@* | node()">
to<xsl:template match="@* | node()" name="identity">
) and replace the use of<xsl:next-match/>
with<xsl:call-template name="identity"/>
.