Php 需要使用xsl:choose和xsl/when选择XSLT的帮助吗

Php 需要使用xsl:choose和xsl/when选择XSLT的帮助吗,php,xslt,Php,Xslt,我的XSLT语言不好-因此,任何帮助都将不胜感激!我试图从下面的XML文件file.XML中选择描述计数小于100个单词的评论,如果缺少描述,也不要选择它。您会注意到XML中缺少描述的第二项 我把xsl:choose和xsl/when弄得一团糟,但似乎仍然无法正确地工作 使用PHP加载XML文件 这是我的XML file.XML: XSLT文件 我的最终输出应该是这样的 谷歌评论四星-约翰 这是一个很好的例子。以civibus最低质量等级进行的争议 谷歌评论五星-珍妮 我喜欢这个地方!这是一个很

我的XSLT语言不好-因此,任何帮助都将不胜感激!我试图从下面的XML文件file.XML中选择描述计数小于100个单词的评论,如果缺少描述,也不要选择它。您会注意到XML中缺少描述的第二项

我把xsl:choose和xsl/when弄得一团糟,但似乎仍然无法正确地工作

使用PHP加载XML文件

这是我的XML file.XML:

XSLT文件

我的最终输出应该是这样的

谷歌评论四星-约翰 这是一个很好的例子。以civibus最低质量等级进行的争议

谷歌评论五星-珍妮
我喜欢这个地方!这是一个很好的例子。以civibus最低质量等级进行的争议。他是一个平庸的教育家。Nec id omnis aperiri iracundia,

我建议从项目选择的谓词开始,例如,项目[description[normalize space]]仅选择具有超过空白内容的描述子元素的项目元素

至于计算单词,这在XPath1.0中更难表达。正如您在PHP中所做的那样,请检查PHP是否公开了libxslt的EXSLT扩展函数,或者调用PHP并计算描述内容中的字数

要在XSLTProcessor中使用PHP的str_word_count函数,可以使用

<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>

我还认为最好创建两个不同的DOMDocument对象,一个用于XML输入,另一个用于XSLT文档。

不能将regex与EXSLT str:split一起使用。表达式str:split.,“\W+”查找文本字符串\W+。-我不认为100字的限制需要从字面上理解;IMHO,计算空间应该足够了。这不需要扩展就可以做到。@michael.hor257k,好吧,很抱歉,对参数的模式的描述让我假设我可以加入正则表达式模式。将编辑为使用PHP函数。@MartinHonnen完美!非常感谢你!我非常感谢你的帮助:
<?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>
<?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>
<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>
$xsltProcessor = new XSLTProcessor();

$xsltProcessor->registerPHPFunctions();