Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>This can be done in a simple and short way (no explicit conditionals)</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="cds_dt" exclude-result-prefixes="x"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&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:template match="ContactPurpose/x:PurposeAsPlainText/text()"&gt;0&lt;/xsl:template&gt; &lt;xsl:template match="ContactPurpose/*"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied to the following XML document</strong> (extended to encorporate both cases of interest):</p> <pre><code>&lt;Contact&gt; &lt;ContactPurpose&gt; &lt;PurposeAsPlainText xmlns="cds_dt"&gt;Call&lt;/PurposeAsPlainText&gt; &lt;/ContactPurpose&gt; &lt;ContactPurpose&gt; &lt;PurposeAsEnum xmlns="cds_dt"&gt;Call&lt;/PurposeAsEnum&gt; &lt;/ContactPurpose&gt; &lt;/Contact&gt; </code></pre> <p><strong>produces the wanted, correct result</strong>:</p> <pre><code>&lt;Contact&gt; &lt;ContactPurpose&gt;0&lt;/ContactPurpose&gt; &lt;ContactPurpose&gt;Call&lt;/ContactPurpose&gt; &lt;/Contact&gt; </code></pre> <p><strong>Explanation</strong>:</p> <p>Overriding the <strong><a href="http://www.dpawson.co.uk/xsl/sect2/identity.html" rel="nofollow">identity rule</a></strong> and appropriate use of templates/match patterns.</p> <p><strong>Update</strong>: The OP has modified his XML document, which is now in a default namespace:</p> <pre><code>&lt;MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;PatientRecord&gt; &lt;Demographics&gt; &lt;Contact&gt; &lt;ContactPurpose&gt; &lt;PurposeAsPlainText xmlns="cds_dt"&gt;Call&lt;/PurposeAsPlainText&gt; &lt;/ContactPurpose&gt; &lt;ContactPurpose&gt; &lt;PurposeAsEnum xmlns="cds_dt"&gt;Call&lt;/PurposeAsEnum&gt; &lt;/ContactPurpose&gt; &lt;/Contact&gt; &lt;/Demographics&gt; &lt;/PatientRecord&gt; &lt;/MyDS&gt; </code></pre> <p><strong>Accordingly, here is a slightly modified transformation that produces the wanted result</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:x="cds_dt" xmlns:c="cds" exclude-result-prefixes="c x"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&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:template match="c:ContactPurpose/x:PurposeAsPlainText/text()"&gt;0&lt;/xsl:template&gt; &lt;xsl:template match="c:ContactPurpose/*"&gt;&lt;xsl:apply-templates/&gt;&lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>When this transformation is applied on the new XML document (closest above), the new wanted, correct result is produced</strong>:</p> <pre><code>&lt;MyDS xmlns="cds" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt; &lt;PatientRecord&gt; &lt;Demographics&gt; &lt;Contact&gt; &lt;ContactPurpose&gt;0&lt;/ContactPurpose&gt; &lt;ContactPurpose&gt;Call&lt;/ContactPurpose&gt; &lt;/Contact&gt; &lt;/Demographics&gt; &lt;/PatientRecord&gt; &lt;/MyDS&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload