简体   繁体   中英

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

I need to select only the first occurrence of the <fo:table ...> node in the following XSL-FO templace, but xPath is returning all of them. Here is the XPath statement I'm using after creating a wrapper element with all the namespaces:

<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>

And here is the XSL-FO input:

<?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>

This is what I expect as output:

<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>

This is what BaseX and Camel xQuery component actually outputs:

<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>

I don't want that second table node. Since /fo:table[1] doesn't work, I've tried putting predicates in there such as /fo:table[fo:table-column/fo:table-row[2]] and nothing is returned. Oddly, even /fo:table[fo:table-column/fo:table-row] returns nothing, but /fo:table[fo:table-column] returns the same unwanted 2nd table node. Anyone have any idea what's going on here?

You're telling the XQuery implementation to return the first fo:table element for each fo:block element, not the first matching fo:table element of the whole document. Put parentheses around the whole path expression, and apply the predicate on the parenthesized expression:

(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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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