在XSLT中是否可以找到特定的标签并将其替换为其内容?例如,给定这个XML:
<span>Hello world</span>
我们最终得到了这个:
Hello world
因此无用且冗余的SPAN标记在任何级别(递归地)都被其内容替换。我们想找到“裸”的span标签(没有属性的标签)并用它们的内容替换它们。
我正在处理我无法控制的XML。感谢。
更新:这是* .XSL文件包含的内容,后跟示例输出:
<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>
以下是目前的示例输出:
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
答案 0 :(得分:2)
此XPath将找到没有属性的所有span
元素:
//span[not(@*)]
<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="span[not(@*)]">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>