获取 XSLT 中的前 3 个项目

Getting the first 3 item in XSLT

我现在正在做一个项目,将word文档转成html文档。

我在这里也添加了 XML 文件的一小部分,这个文件将以 HTML 格式查看。

<w:p w:rsidR="001207E2" w:rsidRPr="001509B0" w:rsidRDefault="001207E2" w:rsidP="004E414C">
        <w:pPr>
            <w:pStyle w:val="AppBody-Description"/>
            <w:numPr>
                <w:ilvl w:val="1"/>
                <w:numId w:val="5"/>
            </w:numPr>
        </w:pPr>
        <w:r w:rsidRPr="001509B0">
            <w:t>First Item</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="00AD36E6" w:rsidRPr="001509B0" w:rsidRDefault="00AD36E6" w:rsidP="004E414C">
        <w:pPr>
            <w:pStyle w:val="AppBody-Description"/>
            <w:numPr>
                <w:ilvl w:val="1"/>
                <w:numId w:val="5"/>
            </w:numPr>
        </w:pPr>
        <w:r w:rsidRPr="001509B0">
            <w:t>Second Item</w:t>
        </w:r>
    </w:p>
    <w:p w:rsidR="00AD36E6" w:rsidRPr="001509B0" w:rsidRDefault="00AD36E6" w:rsidP="004E414C">
        <w:pPr>
            <w:pStyle w:val="AppBody-Description"/>
            <w:numPr>
                <w:ilvl w:val="1"/>
                <w:numId w:val="5"/>
            </w:numPr>
        </w:pPr>
        <w:r w:rsidRPr="001509B0">
            <w:t>Third Item</w:t>
        </w:r>
    </w:p>

所以,这是我尝试使用的 XSLT 代码

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 wp14">
<xsl:output method="html" doctype-system="about:legacy-compat"/>
<xsl:template match="/">
    <html>
        <head>
            <title/>
        </head>
        <body>
            <xsl:for-each select="w:document/w:body/w:p">
                <xsl:if test="w:pPr/w:numPr/w:ilvl[@w:val='1']">
                        <ol>
                            <li>    <xsl:for-each select="w:r/w:t[position()&lt;3]">
                                    <xsl:value-of select="."/>
                                    </xsl:for-each>
                            </li>
                        </ol>
                </xsl:if>
            </xsl:for-each>
        </body>
    </html>
</xsl:template>

这是该代码的结果

<!DOCTYPE HTML SYSTEM "about:legacy-compat"><html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title></title>
</head>
<body>
    <ol>
        <li>First Item</li>
    </ol>
    <ol>
        <li>Second Item</li>
    </ol>
    <ol>
        <li>Third Item</li>
    </ol>
    <ol>
        <li>Fourth Item</li>
    </ol>
</body>

好的,这里的问题是我不想 select list.I 中的第 4 个元素,只想得到它的前 3 个。

但是没用。

我是 XSLT 的新手,所以基本上我看不到 problem.I 检查了几个来源,这对我来说似乎很合乎逻辑。

感谢您的帮助

所有 w:t[position() &lt; 3] 谓词都将通过,因为所有 w:t 元素都位于其各自 w:r 元素的位置 1。

改为测试祖先 w:p 元素的位置:

改变

        <xsl:for-each select="w:document/w:body/w:p">

        <xsl:for-each select="w:document/w:body/w:p[position() &lt; 3]">

根据您的实际 XML 和要求,您可能还想将 w:pPr/w:numPr/w:ilvl[@w:val='1'] 测试合并到 xsl:for-each/@select 测试中。