Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6658375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:54:25+00:00 2026-05-26T01:54:25+00:00

Given below is my XML input. How do I copy or match selected elements

  • 0

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.

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.

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.

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.

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.

XML Input:

<Transaction xmlns="http://www.test.com/rdc.xsd">
    <Transaction>
        <StoreName id="aa">STORE A</StoreName>
        <TransNo>TXN0001</TransNo>
        <RegisterNo>REG001</RegisterNo>
        <Items>
            <Item id="1">
                <ItemID>A001</ItemID>
                <ItemDesc>Keychain</ItemDesc>
            </Item>
            <Item id="2">
                <ItemID>A002</ItemID>
                <ItemDesc>Wallet</ItemDesc>
            </Item>
        </Items>
        <IDONTLIKETHIS_1>
            <STOREXXX>XXX</STOREXXX>
            <TRANSXXX>YYY</TRANSXXX>
        </IDONTLIKETHIS_1>
        <IDONTLIKETHIS_2>
            <STOREXXX>XXX</STOREXXX>
            <TRANSXXX>YYY</TRANSXXX>
        </IDONTLIKETHIS_2>
    </Transaction>
</Transaction>

Desired Output:

<Transaction>
    <Transaction>
        <StoreName id="aa">STORE A</StoreName>
        <TransNo>TXN0001</TransNo>
        <RegisterNo>REG001</RegisterNo>
        <Items>
            <Item id="1">
                <ItemID>A001</ItemID>
                <ItemDesc>Keychain</ItemDesc>
            </Item>
            <Item id="2">
                <ItemID>A002</ItemID>
                <ItemDesc>Wallet</ItemDesc>
            </Item>
        </Items>
    </Transaction>
</Transaction>

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:

<xsl:template match="*">
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
</xsl:template>
<xsl:template match="node()[not(self::*)]">
  <xsl:copy-of select="."/>      
</xsl:template>   
<xsl:template match="*">
  <xsl:copy>
      <xsl:copy-of select="x:Transaction/x:StoreName"/>
      <xsl:copy-of select="x:Transaction/x:TransNo"/>
      <xsl:copy-of select="x:Transaction/x:RegisterNo"/>
      <xsl:copy-of select="x:Transaction/x:Items"/>
 </xsl:copy>      
</xsl:template>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:54:26+00:00Added an answer on May 26, 2026 at 1:54 am

    This transformation uses a list of all names of elements that we want to copy after processing:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:x="http://invia.fujitsu.com/RetailDATACenter/rdc.xsd">
        <xsl:output omit-xml-declaration="yes" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:param name="pNames" select=
        "'|Transaction|StoreName|TransNo|RegisterNo|Items|Item|ItemID|ItemDesc'"/>
    
        <xsl:template match="*" >
         <xsl:if test=
          "contains($pNames,
                    concat('|',local-name(), '|')
                  )">
            <xsl:element name="{name()}">
                <xsl:copy-of select="@*"/>
                <xsl:apply-templates select="node()"/>
            </xsl:element>
         </xsl:if>
        </xsl:template>
    
        <xsl:template match="node()[not(self::*)]">
            <xsl:copy-of select="."/>
        </xsl:template>
    </xsl:stylesheet>
    

    when applied to the provided XML document:

    <Transaction xmlns="http://www.test.com/rdc.xsd">
        <Transaction>
            <StoreName id="aa">STORE A</StoreName>
            <TransNo>TXN0001</TransNo>
            <RegisterNo>REG001</RegisterNo>
            <Items>
                <Item id="1">
                    <ItemID>A001</ItemID>
                    <ItemDesc>Keychain</ItemDesc>
                </Item>
                <Item id="2">
                    <ItemID>A002</ItemID>
                    <ItemDesc>Wallet</ItemDesc>
                </Item>
            </Items>
            <IDONTLIKETHIS_1>
                <STOREXXX>XXX</STOREXXX>
                <TRANSXXX>YYY</TRANSXXX>
            </IDONTLIKETHIS_1>
            <IDONTLIKETHIS_2>
                <STOREXXX>XXX</STOREXXX>
                <TRANSXXX>YYY</TRANSXXX>
            </IDONTLIKETHIS_2>
        </Transaction>
    </Transaction>
    

    the wanted, correct result is produced:

    <Transaction>
       <Transaction>
          <StoreName id="aa">STORE A</StoreName>
          <TransNo>TXN0001</TransNo>
          <RegisterNo>REG001</RegisterNo>
          <Items>
             <Item id="1">
                <ItemID>A001</ItemID>
             </Item>
             <Item id="2">
                <ItemID>A002</ItemID>
             </Item>
          </Items>
       </Transaction>
    </Transaction>
    

    Do note: One advantage of this solution over other possible solution is that the string containing the pipe-delimited list of element names can be provide as an externally-specified parameter to the transformation, making it very powerful and flexible and eliminating the need to alter the code any time we include new elements (or exclude some) from our white-list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking to transform a input xml given below <profile name=default> <color id=forecolor
I am using XML::Simple for parsing a XML file. Code is given below with
I'm using C# and .NET 2.0. Given the XML below, I'd like to get
I have an XML file that I want to transform using an XSLT. It
Given the XML below.. and given that I have two variable 'Idnt' and 'Xref'
Given the contrived XML schema, sample XML input, and sample XSLT below used to
I have a xml given below: <root title=الصفحة الرئيسة> <item title=الصفحة الرئيسة itemuri=tcm:8-29-4 ShowInNav=True
Given the XML below, I need to group by InputItem and sum the costs
In the below XSL every time the xsl:when gets satisfied I want to append
I am using XML::Simple to parse a XML file. The output is a hash.(using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.