获取具有以下兄弟姐妹和条件的连续节点 [英] Get consecutive nodes with following-sibling and condition
问题描述
我的 XML 文件看起来像这样,但更大:
My XML file looks like this, but bigger:
<w><forme>la</forme><lemme>le</lemme><categorie>DETDFS</categorie></w>
<w><forme>grande</forme><lemme>grand</lemme><categorie>ADJFS</categorie></w>
<w><forme>douleur</forme><lemme>douleur</lemme><categorie>NCFS</categorie></w>
<w><forme>du</forme><lemme>du</lemme><categorie>DETDMS</categorie></w>
<w><forme>père</forme><lemme>père</lemme><categorie>NCMS</categorie></w>
<w><forme>duchesne</forme><lemme>duchesne</lemme><categorie>NCMS</categorie></w>
<w><forme>exemple.</forme><lemme>exemple.</lemme><categorie>NCMS</categorie></w>
<!-- ... -->
我想递归地获取每个 <w>
节点以获取用于创建句子的单词.例如,这里的最后一个词是example."
.所以我想得到每个
直到 .
生成一个句子.
I would like to get every <w>
node recursively to get words to create sentences with.
For example, the final word here is "exemple."
. So I would like to get every <forme>
until .
to generate a sentence.
现在我只能用句号匹配节点.我不知道如何选择第一个
节点(句子的第一个单词),直到最后一个
节点(最后一个单词).
For now I can only match the node with the full stop. I don't know how to select the first <w>
node (first word of the sentence) until the last <w>
node (final word).
following-sibling::w[contains(forme, '.')][1]
我的目标是选择每个
节点(或
),直到单词带有句号,然后递归.
My goal is to select every <w>
node (or <forme>
) until the word with the full stop, and recursively.
我开始写一个模板来连接字符串:
I started to write a template to concatenate strings:
<xsl:template name="concat">
<xsl:param name="liste" />
<xsl:param name="result" select="''"/>
<xsl:choose>
<xsl:when test="$liste">
<xsl:call-template name="concat">
<xsl:with-param name="liste" select="$liste[position() > 1]" />
<xsl:with-param name="result" select="concat($result, ' ', $liste[1])" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$result" disable-output-escaping="no" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
感谢您的帮助.这很重要.
Thx for the help. It's very important.
推荐答案
这在 XSLT 2.0 中使用 xsl:fer each group
和 group-ending-with
非常简单代码>.
This is very simple to do in XSLT 2.0 using xsl:fer each group
with group-ending-with
.
在 XSLT 1.0 中有几种方法可以完成同样的事情——其中包括兄弟递归"和兄弟递归".或使用 键 将每个单词链接到最近的以句点结尾的兄弟姐妹(这两个都已被 Martin Honnen 在上面的回答中提到).
There are several methods to accomplish the same thing in XSLT 1.0 - among them "sibling recursion" or using a key to link each word to its nearest following sibling that ends with a period (both of these have been mentioned by Martin Honnen in his answer above).
您尝试使用递归命名模板的方法也是一种选择.这是一个简化的例子:
The method you have attempted, using a recursive named template, is also an option. Here is a simplified example:
XML
<root>
<word>Joe</word>
<word>waited</word>
<word>for</word>
<word>train.</word>
<word>The</word>
<word>train</word>
<word>was</word>
<word>late.</word>
<word>Mary</word>
<word>and</word>
<word>Samantha</word>
<word>took</word>
<word>the</word>
<word>bus.</word>
<word>Orphan</word>
</root>
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<output>
<xsl:call-template name="combine-words">
<xsl:with-param name="words" select="word"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="combine-words">
<xsl:param name="words" />
<xsl:param name="accumulated" select="/.."/>
<xsl:if test="$words">
<xsl:variable name="word" select="$words[1]" />
<xsl:variable name="isLast" select="substring($word, string-length($word), 1)='.'" />
<xsl:if test="$isLast">
<sentence>
<xsl:for-each select="$accumulated | $word">
<xsl:value-of select="." />
<xsl:if test="position()!=last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
</sentence>
</xsl:if>
<xsl:call-template name="combine-words">
<xsl:with-param name="words" select="$words[position() > 1]"/>
<xsl:with-param name="accumulated" select="$accumulated[not($isLast)] | $word[not($isLast)]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
结果
<?xml version="1.0" encoding="UTF-8"?>
<output>
<sentence>Joe waited for train.</sentence>
<sentence>The train was late.</sentence>
<sentence>Mary and Samantha took the bus.</sentence>
</output>
这篇关于获取具有以下兄弟姐妹和条件的连续节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!