2011-08-14 52 views
2

XSLT可以找到特定标记并将其替换为其内容吗?例如,鉴于此XML:XSLT - 搜索并查找特定标记

<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=&quot;_blank&quot;')" /> 
    <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 !='&lt;div&gt;&lt;/div&gt;'"> 
       <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: [email protected] 

Address: 
123 Main St. 

(MAP) 

Opening Hours: 
Sunday -Thursday  08:00 -17:30 

回答

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> 
+0

谢谢,基里尔。你让它看起来这么容易!是否有可能对上面的输出做其他XSLT?例如,如果我需要应用样式等,我有一个自定义模板,会在上面的代码后面(在关闭样式表标签内)? – Alex

+1

@亚历山大,不客气。你能提供样本XML和样本输出XML吗? (更新您的问题) –

+0

Kirill,请参阅上面更新的问题。谢谢。 – Alex