0

I have a requierement as below:

if i give input as :

<?xml version="1.0"?>
<new:NewAddressData xmlns:new="http://www.example.org/NewAddress">
  <new:NewStreet></new:NewStreet>
  <new:NewArea>Area_1</new:NewArea>
  <new:NewState></new:NewState>
</new:NewAddressData>

Output should be:

<new:NewArea>Area_1</new:NewArea>

Actually Iam new bee to XSLT but I read some basics and tried below code :

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">        
    <xsl:copy>
        <xsl:choose>
            <xsl:when test="@*|node() != ''">  
      <xsl:value-of select="." disable-output-escaping="yes" />
            </xsl:when>                        
            <xsl:otherwise>
                <xsl:apply-templates  select="@*|node()"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:copy>
</xsl:template>

for this I am getting output as :

<new:NewAddressData xmlns:new="http://www.example.org/NewAddress">Area_1</new:NewAddressData>

where expected value should be like :

<new:NewArea>Area_1</new:NewArea>

So how can I achieve this using XSLT 1.0

Thanks in advance

5
  • 4
    What you did here is: You dropped a requirement onto this website, in hopes someone would give you the codes. That's not how this website works. Please show your efforts and explain the specific problems you have. We are not a free programming service. Commented Nov 24, 2014 at 14:54
  • Input is not standard XML, it is not well formed, place your XSLT, what you have tried to get the result. Commented Nov 24, 2014 at 15:31
  • Hi @Tomalak thanks for the response. please let me know if any other info required. Commented Nov 24, 2014 at 19:36
  • Hi @RudramuniTP thanks for the response actually i had placed the input xml which i am using. let me know if any further info required. Commented Nov 24, 2014 at 19:38
  • @Uday, your are welcome. U asked a nice question, in future along post place complete info like 'input XML', XSLT, and required result. Commented Nov 25, 2014 at 9:27

2 Answers 2

1

You could do something like this:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="*[text()]">
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

Depending on input, like if there was more than one element that contained text, this might result in output that is not well formed.

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect code, it is working for complex elements too.
Hi @Daniel, thanks for the helping hand. code is working as expected.
0

It looks like you have read about the XSLT Identity Template, which is good!

 <xsl:template match="@*|node()">
   <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
   </xsl:copy>
</xsl:template>

On its own, this will copy across all nodes unchanged (such as your NewArea element), so you need to then write templates for the things you do want to change. In this case, it looks like you want to remove elements that don't have non-empty text nodes as children.

 <xsl:template match="*[not(text()[normalize-space()])]">

Try this XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" indent="yes"/>

   <xsl:template match="@*|node()">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>

   <xsl:template match="*[not(text()[normalize-space()])]">
      <xsl:apply-templates />
   </xsl:template>
</xsl:stylesheet>

This would output the following

 <new:NewArea xmlns:new="http://www.example.org/NewAddress">Area_1</new:NewArea>

The namespace is necessary here. You can not output an element with a prefix without also declaring the namespace associated with it.

1 Comment

Hi @Tim, thanks for the helping hand. code is working as expected.

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.