2017-08-07 46 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

相關問題