am trying to flatten an XML element based on a child element name using XSLT 1.0
The source XML:
<Contact>
<ContactPurpose>
<PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
</ContactPurpose>
<ContactPurpose>
<PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
</ContactPurpose>
</Contact>
Should be transformed into the following XML:
<Contact>
<ContactPurpose>O</ContactPurpose>
<ContactPurpose>Call</ContactPurpose>
</Contact>
The Logic is:
IF the child element name is “PurposeAsPlainText “ THEN
set “O” for Other in destination
ELSEIF the child element name is “PurposeAsEnum” THEN
copy the source value to destination
EDIT 1: I could be more clear as none of the solutions flattened the xml, please see revised source and dest XML.
EDIT 2: Here is the XML I was testing against. The two tranform solutions below actually do work on my original xml but not the revised xml that I was testing using .NET 4.0 XslCompiledTransform. Or should I make a new question?
<MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PatientRecord>
<Demographics>
<Contact>
<ContactPurpose>
<PurposeAsPlainText xmlns="cds_dt">Call</PurposeAsPlainText>
</ContactPurpose>
<ContactPurpose>
<PurposeAsEnum xmlns="cds_dt">Call</PurposeAsEnum>
</ContactPurpose>
</Contact>
</Demographics>
</PatientRecord>
</MyDS>
This can be done in a simple and short way (no explicit conditionals):
when applied to the following XML document (extended to encorporate both cases of interest):
produces the wanted, correct result:
Explanation:
Overriding the identity rule and appropriate use of templates/match patterns.
Update: The OP has modified his XML document, which is now in a default namespace:
Accordingly, here is a slightly modified transformation that produces the wanted result:
When this transformation is applied on the new XML document (closest above), the new wanted, correct result is produced: