如何在 XSLT 管道的下一阶段删除空元素

How to remove empty elements in the next stage of XSLT pipeline

我正在尝试以 XML 文档的形式对解压缩的 Microsoft DOCX 文档进行 XSL 转换。使用以下样式表:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:fn="http://www.w3.org/2005/xpath-functions">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="w:body">
        <body>
            <xsl:for-each select="w:p">
                <p>
                    <xsl:for-each select="w:r">
                        <xsl:value-of select="w:t"/>
                    </xsl:for-each>
                </p>
            </xsl:for-each>
        </body>
    </xsl:template>
</xsl:stylesheet>

我能够获得以下 XML 片段:

<?xml version="1.0" encoding="UTF-8"?>
<body xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:fn="http://www.w3.org/2005/xpath-functions">
<p></p>
<p></p>
<p>Mastering</p>
<p>Front-End Web Development</p>
<p>14 Books in 1</p>
<p>Introducing 200+ ExtensionsAn Advanced Guide</p>
<p></p>
<p></p>
<p></p>
......

请注意,我已经连接了损坏的文本部分( in s)并删除了原始 XML 文档中不需要的标签。

现在如何将它传递到 XSLT 管道的下一阶段,以便删除所有空的

元素?

自 2017 年以来,我们有了 XSLT 3,它有 xsl:where-populated 似乎您想使用

           <xsl:where-populated>
            <p>
                <xsl:for-each select="w:r">
                    <xsl:value-of select="w:t"/>
                </xsl:for-each>
            </p>                   
            </xsl:where-populated>

因此,如果您使用 Saxon 9.8 或更高版本或 Saxon JS 2 或 AltovaXML 2017 R3 或更高版本,这可能会很容易。

或使用

<xsl:for-each select="w:p[w:r/w:t]">