0

I am using XSLT to do XML to XML by removing some nodes. I am new to XSLt and it is confusing.

XML input,

<?xml version="1.0" encoding="UTF-8"?>
    <role>
       <status>success</status>
       <data>
          <name>ac1</name>
       </data>
       <data>
          <name>ac2</name>
       </data>
       <data>
          <name>ac3</name>
       </data>
       <day>monday</day>
    </role>

XSLT is,

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
   <xsl:output method="xml" encoding="utf-8" indent="yes" />
   <xsl:template match="role">
      <xsl:copy>
         <xsl:for-each select="data">
            <disaply_name>
               <xsl:value-of select="name" />
            </disaply_name>
         </xsl:for-each>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

Output is,

<?xml version="1.0" encoding="utf-8"?>
<role>
  <disaply_name>ac1</disaply_name>
  <disaply_name>ac2</disaply_name>
  <disaply_name>ac3</disaply_name>
</role>

Expected Output is,

<?xml version="1.0" encoding="utf-8"?>
<root>
<role2>
  <disaply_name>ac1</disaply_name>
</role2>
<role2>
  <disaply_name>ac2</disaply_name>
</role2>
<role2>
  <disaply_name>ac3</disaply_name>
</role2>
</root>

What should I modify? .......................

2
  • You should modify what your expected output is. You shouldn't be having several <role> tags, as it is a root node. Your expected output is not valid XML. Commented Nov 8, 2017 at 19:21
  • I have changed the expected output.. Commented Nov 8, 2017 at 19:31

1 Answer 1

1

Your use of <xsl:copy> means that it's copying the current node, in this case <role>, which explains its appearance in your output. And since there are no other nodes specified before, XSLT is considering it to be the root node. So I just removed it, and correctly placed the nodes <root> and <role2>.

To got your expected output, try using the following:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes" />
    <xsl:template match="role">
        <root>
            <xsl:for-each select="data">
                <role2>
                    <display_name>
                        <xsl:value-of select="name" />
                    </display_name>
                </role2>
            </xsl:for-each>
        </root>
    </xsl:template>
</xsl:stylesheet>

Here is the output I got:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <role2>
    <display_name>ac1</display_name>
  </role2>
  <role2>
    <display_name>ac2</display_name>
  </role2>
  <role2>
    <display_name>ac3</display_name>
  </role2>
</root>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.