亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要幫助使用 xsl:choose 和 xsl/when 選擇 XSLT

需要幫助使用 xsl:choose 和 xsl/when 選擇 XSLT

PHP
慕雪6442864 2023-07-15 16:46:38
我的 XSLT 語言不好 - 所以任何幫助將不勝感激!我正在嘗試從下面的 XML 文件 (file.xml) 中選擇評論,該評論的描述數量少于 100 個單詞,如果缺少描述,則根本不要選擇它(您會注意到第二項)在 xml 中缺少描述)我搞亂了xsl:choose和xsl/when但似乎仍然無法正常工作。使用 PHP 加載 XML 文件$xmlFile = 'file.xml';                   $doc = new DOMDocument();$xsl = new XSLTProcessor();                    $doc->load($xslFile);$xsl->importStyleSheet($doc);                    $doc->load($xmlFile);echo $xsl->transformToXML($doc);這是我的 XML (file.xml):<?xml version="1.0" encoding="UTF-8" ?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">    <channel>        <title>Reviews</title>        <description>5 Star Reviews</description>        <link></link>        <item>            <title>Google review 4 stars - John</title>            <description>Lorem ipsum dolor sit amet, dico quaestio eu vis. Errem disputationi mel te, in civibus minimum qualisque vel. </description>        </item>        <item>            <title>Google review 5 stars - Sarah</title>            <description></description>        </item>        <item>            <title>Google review 5 stars - Jenny</title>            <description>I love this place! Lorem ipsum dolor sit amet, dico quaestio eu vis. Errem disputationi mel te, in civibus minimum qualisque vel. Et duo quando detracto tacimates, mediocrem instructior id pro. Nec id omnis aperiri iracundia,</description>        </item>    </channel></rss>XSLT 文件<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="utf-8" indent="no"/><xsl:template match="/rss/channel">    <xsl:for-each select="item">        <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>我的最終輸出應該是這樣的。
查看完整描述

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()) &lt; 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 文檔。


查看完整回答
反對 回復 2023-07-15
  • 1 回答
  • 0 關注
  • 116 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號