Boopathy S
asked on
Need to remove the space element values using XSL
I have two types of XML elements, one type is
First XML:
<Body>
<h1>aaa</h1>
<h1>bbb</h1>
<h1>ccc</h1>
</Body>
Second XML:
<Body>
<h1> </h1>
<h1>aaa</h1>
<h1>bbb</h1>
</Body>
XSL I'm using as
<xsl:strip-space elements="h1"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templ ates select="@*|node()"/></xsl: copy>
</xsl:template>
<xsl:template match="Body">
<xsl:copy>
<h1><xsl:value-of select="h1"/></h1>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="h1"/>
Output I'm getting correctly for first XML as like:
<Body>
<h1>aaa bbb ccc</h1>
</Body>
But even I used strip-space element as h1 for the second xml, I'm getting like below:
<Body>
<h1> aaa bbb</h1>
</Body>
Expected output be like
<Body>
<h1>aaa bbb</h1>
</Body>
Need to remove the space element of h1. But due to that tag, its combining even the space. I used priority and used the seperate h1[normalize-space()] tag also in the template. But its not working. There is having any way to avoid this. Please suggest. I'm using XSLT 2.0 and saxon-PE 9.6.0.7. Thanks in advance
First XML:
<Body>
<h1>aaa</h1>
<h1>bbb</h1>
<h1>ccc</h1>
</Body>
Second XML:
<Body>
<h1> </h1>
<h1>aaa</h1>
<h1>bbb</h1>
</Body>
XSL I'm using as
<xsl:strip-space elements="h1"/>
<xsl:template match="@*|node()">
<xsl:copy><xsl:apply-templ
</xsl:template>
<xsl:template match="Body">
<xsl:copy>
<h1><xsl:value-of select="h1"/></h1>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="h1"/>
Output I'm getting correctly for first XML as like:
<Body>
<h1>aaa bbb ccc</h1>
</Body>
But even I used strip-space element as h1 for the second xml, I'm getting like below:
<Body>
<h1> aaa bbb</h1>
</Body>
Expected output be like
<Body>
<h1>aaa bbb</h1>
</Body>
Need to remove the space element of h1. But due to that tag, its combining even the space. I used priority and used the seperate h1[normalize-space()] tag also in the template. But its not working. There is having any way to avoid this. Please suggest. I'm using XSLT 2.0 and saxon-PE 9.6.0.7. Thanks in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
the generic template for node() will pick up the h1 that does have value
ASKER
Hi Geert. Thanks. Its working fine for the below tag
<h1><xsl:value-of select="h1[normalize-space ()]" separator=" "/></h1>
<h1><xsl:value-of select="h1[normalize-space
ASKER
Thanks
let the apply-templates mechanism do the work and use predicate conditions
Open in new window
instead of
Open in new window