Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 删除姓氏,但使用XSLT保留首字母_Php_Xslt - Fatal编程技术网

Php 删除姓氏,但使用XSLT保留首字母

Php 删除姓氏,但使用XSLT保留首字母,php,xslt,Php,Xslt,几天前我写了一篇文章,你可以在这里找到。我得到了PHP所需的答案,但现在我需要弄清楚如何使用xslt模板做同样的事情 由于hipaa的法律规定,我无法在评论中显示姓氏,因此我尝试保留姓氏,只显示姓氏的首字母 如果需要,这里是我的xml结构(data.xml): <item> <title>Carole Baskin left a 5 Star Review on Google</title> <description>Maecenas ullamc

几天前我写了一篇文章,你可以在这里找到。我得到了PHP所需的答案,但现在我需要弄清楚如何使用xslt模板做同样的事情

由于hipaa的法律规定,我无法在评论中显示姓氏,因此我尝试保留姓氏,只显示姓氏的首字母

如果需要,这里是我的xml结构(data.xml):

<item>
<title>Carole Baskin left a 5 Star Review on Google</title>
<description>Maecenas ullamcorper id eros nec dictum. Proin mattis ullamcorper nisl, id gravida tortor eleifend at. Fusce condimentum mauris non iaculis eleifend.</description>
</item>
<?php
   $xmlFile = "data.xml;
   $xslFile = "xsl.xml";
                        
   $doc = new DOMDocument();
   $xsl = new XSLTProcessor();
                    
   $doc->load($xslFile);
   $xsl = new XSLTProcessor();
   $xsl->registerPHPFunctions();
    
   $xsl->importStyleSheet($doc);
                    
   $doc->load($xmlFile);
   echo $xsl->transformToXML($doc);
?>

卡罗尔·巴斯金(Carole Baskin)在谷歌上留下了一篇五星评论
梅塞纳·乌拉姆科珀是一位爱神。Proin mattis ullamcorper nisl,身份证是孕妇侵权人。非艾库利埃利芬德葡萄酒调味品mauris non iaculis eleifend。
下面是我的xsl模板代码(xsl.xml)。select=“title”将显示名字和姓氏。例如:卡罗尔·巴斯金在谷歌上留下了一篇五星评论。我需要它说“Carole B在Google上留下了一篇五星评论”


  • 我用来加载xsl的PHP代码:

    <item>
    <title>Carole Baskin left a 5 Star Review on Google</title>
    <description>Maecenas ullamcorper id eros nec dictum. Proin mattis ullamcorper nisl, id gravida tortor eleifend at. Fusce condimentum mauris non iaculis eleifend.</description>
    </item>
    
    <?php
       $xmlFile = "data.xml;
       $xslFile = "xsl.xml";
                            
       $doc = new DOMDocument();
       $xsl = new XSLTProcessor();
                        
       $doc->load($xslFile);
       $xsl = new XSLTProcessor();
       $xsl->registerPHPFunctions();
        
       $xsl->importStyleSheet($doc);
                        
       $doc->load($xmlFile);
       echo $xsl->transformToXML($doc);
    ?>
    
    如果(如您在对另一个问题的评论中所说)姓氏始终是
    标题
    第一和第二空格之间的字符串,则可以替换:

    <xsl:value-of select="title"/>
    
    
    
    与:

    
    
    或者,如果您愿意:

    <xsl:variable name="head" select="substring-before(title, ' ')"/>
    <xsl:value-of select="substring(title, 1, string-length($head) + 2)"/>
    <xsl:text> </xsl:text>
    <xsl:value-of select="substring-after(substring-after(title, ' '), ' ')"/>
    
    
    
    非常感谢!正是我想做的:)对于XSLT问题,您需要说明您正在使用哪个版本的XSLT,因为这通常会影响答案。对于涉及字符串操作的问题尤其如此,在XSLT2.0+中,字符串操作可以使用正则表达式完成。