2017-08-07 35 views
0

这是可能使用XSL我需要用一个模板

输入XML使用,去除的H#序列号删除所有 系列标签:

<table> 
    <tbody> 
     <tr> 
     <td> 
      <h2> 
      <img src="https://admin.com" /> 
      </h2> 
     </td> 
     <td> 
      <h3> 
      <img src="https://admin.com" /> 
      </h3> 
     </td> 
     <td> 
      <h4> 
      <img src="https://admin.com" /> 
      </h4> 
     </td> 
     </tr> 
    </tbody> 
    </table> 

XSL我使用:

<xsl:template match="table/tbody/tr/td/h2[img]"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="table/tbody/tr/td/h3[img]"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="table/tbody/tr/td/h4[img]"> 
    <xsl:apply-templates/> 
</xsl:template> 

就像在所有输入很多h#将在表中可能。是否可以编写单个模板以从表格中删除所有h1 h2 h3 ....... h系列?

回答

1

您可以在示例模板中使用多个pattern,例如<xsl:template match="table/tbody/tr/td/h2[img] | table/tbody/tr/td/h3[img] | table/tbody/tr/td/h4[img]"><xsl:apply-templates/></xsl:template>

我不会建议使用正则表达式,如果你想缩短模式,那么也许做

<xsl:template match="table/tbody/tr/td/*[(self::h1 | self::h2 | self::h3 | self::h4 | self::h5 | self::h6) and img]"> 
    <xsl:apply-templates/> 
</xsl:template> 
+0

嗨@马丁。谢谢。这可能使用任何正则表达式为该h系列数字删除。请建议? – User501

+0

我不确定在模式中使用正则表达式来匹配不同的元素名称是个不错的主意,您当然可以尝试'* [match(name(),'^ h(1 | 2 | 3 | 4 | 5 | 6)$'']]'但为了清楚起见y的效率可能会更好地说明元素名称在步骤模式。 –

+0

感谢@Martin。它的工作正常。 – User501