【问题标题】:Xslt does not work in php but saxon didXslt 在 php 中不起作用,但 saxon 可以
【发布时间】:2011-04-30 18:27:37
【问题描述】:

我有时在 Kernow 中使用 saxon 处理器测试我的 xslt 样式表。我总是使用 xslt 1.0,因为 simplexml 甚至 simpledom 只能执行 xslt 1.0。以下样式表在 php 中不起作用:

$tagsXml=simpledom_load_file('...xml');
    $dom_sxe=dom_import_simplexml($tagsXml);
    $dom = new DOMDocument('1.0');
    $dom_sxe = $dom->importNode($dom_sxe, true);
    $dom_sxe = $dom->appendChild($dom_sxe); 
    $proc = new XSLTProcessor();
    $xsl = new DOMDocument;
    $xsl->load('...xslt');
    $proc->importStylesheet($xsl);
    $newXml = $proc->transformToXml($dom);

这就是样式表是php抛出一个编译错误,无法编译when语句<xsl:when test="count($ctag/ancestor::*[local-name()=current()/local-name() and text()=current()/text()]|$ctag/descendant::*[local-name()=current()/local-name() and text()=current()/text()])>0">

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" exclude-result-prefixes="xs">

<xsl:output method="xml"/>
<xsl:output indent="no"/> 
<xsl:output omit-xml-declaration="yes"/>

<xsl:template match="tag">

</xsl:template>

<xsl:template match="related">
    <xsl:for-each select="descendant::*[name()!='right' and name()!='rating' and name()!='id' and name()!='geo']">
        <xsl:variable name="ctag" select="."/>

        <xsl:variable name="memxlvl" select="count(//*[max(count(ancestor-or-self::*)) and count(ancestor-or-self::*[.=current()/.]) &gt;0]/ancestor-or-self::*)"/>
        <xsl:variable name="melvl" select="count(current()/ancestor::*)"/>

        <!--<xsl:value-of select="$memxlvl - $melvl"/>-->
        <!--<xsl:value-of select="(($memxlvl - $melvl) >= 0)*($memxlvl - $melvl) - not(($memxlvl - $melvl) >= 0)*($memxlvl - $melvl)"/>-->

        <xsl:for-each select="current()/ancestor::related/descendant::*[text()!=current()/text() and name()!='right' and name()!='rating' and name()!='id' and name()!='geo']">
                <tagconn me="{$ctag/text()}" friend="{./text()}">

                <xsl:choose>
                <xsl:when test="count($ctag/ancestor::*[local-name()=current()/local-name() and text()=current()/text()]|$ctag/descendant::*[local-name()=current()/local-name() and text()=current()/text()])&gt;0">
                        <xsl:variable name="fmxlvl" select="count(//*[max(count(ancestor-or-self::*)) and count(ancestor-or-self::*[.=current()/.]) &gt;0]/ancestor-or-self::*)"/>
                        <xsl:variable name="flvl" select="count(current()/ancestor::*)"/>
                <xsl:value-of select="((($memxlvl - $melvl)-($fmxlvl - $flvl)) >= 0)*(($memxlvl - $melvl)-($fmxlvl - $flvl)) - not((($memxlvl - $melvl)-($fmxlvl - $flvl)) >= 0)*(($memxlvl - $melvl)-($fmxlvl - $flvl))"/>         
                </xsl:when>
                <xsl:otherwise>
                        <xsl:variable name="fmxlvl" select="count(//*[max(count(ancestor-or-self::*)) and count(ancestor-or-self::*[.=current()/.]) &gt;0]/ancestor-or-self::*)"/>
                        <xsl:variable name="flvl" select="count(current()/ancestor::*)"/>
                        <xsl:value-of select="((($memxlvl - $melvl)-($fmxlvl - $flvl)) >= 0)*(($memxlvl - $melvl)-($fmxlvl - $flvl)) - not((($memxlvl - $melvl)-($fmxlvl - $flvl)) >= 0)*(($memxlvl - $melvl)-($fmxlvl - $flvl))+((($memxlvl - $melvl) >= ($fmxlvl - $flvl)))*($memxlvl - $melvl) - not((($memxlvl - $melvl) &lt; ($fmxlvl - $flvl)))*(($fmxlvl - $flvl))"/>            

                </xsl:otherwise>
                </xsl:choose>
             </tagconn>
        </xsl:for-each>     
    </xsl:for-each>
</xsl:template>

<xsl:template match="alltags">
<items>
    <xsl:apply-templates select="descendant::related"/>
</items>
</xsl:template>
</xsl:stylesheet>

我知道该模板并不完美且不易理解。在php中遇到此类问题的任何经验?感谢您的帮助!

罗伯特

【问题讨论】:

  • 测试对我来说看起来不错,也许是转义字符的问题。不如把&amp;gt; 换成!= 看看是不是这样?
  • 好吧,我尝试了这样的逃生场景,但没有成功!谢谢你的帮助..

标签: php xslt saxon


【解决方案1】:

语法 current()/local-name() 特定于 XSLT 2.0。在 XSLT 1.0 中,您需要编写 local-name(current())。请注意,指定 xsl:version="1.0" 并不能保证您的样式表符合 XSLT 1.0。

【讨论】:

  • OP 也使用 XPath 2.0 fn:max() 函数。
猜你喜欢
  • 2021-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-09
相关资源
最近更新 更多