1 回答

TA貢獻1891條經驗 獲得超3個贊
我建議從選擇的謂詞開始,item例如item[description[normalize-space()]]僅選擇具有超過空白內容的子元素的item元素。description
至于字數統計,在 XPath 1.0 中表達起來比較困難。正如您似乎從 PHP 中執行的那樣,請檢查 PHP 是否公開了 libxslt 的 EXSLT 擴展函數,或者調用 PHP 來計算描述內容中的單詞數。
要將 PHP 的str_word_count函數與 XSLTProcessor 一起使用,您可以使用
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:php="http://php.net/xsl"
exclude-result-prefixes="php"
version="1.0">
<xsl:output method="html" encoding="utf-8" indent="yes"/>
<xsl:template match="/rss/channel">
<xsl:for-each select="item[description[normalize-space() and php:function('str_word_count', string()) < 100]]">
<li>
<p class="heading">
<xsl:value-of select="title"/>
</p>
<p class="text">
<xsl:value-of select="description"/>
</p>
</li>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
在你需要的 PHP 代碼中
$xsltProcessor = new XSLTProcessor();
$xsltProcessor->registerPHPFunctions();
我還認為您最好創建兩個不同的 DOMDocument 對象,一個用于 XML 輸入,另一個用于 XSLT 文檔。
- 1 回答
- 0 關注
- 116 瀏覽
添加回答
舉報