我目前正在编写XSLT文档,以将javadoc XML输出(不可修改)转换为重组文本。我遇到的问题之一是javadoc的XML结构如下
<node1>
<node2>
<code/>
</node2>
<node3>
<![CDATA[DataType]]>
</node3>
</node1>
<node1>
<node3>
<![CDATA[s need special formatting, but breaks in restructured text]]>
</node3>
</node1>
这将产生ASCII输出(在
node2/code
内部存在node1
表示应将其用``)包围)``DataType``s need special formatting, but break in restructured text
在重组文本中,结尾的``不能跟字母数字或它不能正确呈现,因此除了前一个输出,我还需要查看匹配
//node1/node3
的下一个节点是否没有它是字母数字的第一个字符,如果这样做,它将像这样``DataType``\s need special formatting, but breaks in restructured text
但如果是标点符号,则可以使用以下命令
``DataType``. need special formatting, but breaks in restructured text
XSLT2.0有可能吗?
最佳答案
做一个“向后看”可能比尝试向前看更容易。
<xsl:template match="node1/node3" priority="1">
<xsl:value-of select="." />
</xsl:template>
<xsl:template match="node1[node2/code]/node3" priority="2">
<xsl:text>``</xsl:text>
<xsl:next-match />
<xsl:text>``</xsl:text>
</xsl:template>
<!-- special template for the block immediately following a node2/code block -->
<xsl:template match="node1[preceding-sibling::node1[1]/node2/code]/node3" priority="3">
<xsl:if test="matches(., '^[A-Za-z0-9]')">\</xsl:if>
<xsl:next-match />
</xsl:template>
您甚至可以将
if
合并到match表达式中<xsl:template match="node1[preceding-sibling::node1[1]/node2/code]
/node3[matches(., '^[A-Za-z0-9]')]" priority="3">
<xsl:text>\</xsl:text>
<xsl:next-match />
</xsl:template>
关于xml - 查看满足XPATH的下一个节点以定义行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14774601/