text()包含关键字的组(group where text() contains a keyword)

编程入门 行业动态 更新时间:2024-10-23 17:30:37
text()包含关键字的组(group where text() contains a keyword)

我有一个大文本文件,在某些时候看起来像这样:

<w> <randomnode> <pa/> <pa> Keyword1 sometxt1 <thing>abc</thing>: blabla </pa> <stuff>abc</stuff> <pa> just blabla </pa> <pa> Keyword2 othertxt2: blabla </pa> <pa> just blabla </pa> <pa> just blabla </pa> <pa> Keyword1 xxx: and blabla </pa> </randomnode> </w>

并希望得到这个结果:

<w> <randomnode> <k attr="keyword1 sometxt1"> <p> <s> Keyword1 sometxt1 <thing>abc</thing>: </s> blabla </p> <stuff>abc</stuff> <p> just blabla </p> </k> <k attr="keyword1 othertxt2"> <p> <s> Keyword2 othertxt2: </s> blabla </p> <p> just blabla </p> <p> just blabla </p> </k> <k attr="keyword1 xxx"> <p> <s> Keyword1 xxx: </s> and blabla </p> </k> </randomnode> </w>

在英语中:我想通过每个<pa>并在该节点的text()中有keyword1或keyword2或keyword3时对它们进行分组。 对: <s>的内容的拆分是在另一个模板中完成的,并且一旦我可以正确地对<pa>进行分组就应该有效。

到目前为止我有这个:

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa/text()[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]">

问题是没有选择任何东西,我感觉这是因为text()......

我可以在text()上使用group-starting-woth吗? 我真的想在使用完全不同的东西之前使用它并扩展/纠正它。

I have a large text file that at some point looks like this :

<w> <randomnode> <pa/> <pa> Keyword1 sometxt1 <thing>abc</thing>: blabla </pa> <stuff>abc</stuff> <pa> just blabla </pa> <pa> Keyword2 othertxt2: blabla </pa> <pa> just blabla </pa> <pa> just blabla </pa> <pa> Keyword1 xxx: and blabla </pa> </randomnode> </w>

and want to get this result:

<w> <randomnode> <k attr="keyword1 sometxt1"> <p> <s> Keyword1 sometxt1 <thing>abc</thing>: </s> blabla </p> <stuff>abc</stuff> <p> just blabla </p> </k> <k attr="keyword1 othertxt2"> <p> <s> Keyword2 othertxt2: </s> blabla </p> <p> just blabla </p> <p> just blabla </p> </k> <k attr="keyword1 xxx"> <p> <s> Keyword1 xxx: </s> and blabla </p> </k> </randomnode> </w>

In English: I want to go through each <pa> and group them whenever there is a keyword1 or keyword2 or keyword3 in the text() of that node. The splitting-up for the content of the <s> on the : is done in another template and should work once I can group the <pa> correctly.

I have this so far:

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa/text()[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]">

The problem ist that nothing is selected and I have a feeling it is because of text()...

Can I use group-starting-woth on text() at all? I would really like to use this and extend/correct it before I do something completly different..

最满意答案

好吧,我会彻底抛弃text()并简单比较

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]">

如果要使用text()子节点选择,则需要

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa[text()[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]]">

Well I would ditch the text() completely and simply compare

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]">

If you want to use the text() child node selection then you need

<xsl:for-each-group select="$randomnode/*[normalize-space(.)!='']" group-starting-with="pa[text()[contains(., 'keyword1') or contains(., 'keyword2') or contains(., 'keyword3')]]">

更多推荐

本文发布于:2023-08-02 03:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1368416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关键字   text   keyword   group

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!