如何使用表达式在XSLT 1.0中选择唯一节点? [英] How do I select unique nodes in XSLT 1.0 using an expression?

查看:64
本文介绍了如何使用表达式在XSLT 1.0中选择唯一节点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遇到了很多有关在XSLT中确定唯一节点的问题和解决方案,并成功地使它起作用,直到需要使用表达式来评估唯一性为止.

I've seen lots of questions and solutions regarding determining unique nodes in XSLT and succeeded in getting that to work until I needed to evaluate the uniqueness using an expression.

使用此数据文件:

<root>
    <data><field>find me: keyword1</field></data>
    <data><field>find me: keyword2</field></data>
    <data><field>find me: keyword1</field></data>
    <data><field>find me: keyword2</field></data>
    <data><field>find me: keyword1</field></data>
    <data><field>find me: keyword1</field></data>
    <data><field>find me: keyword2</field></data>
</root>

这个转换模板:

<xsl:template match="/root">
    <xsl:for-each select="data[not(field=preceding-sibling::data/field)]">
        field=<xsl:value-of select="field"/>
    </xsl:for-each>
    <xsl:for-each select="data[not(substring-after(field,': ')=substring-after(preceding-sibling::data/field,': '))]">
        short_field=<xsl:value-of select="substring-after(field,': ')" />
    </xsl:for-each>
</xsl:template>

我得到以下输出:

field=find me: keyword1
field=find me: keyword2
short_field=keyword1
short_field=keyword2
short_field=keyword2
short_field=keyword2

第一个for-each可以按预期工作,但是一旦我使用substring-after处理field(因为实际上前导文本并不总是匹配),则实际上只有第一个值匹配.请注意两个额外的keyword2值.

The first for-each works as expected but once I process field using substring-after (because in reality the leading text won't always match), only the first value actually matches. Notice the two extra keyword2 values.

有人可以解释一下并提出解决方案吗?我正在使用MSXSL进行此评估.

Can anybody explain this and propose a solution? I'm using MSXSL to perform this evaluation.

推荐答案

我已经看到许多有关确定唯一性的问题和解决方案 XSLT中的节点

I've seen lots of questions and solutions regarding determining unique nodes in XSLT

太遗憾了,您错过了所有优秀的产品-即那些与 Muenchian分组的人.尝试这种方式:

It's too bad you missed all the good ones - i.e. those dealing with Muenchian grouping. Try it this way:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>

<xsl:key name="field-by-suffix" match="field" use="substring-after(.,': ')" />

<xsl:template match="/">
    <xsl:for-each select="root/data/field[count(. | key('field-by-suffix', substring-after(.,': '))[1]) = 1]">
        <xsl:text>short_field=</xsl:text>
        <xsl:value-of select="substring-after(.,': ')" />
        <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

应用于以下示例输入时:

When applied to the following example input:

<root>
    <data><field>alpha: keyword1</field></data>
    <data><field>bravo: keyword2</field></data>
    <data><field>charlie delta: keyword1</field></data>
    <data><field>echo: keyword2</field></data>
    <data><field>foxtrot: keyword1</field></data>
    <data><field>golf hotel: keyword1</field></data>
    <data><field>echo: keyword2</field></data>
</root>

结果是:

short_field=keyword1
short_field=keyword2

这篇关于如何使用表达式在XSLT 1.0中选择唯一节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆