繁体   English   中英

我只希望XML / XSLT文档中第一次出现节点。 XPath即使返回[1]也会全部返回

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

我只需要选择在以下XSL-FO templace中第一次出现的<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>

我不想要第二个表节点。 由于/fo:table[1]不起作用,因此我尝试将谓词放到/fo:table[fo:table-column/fo:table-row[2]] ,但未返回任何内容。 奇怪的是,即使/fo:table[fo:table-column/fo:table-row]也不返回任何内容,但是/fo:table[fo:table-column]返回相同的不需要的第二个表节点。 有人知道这是怎么回事吗?

您要告诉XQuery实现为每个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]

暂无
暂无

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

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