Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to copy or match only selected elements from an XML input with namespace and then remove the namespace at the same time
    primarykey
    data
    text
    <p>Given below is my XML input. How do I copy or match selected elements that I want to reflect in my XML output using XSL. </p> <p>The general idea of the logic should be to specify only the elements I am interested instead of specifying the elements that I don't like to be included in the output. The elements that I want to reflect in my output XML are always present in the XML Inpput. The rest of the elements vary depending on what the system has generated and so I cannot just specify what to remove.</p> <p>I was able to this without the namespace from the input by doing the copy-of, but when the namespace is present the code is not working. </p> <p>I found a way to remove the namespace but when combined with the copy-of, didn't work as well. I am quite confused as to how XSL behaves. </p> <p>Please bear with my inquiry, I'm very new to XML and XSL and I was assigned to this task because no one from our team had the experience working with XML. Thank you in advance.</p> <p>XML Input:</p> <pre><code>&lt;Transaction xmlns="http://www.test.com/rdc.xsd"&gt; &lt;Transaction&gt; &lt;StoreName id="aa"&gt;STORE A&lt;/StoreName&gt; &lt;TransNo&gt;TXN0001&lt;/TransNo&gt; &lt;RegisterNo&gt;REG001&lt;/RegisterNo&gt; &lt;Items&gt; &lt;Item id="1"&gt; &lt;ItemID&gt;A001&lt;/ItemID&gt; &lt;ItemDesc&gt;Keychain&lt;/ItemDesc&gt; &lt;/Item&gt; &lt;Item id="2"&gt; &lt;ItemID&gt;A002&lt;/ItemID&gt; &lt;ItemDesc&gt;Wallet&lt;/ItemDesc&gt; &lt;/Item&gt; &lt;/Items&gt; &lt;IDONTLIKETHIS_1&gt; &lt;STOREXXX&gt;XXX&lt;/STOREXXX&gt; &lt;TRANSXXX&gt;YYY&lt;/TRANSXXX&gt; &lt;/IDONTLIKETHIS_1&gt; &lt;IDONTLIKETHIS_2&gt; &lt;STOREXXX&gt;XXX&lt;/STOREXXX&gt; &lt;TRANSXXX&gt;YYY&lt;/TRANSXXX&gt; &lt;/IDONTLIKETHIS_2&gt; &lt;/Transaction&gt; &lt;/Transaction&gt; </code></pre> <p>Desired Output:</p> <pre><code>&lt;Transaction&gt; &lt;Transaction&gt; &lt;StoreName id="aa"&gt;STORE A&lt;/StoreName&gt; &lt;TransNo&gt;TXN0001&lt;/TransNo&gt; &lt;RegisterNo&gt;REG001&lt;/RegisterNo&gt; &lt;Items&gt; &lt;Item id="1"&gt; &lt;ItemID&gt;A001&lt;/ItemID&gt; &lt;ItemDesc&gt;Keychain&lt;/ItemDesc&gt; &lt;/Item&gt; &lt;Item id="2"&gt; &lt;ItemID&gt;A002&lt;/ItemID&gt; &lt;ItemDesc&gt;Wallet&lt;/ItemDesc&gt; &lt;/Item&gt; &lt;/Items&gt; &lt;/Transaction&gt; &lt;/Transaction&gt; </code></pre> <p>I've tried the code below but the problem with it is that I'm missing the second Transaction element and the xmlns attribute is present in the root element:</p> <pre><code>&lt;xsl:template match="*"&gt; &lt;xsl:copy-of select="@*"/&gt; &lt;xsl:apply-templates/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="node()[not(self::*)]"&gt; &lt;xsl:copy-of select="."/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="*"&gt; &lt;xsl:copy&gt; &lt;xsl:copy-of select="x:Transaction/x:StoreName"/&gt; &lt;xsl:copy-of select="x:Transaction/x:TransNo"/&gt; &lt;xsl:copy-of select="x:Transaction/x:RegisterNo"/&gt; &lt;xsl:copy-of select="x:Transaction/x:Items"/&gt; &lt;/xsl:copy&gt; &lt;/xsl:template&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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