Is it possible in XSLT to find a specific tag and replace it with its contents? For example, given this XML:
<span>Hello world</span>
we’d end up with this:
Hello world
So the useless and redundant SPAN tag is replaced with its contents, at any level (recursively). We’d want to find the “naked” span tags (the ones with no attributes) and replace them with their content.
I’m processing XML over which I don’t have control. Thanks.
Update: Here’s what the *.XSL file contains, followed by sample output:
<xsl:stylesheet
version="1.0"
exclude-result-prefixes="x d xsl msxsl cmswrt"
xmlns:x="http://www.w3.org/2001/XMLSchema"
xmlns:d="http://schemas.microsoft.com/sharepoint/dsp"
xmlns:cmswrt="http://schemas.microsoft.com/WebParts/v3/Publishing/runtime"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:param name="ItemsHaveStreams">
<xsl:value-of select="'False'" />
</xsl:param>
<xsl:variable name="OnClickTargetAttribute" select="string('javascript:this.target="_blank"')" />
<xsl:variable name="ImageWidth" />
<xsl:variable name="ImageHeight" />
<xsl:template name="Contact" match="Row[@Style='Contact']" mode="itemstyle">
<div class="outer-container">
<table border="0" cellspacing="0" width="100%">
<tr>
<td class="ms-vb" style="text-align:left; padding:9px;">
<span style="font-weight:bold; border-bottom:1px solid #999;"><xsl:value-of select="@Title"/></span>
<!-- Phone and Emergency Phone -->
<xsl:if test="@Phone != '' or @EmergencyPhone != ''">
<xsl:if test="@Phone != ''">
<xsl:value-of select="@Phone" disable-output-escaping="yes"/><br />
</xsl:if>
<xsl:if test="@EmergencyPhone != ''">
<xsl:value-of select="@EmergencyPhone" disable-output-escaping="yes"/>
</xsl:if>
</xsl:if>
<!-- Email -->
<xsl:if test="@Email != ''">
<span style="text-align:left">E-mail: <a href="mailto:{@Email}"><xsl:value-of select="@Email"/></a></span>
</xsl:if>
<!-- Address & Map -->
<!--
Must test for both empty string and empty div tags, escaped.
-->
<xsl:if test="@Address != '' and @Address !='<div></div>'">
<p>Address: <xsl:value-of select="@Address" disable-output-escaping="yes"/></p>
</xsl:if>
<xsl:if test="@Map != ''">
(<a href="{@Map}">MAP</a>)
</xsl:if>
<!-- Opening Hours -->
<xsl:if test="@OpeningHours != ''">
<p><b>Opening Hours:</b></p>
<xsl:value-of select="@OpeningHours" disable-output-escaping="yes"/>
</xsl:if>
</td>
</tr>
</table>
</div>
</xsl:template>
</xsl:stylesheet>
Here’s a sample output currently:
Contact Health Services
962-8328
962-8945
Emergency only: 962-8884
After hours, contact Security Dispatch to connect with Health Services staff on duty.
E-mail: health@mydomain.org
Address:
123 Main St.
(MAP)
Opening Hours:
Sunday -Thursday 08:00 -17:30
This XPath will find all
spanelements without attributes: