XSL-将唯一的和排序的数据存储在变量中 [英] XSL - store unique and sorted data in a variable

查看:197
本文介绍了XSL-将唯一的和排序的数据存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用XSLT 2.0和Apache FOP,我希望能够创建一个新变量,并通过category在其中具有唯一且排序的值,但要保留节点.因此,新变量应具有以下节点:

Using XSLT 2.0 and Apache FOP I want to be able to create a new variable, have unique and sorted values inside it by category but preserve the nodes. So the new variable should have the following nodes:

<category>1. First Aid</category>
<category>2. Access control</category>
<category>3. Fire safety</category>
<category>4. Recognition</category>

输入XML如下:

<equipment>
    <E0132>
        <category>1. First Aid</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E0132>
    <E0133>
        <category>1. First Aid</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E0133>
    <E4122>
        <category>3. Fire safety</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E4122>
    <E4182>
        <category>3. Fire safety</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E4182>
    <E4622>
        <category>2. Access control</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E4622>
    <E5225>
        <category>4. Recognition</category>
        <description>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</description>
    </E5225>
</equipment>

关于XSL,这是我到目前为止所拥有的:

In regard to the XSL, this is what I have so far:

<xsl:variable name="equipment">
    <xsl:for-each-group select="//equipment/node()" group-by="category">
        <xsl:sort select="." order="ascending" />
        <xsl:value-of select="."/>              
    </xsl:for-each-group>        
</xsl:variable>

但是它没有按预期工作.它不包含我想要的category节点,而且我不知道如何在此处集成distinct-values() XSL函数以实现统一性.

But it's not working as expected. It doesn't contain the category nodes as I would like to and I don't know how to integrate distinct-values() XSL function here in order to achieve unicity.

推荐答案

您可以使用current-grouping-key()函数存储值.下面是更新的变量声明.

You can use the current-grouping-key() function to store the values. Below is the updated variable declaration.

<xsl:variable name="equipment">
    <xsl:for-each-group select="//equipment/*/category" group-by=".">
        <xsl:sort select="." order="ascending" />
        <category>
            <xsl:value-of select="current-grouping-key()"/>
        </category>              
    </xsl:for-each-group>   
</xsl:variable>

检查变量内容

<xsl:copy-of select="$equipment" />

将输出提供为

<category>1. First Aid</category>
<category>2. Access control</category>
<category>3. Fire safety</category>
<category>4. Recognition</category>

要在循环中打印变量值,请尝试以下

To print the variable values within a loop, try the below

<!-- print variable values -->
<xsl:for-each select="$equipment/category" >
    <xsl:value-of select="." />
    <xsl:text>&#xA;</xsl:text>
</xsl:for-each>

输出

1. First Aid
2. Access control
3. Fire safety
4. Recognition

这篇关于XSL-将唯一的和排序的数据存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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