我只想在 XML/XSLT 文档中第一次出现节点。 XPath 返回所有甚至 [1]

I only want the first occurrence of a node in XML/XSLT document. XPath is returning all even with [1]

我只需要 select 以下 XSL-FO 模板中第一次出现的 <fo:table ...> 节点,但 xPath 正在返回所有这些节点。这是我在使用所有名称空间创建包装器元素后使用的 XPath 语句:

<fo:wrapper xmlns:fo="http://www.w3.org/1999/XSL/Format"
    xmlns:j4luserext="xalan://com.java4less.xreport.fop.XLSTDummyExtension"
    xmlns:j4lext="xalan://com.java4less.xreport.fop.XLSTExtension"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:j4lif="http://java4less.com/fop/iform">

    {xsl:stylesheet/xsl:template/fo:root/fo:page-sequence/fo:flow/fo:table/
        fo:table-body/fo:table-row/fo:table-cell/fo:block/fo:table[1]}
</fo:wrapper>

这是 XSL-FO 输入:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:j4lif="http://java4less.com/fop/iform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:j4lext="xalan://com.java4less.xreport.fop.XLSTExtension"
extension-element-prefixes="j4lext j4luserext"
xmlns:j4luserext="xalan://com.java4less.xreport.fop.XLSTDummyExtension">
<xsl:template match="/">
    <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
        <fo:layout-master-set>
            <fo:simple-page-master master-name="master0"
                page-width="21.0cm" page-height="29.7cm" margin-top="2.0cm"
                margin-bottom="2.0cm" margin-left="2.0cm" margin-right="2.0cm">
                <fo:region-body region-name="body0" margin-top="1.5cm"
                    margin-bottom="1.5cm" />
                <fo:region-before region-name="header0" extent="1.5cm" />
                <fo:region-after region-name="footer0" extent="1.5cm" />
            </fo:simple-page-master>
        </fo:layout-master-set>
        <fo:page-sequence master-reference="master0">
            <fo:static-content flow-name="header0">
                <!-- 846098b14a344ae29ebeb7d3c3ae73c0 -->
                <fo:table width="17.0cm">
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="1.5cm">
                            <fo:table-cell>
                                <fo:table>
                                    <fo:table-column column-width="17.0cm" />
                                    <fo:table-body>
                                        <fo:table-row background-color="#ffffff" height="1.5cm">
                                            <fo:table-cell number-columns-spanned="1">
                                                <fo:block></fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </fo:table-body>
                                </fo:table>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:static-content>
            <fo:static-content flow-name="footer0">
                <!-- b468381816be4ef8bb31f448a8bf5b09 -->
                <fo:table width="17.0cm">
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="1.5cm">
                            <fo:table-cell>
                                <fo:table>
                                    <fo:table-column column-width="17.0cm" />
                                    <fo:table-body>
                                        <fo:table-row background-color="#ffffff" height="1.5cm">
                                            <fo:table-cell number-columns-spanned="1">
                                                <fo:block></fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </fo:table-body>
                                </fo:table>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:static-content>
            <fo:flow flow-name="body0">

                <!-- START Area Header -->
                <!-- 4ce697eac861472391f5eac35a51db48 -->
                <fo:table width="17.0cm">
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="1.5cm">
                            <fo:table-cell>
                                <fo:table>
                                    <fo:table-column column-width="17.0cm" />
                                    <fo:table-body>
                                        <fo:table-row background-color="#ffffff" height="1.5cm">
                                            <fo:table-cell number-columns-spanned="1">
                                                <fo:block></fo:block>
                                            </fo:table-cell>
                                        </fo:table-row>
                                    </fo:table-body>
                                </fo:table>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block>

                                    <!-- START Area Detail -->
                                    <!-- 7f22844da7e94220b5877bf7593e1dcb -->
                                    <fo:table width="17.0cm">
                                        <fo:table-column column-width="17.0cm" />
                                        <!-- 63bbf10b61f44321913cffd695225a53 -->
                                        <fo:table-header>
                                            <fo:table-row background-color="#ffffff"
                                                height="2.0cm">
                                                <fo:table-cell>
                                                    <fo:table>
                                                        <fo:table-column column-width="17.0cm" />
                                                        <fo:table-body>
                                                            <fo:table-row background-color="#ffffff"
                                                                height="2.0cm">
                                                                <fo:table-cell number-columns-spanned="1">
                                                                    <fo:block></fo:block>
                                                                </fo:table-cell>
                                                            </fo:table-row>
                                                        </fo:table-body>
                                                    </fo:table>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-header>
                                        <fo:table-body>
                                            <fo:table-row background-color="#ffffff"
                                                height="4.0cm">
                                                <fo:table-cell>
                                                    <fo:table>
                                                        <fo:table-column column-width="17.0cm" />
                                                        <fo:table-body>
                                                            <fo:table-row background-color="#ffffff"
                                                                height="4.0cm">
                                                                <fo:table-cell number-columns-spanned="1">
                                                                    <fo:block></fo:block>
                                                                </fo:table-cell>
                                                            </fo:table-row>
                                                        </fo:table-body>
                                                    </fo:table>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-body>
                                    </fo:table>

                                    <!-- END Area Detail -->
                                </fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                        <fo:table-row>
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block>

                                    <!-- START Area Footer -->
                                    <!-- ae182bb24f5648da9653ed5b997121c4 -->
                                    <fo:table width="17.0cm">
                                        <fo:table-column column-width="17.0cm" />
                                        <fo:table-body>
                                            <fo:table-row background-color="#ffffff"
                                                height="1.5cm">
                                                <fo:table-cell>
                                                    <fo:table>
                                                        <fo:table-column column-width="17.0cm" />
                                                        <fo:table-body>
                                                            <fo:table-row background-color="#ffffff"
                                                                height="1.5cm">
                                                                <fo:table-cell number-columns-spanned="1">
                                                                    <fo:block></fo:block>
                                                                </fo:table-cell>
                                                            </fo:table-row>
                                                        </fo:table-body>
                                                    </fo:table>
                                                </fo:table-cell>
                                            </fo:table-row>
                                        </fo:table-body>
                                    </fo:table>

                                    <!-- END Area Footer -->
                                </fo:block>
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>

                <!-- END Area Header -->
                <fo:block id="last-page" />
            </fo:flow>
        </fo:page-sequence>
    </fo:root>
</xsl:template>
</xsl:stylesheet>

这是我期望的输出:

<fo:wrapper xmlns:j4lif="http://java4less.com/fop/iform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:j4lext="xalan://com.java4less.xreport.fop.XLSTExtension"
xmlns:j4luserext="xalan://com.java4less.xreport.fop.XLSTDummyExtension"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:table width="17.0cm">
    <fo:table-column column-width="17.0cm" />
    <!-- 63bbf10b61f44321913cffd695225a53 -->
    <fo:table-header>
        <fo:table-row background-color="#ffffff" height="2.0cm">
            <fo:table-cell>
                <fo:table>
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="2.0cm">
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block />
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:table-cell>
        </fo:table-row>
    </fo:table-header>
</fo:table> 
</fo:wrapper>

这是 BaseX 和 Camel xQuery 组件实际输出的内容:

<fo:wrapper xmlns:j4lif="http://java4less.com/fop/iform"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:j4lext="xalan://com.java4less.xreport.fop.XLSTExtension"
xmlns:j4luserext="xalan://com.java4less.xreport.fop.XLSTDummyExtension"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:table width="17.0cm">
    <fo:table-column column-width="17.0cm" />
    <!-- 63bbf10b61f44321913cffd695225a53 -->
    <fo:table-header>
        <fo:table-row background-color="#ffffff" height="2.0cm">
            <fo:table-cell>
                <fo:table>
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="2.0cm">
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block />
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:table-cell>
        </fo:table-row>
    </fo:table-header>
    <fo:table-body>
        <fo:table-row background-color="#ffffff" height="4.0cm">
            <fo:table-cell>
                <fo:table>
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="4.0cm">
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block />
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:table-cell>
        </fo:table-row>
    </fo:table-body>
</fo:table>
<fo:table width="17.0cm">
    <fo:table-column column-width="17.0cm" />
    <fo:table-body>
        <fo:table-row background-color="#ffffff" height="1.5cm">
            <fo:table-cell>
                <fo:table>
                    <fo:table-column column-width="17.0cm" />
                    <fo:table-body>
                        <fo:table-row background-color="#ffffff" height="1.5cm">
                            <fo:table-cell number-columns-spanned="1">
                                <fo:block />
                            </fo:table-cell>
                        </fo:table-row>
                    </fo:table-body>
                </fo:table>
            </fo:table-cell>
        </fo:table-row>
    </fo:table-body>
</fo:table>
</fo:wrapper>

我不想要第二个 table 节点。由于 /fo:table[1] 不起作用,我尝试将谓词放在那里,例如 /fo:table[fo:table-column/fo:table-row[2]] 并且没有返回任何内容。奇怪的是,甚至 /fo:table[fo:table-column/fo:table-row] returns 什么都没有,但是 /fo:table[fo:table-column] returns 同样不需要的第二个 table 节点。有人知道这里发生了什么吗?

您是在告诉 XQuery 实现 return 每个 fo:block 元素的第一个 fo:table 元素,而不是整个文档的第一个匹配 fo:table 元素。将整个路径表达式括起来,并将谓词应用于括号内的表达式:

(xsl:stylesheet/xsl:template/fo:root/fo:page-sequence/fo:flow/fo:table/
    fo:table-body/fo:table-row/fo:table-cell/fo:block/fo:table)[1]