为了账号安全,请及时绑定邮箱和手机立即绑定

需要帮助使用 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 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号