带有嵌套表的XSLT中的数据对齐问题 [英] Problems with data alignment in XSLT with nested tables

查看:69
本文介绍了带有嵌套表的XSLT中的数据对齐问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用XSLT和嵌套表通过FoundationPHP生成Word文档.

I'm generating a Word document via FoundationPHP using XSLT and nested tables.

我想要的结果是:

Table 1        Table 2
Image 1        Image 2
Label 1        Label 2
Data 1         Data 2

Table 3        Table 4
Image 3        Image 4
Label 3        Label 4
Data 3         Data 4

Table 5
Image 5
Label 5
Data 5

但是,我得到的是:

Table 1        Table 2
Image 1        Image 2
Label 1        Label 2
Label 1        Label 4

Table 3        Table 4
Image 3        Image 4
Label 3        Label 4
Data 2        Label 5

Table 5
Image 5
Label 5
Data 3

我的XSLT代码(为发布目的而简化)

My XSLT code (simplified for posting purposes)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:w="http://some.bla">      <!-- Namespace of 'w' added by edit -->
    <xsl:template match="/">
        <w:document>             <!-- Added by edit !!! -->
            blah
            <w:body>
                <w:tbl>
                    <xsl:variable name="size" select="count(root/row)"/>

                    <xsl:for-each name="count" select="root/row[ceiling($size div 2) &gt;= position()]">
                        <xsl:variable name="level0Count" select="position()"/>
                        <xsl:variable name="imageid" select="position()+1"/>
                        <xsl:variable name="level1Count" select="position() - 1"/>
                        <xsl:variable name="level2Count" select="$level1Count * 2 + position()"/>
                        <w:tr w:rsidR="003346C6" w:rsidTr="003346C6">
                            <w:tc>
                                <w:tcPr>
                                    <w:tcW w:w="4648" w:type="dxa"/>
                                </w:tcPr>
                                <w:tbl>
                                    <w:tr w:rsidR="003346C6" w:rsidTr="00BD1383">
                                        <w:tc>
                                            <w:p w:rsidR="003346C6" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                <w:r>
                                                    Image 1
                                                </w:r>
                                            </w:p>
                                        </w:tc>
                                    </w:tr>
                                    <w:tr w:rsidR="003346C6" w:rsidTr="00BD1383">
                                        <w:tc>
                                            <w:p w:rsidR="003346C6" w:rsidRPr="0004412A" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                <w:r w:rsidRPr="0004412A">
                                                    <xsl:choose>
                                                        <xsl:when test="position()=1">
                                                            <w:t>Label <xsl:value-of select="position()"/>
                                                                <w:b />
                                                            </w:t>
                                                        </xsl:when>
                                                        <xsl:otherwise>
                                                            <w:t>Label <xsl:value-of select="2*position()-1"/>
                                                                <w:b />
                                                            </w:t>
                                                        </xsl:otherwise>
                                                    </xsl:choose>
                                                </w:r>
                                            </w:p>
                                            <w:p w:rsidR="003346C6" w:rsidRPr="005F3377" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                <w:r>
                                                    <w:t>
                                                        <xsl:value-of select="Data"/>
                                                    </w:t>
                                                </w:r>
                                                <w:bookmarkStart w:id="0" w:name="_GoBack"/>
                                                <w:bookmarkEnd w:id="0"/>
                                            </w:p>
                                        </w:tc>
                                    </w:tr>
                                </w:tbl>
                                <w:p w:rsidR="003346C6" w:rsidRDefault="003346C6" w:rsidP="003346C6"/>
                            </w:tc>
                            <xsl:if test="following::row[ceiling($size div 2)]/data!=''">
                                <w:tc>
                                    <w:tcPr>
                                        <w:tcW w:w="4648" w:type="dxa"/>
                                    </w:tcPr>
                                    <w:tbl>
                                        <w:tr w:rsidR="003346C6" w:rsidTr="00BD1383">
                                            <w:tc>
                                                <w:p w:rsidR="003346C6" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                    <w:r>
                                                        Image 2
                                                    </w:r>
                                                </w:p>
                                            </w:tc>
                                        </w:tr>
                                        <w:tr w:rsidR="003346C6" w:rsidTr="00BD1383">
                                            <w:tc>
                                                <w:p w:rsidR="003346C6" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                    <w:r>
                                                        <xsl:choose>
                                                            <xsl:when test="position()=1">
                                                                <w:t>
                                                                    Label <xsl:value-of select="position()+1"/>
                                                                </w:t>
                                                            </xsl:when>
                                                            <xsl:otherwise>
                                                                <xsl:if test="following::row[ceiling($size div 2)]/data!=''">
                                                                    <w:t>
                                                                        Label <xsl:value-of select="2*position()"/>
                                                                    </w:t>
                                                                </xsl:if>
                                                            </xsl:otherwise>
                                                        </xsl:choose>
                                                    </w:r>
                                                </w:p>
                                                <w:p w:rsidR="003346C6" w:rsidRPr="005F3377" w:rsidRDefault="003346C6" w:rsidP="003346C6">
                                                    <w:r>
                                                        <w:t>
                                                            <xsl:value-of select="following::row[ceiling($size div 2)]/data"/>
                                                        </w:t>
                                                    </w:r>
                                                </w:p>
                                            </w:tc>
                                        </w:tr>
                                    </w:tbl>
                                    <w:p w:rsidR="003346C6" w:rsidRDefault="003346C6" w:rsidP="003346C6"/>
                                </w:tc>

                            </xsl:if>
                        </w:tr>
                    </xsl:for-each>
                </w:tbl>
                <w:p w:rsidR="00A00BC9" w:rsidRDefault="00A00BC9"/>
            </w:body>
        </w:document>
    </xsl:template>
</xsl:stylesheet>

如您所见,图像和标签是正确的,但是数据是垂直输入的.我一直在盯着这段代码,以至于我的双重视野使我无法再猜测一切.

As you can see, the image and the label are correct, however the data is coming in vertically. I've been staring at this code to the point where my double vision is making me second guess everything.

这是我脚本的XML输出. (我只包含3行)

This is the XML output from my script. (I only included 3 rows)

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <row>
        <id>10755</id>
        <image>10755/11786.jpg</image>
        <data> 8220 Southwest Warm Springs Street</data>
    </row>
    <row>
        <id>10493</id>
        <image>10493/11786.jpg</image>
        <data> 21101-21149 SW 115th Avenue</data>
    </row>
    <row>
        <id>11008</id>
        <image>11008/11786.jpg</image>
        <data> 7144 NW Progress Court</data>
    </row>
</root>

为使问题更完整,下面是获取数据的代码:

To make the question more complete, here is the code to get the data:

if ( isset( $_GET[ 'id' ] ) ) {
    $sql = "SELECT ID, data, image FROM property WHERE ID in (" . $_GET['id'] .") ORDER BY field(ID," . $_GET['id'] .")";
    $result = $db->query($sql);
    if ($db->error) {
        $error = $db->error;
    }
}
}

function getRow($result) {
    return $result->fetch_assoc();
}

推荐答案

在您第一次调用数据时,请更改为

In your first call to data, change to

<xsl:choose>
  <xsl:when test="position()=1">
    <w:t><xsl:value-of select="data"/></w:t>
  </xsl:when>
  <xsl:otherwise>
    <w:t><xsl:value-of select="following::row[$imageid - 2]/data"/></w:t>
  </xsl:otherwise>
</xsl:choose>

然后在第二个通话中使用

Then, on your second call, use

    <w:t><xsl:value-of select="following::row[$imageid - 1]/data"/></w:t>

这应该按照您想要的方式排列数据.

This should line up the data the way you want.

这篇关于带有嵌套表的XSLT中的数据对齐问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆