[英]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>
......
請注意,我已經連接了損壞的文本部分(<w:r>s 中的 <w:t>)並刪除了原始 XML 文檔中不需要的標簽。
現在如何將它傳遞到 XSLT 管道的下一階段,以便刪除所有空的 <p> 元素?
自 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]">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.