HTMLから特定のタグや属性を除去する XSLT
問題
HTMLから特定のタグ、特定の属性、特定のタグと属性の組み合わせなどを除去したい。
(ある場面で許可されていない属性やタグを除去したい)

答え
例えば XSLT(XSL変換)で。
以下のような 1.html があるとする(1行目のmetaタグは日本語を使う兼ね合いで文字コード判定のため付与してやってください)。
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <div class="hoge" id="hogehoge"> <p class="fuga" style="text-align:center;"> これは、 <span style="font-size:100px;">テスト</span> です。 </p> </div>
以下のような 1.xsl があるとする。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@style">
</xsl:template>
<xsl:template match="@*">
<xsl:copy></xsl:copy>
</xsl:template>
</xsl:stylesheet>
以下のように変換すると
<?php
$htmlstr = file_get_contents('1.html');
$xslstr = file_get_contents('1.xsl');
$xml = new DOMDocument;
$xml->loadHTML($htmlstr);
$xsl = new DOMDocument;
$xsl->loadXML($xslstr);
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML($xml);
結果はこうなる(style属性を除去できた)
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> <body><div class="hoge" id="hogehoge"> <p class="fuga"> これは、 <span>テスト</span> です。 </p> </div></body> </html>
他の例。
特定のタグを、タグの中身も一緒に除去する(spanタグを除去)
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="span">
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
↓
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> <body><div class="hoge" id="hogehoge"> <p class="fuga" style="text-align:center;"> これは、 です。 </p> </div></body> </html>
特定のタグを、タグの中身はそのままで、タグだけ除去する(pタグだけ除去)。
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="p">
<xsl:apply-templates select="node()"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
↓
<html> <head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head> <body><div class="hoge" id="hogehoge"> これは、 <span style="font-size:100px;">テスト</span> です。 </div></body> </html>
あるタグにある属性以外が使われていたら、属性を除去する(span要素のstyle属性だけ削除)
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:apply-templates select="/html/body/div" /> </xsl:template> <xsl:template match="*"> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:copy></xsl:copy> </xsl:template> <xsl:template match="span/@style"> </xsl:template> </xsl:stylesheet>
↓
<?xml version="1.0"?> <div class="hoge" id="hogehoge"> <p class="fuga" style="text-align:center;"> これは、 <span>テスト</span> です。 </p> </div>