简体   繁体   English

XSLTProcessor问题

[英]XSLTProcessor issues

I have the following XML for my XSLT stylesheet (take note of the table-number parameter: 我的XSLT样式表具有以下XML(请注意table-number参数:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:tet="http://www.pdflib.com/XML/TET3/TET-3.0">
    <xsl:output method="text" />
    <xsl:param name="table-number" select="1" />
    <xsl:param name="page-number" select="0" />
    <xsl:param name="separator-char" select="','" />
    <xsl:variable name="double-quote">"</xsl:variable>

    <xsl:template match="/">
        <xsl:if test="tet:TET/tet:Document/tet:Pages/tet:Page/tet:Content[not(@granularity = 'word' or @granularity = 'page')]">
            <xsl:message terminate="yes">
                <xsl:text>Stylesheet table.xsl processing TETML for document '</xsl:text>
                <xsl:value-of select="tet:TET/tet:Document/@filename" />
                <xsl:text>': this stylesheet requires word info in TETML.</xsl:text>
                <xsl:text>Create the input in page mode "word" or "wordplus".</xsl:text>
            </xsl:message>
        </xsl:if>

        <xsl:choose>
            <xsl:when test="$page-number = 0">
                <xsl:variable name="table" select="(tet:TET/tet:Document/tet:Pages/tet:Page//tet:Table)[$table-number]"/>
                <xsl:if test="count($table) = 0">
                    <xsl:call-template name="table-not-found" />
                </xsl:if>
                <xsl:apply-templates select="$table" />
            </xsl:when>
            <xsl:otherwise>
                <xsl:variable name="table" select="(tet:TET/tet:Document/tet:Pages/tet:Page[@number = $page-number]//tet:Table)[$table-number]"/>
                <xsl:if test="count($table) = 0">
                    <xsl:call-template name="table-not-found" />
                </xsl:if>
                <xsl:apply-templates select="$table" />
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>
    .
    .
    .
</stylesheet>

And the following PHP code executes the XSLT on a given input file of XML: 下面的PHP代码在给定的XML输入文件上执行XSLT:

protected function tetml_to_csv() {
    $processor = new XSLTProcessor();

    $xsl = new DOMDocument();
    $xsl->load(realpath('./path/to/xslt/tetml_to_csv.xsl'), LIBXML_NOCDATA);

    $processor->importStylesheet($xsl);

    $tetml = file_get_contents($this->tetmlDoc);

    $tetml = new DOMDocument();
    $tetml->load($this->tetmlDoc);

    //var_dump($processor->getParameter('xsl', 'table-number'));
    //exit();
    $processor->setParameter(NULL, 'table-number', 1);

    file_put_contents($this->filepath . 'output.csv',  $processor->transformToXml($tetml));

    unlink($this->tetmlDoc);
}

When I run the code without calling setParameter() the XSLT places only the first table from the input XML into the output .csv file (which is the expected behavior). 当我运行代码而不调用setParameter() ,XSLT仅将输入XML中的第一个表放到输出.csv文件中(这是预期的行为)。

However, when I call setParameter() prior to executing the conversion, ALL tables from the input XML file are transferred to the output .csv file, even if I set the value of the parameter to the default value. 但是,当我在执行转换之前调用setParameter()时,即使我将参数的值设置为默认值,来自输入XML文件的所有表也​​会转移到输出.csv文件。

When I call getParameter() the function returns false (even though the return value is supposed to be either the value of the parameter, or null if the parameter isn't found). 当我调用getParameter()该函数返回false (即使返回值应该是参数的值,如果找不到该参数,则返回null )。

I tried changing the value for namespace that I pass to the functions, but that didn't help at all. 我尝试更改传递给函数的namespace的值,但这根本没有帮助。

What am I doing wrong? 我究竟做错了什么? It must be something simple, but I can't for the life of me figure it out. 它一定很简单,但是我无法终生解决。

EDIT 编辑

I have tried passing the value parameter for setParameter() as both a string and a numeric; 我尝试将setParameter()value参数作为字符串和数字传递; both had the same result. 两者都有相同的结果。

I don´t know the API but http://php.net/manual/en/xsltprocessor.setparameter.php suggests the argument passed in by PHP is a string so perhaps when you do $processor->setParameter(NULL, 'table-number', 1); 我不知道该API,但http://php.net/manual/zh/xsltprocessor.setparameter.php建议PHP传递的参数是一个字符串,所以也许当您执行$processor->setParameter(NULL, 'table-number', 1); the number 1 is passed as a string to XSLT and in that case the positional predicates in XSLT don't work unless you explicitly write them as [position() = $table-number] . 数字1作为字符串传递给XSLT,在这种情况下,除非您明确将它们写为[position() = $table-number]否则XSLT中的位置谓词将不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM