0

我有时在 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()])&gt;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中遇到此类问题的任何经验?感谢您的帮助!

罗伯特

4

1 回答 1

2

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

于 2011-04-30T22:09:58.240 回答