按属性值对XML节点进行分组

时间:2016-06-16 10:41:08

标签: javascript xml node.js xslt

我是xslt转换的新手,我需要重新组合包含相同用户名的所有节点,并重新组合与此用户相对应的技能,这是一个简单的示例。 输入:



<Rowsets>
    <Rowset>
        <Columns>
            <Column Description="" MaxRange="1" MinRange="0" Name="login" SQLDataType="1" SourceColumn="User_login"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="longName" SQLDataType="1" SourceColumn="User_longName"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Competency" SQLDataType="1" SourceColumn="Competency"/>
        </Columns>
        <Row>
            <login>NG5E054</login>
            <longName>---</longName>
            <Competency>SKILL1</Competency>
        </Row>
        <Row>
            <login>NG5E059</login>
            <longName>---</longName>
            <Competency>SKILL1</Competency>
        </Row>
        <Row>
            <login>NG5E059</login>
            <longName>---</longName>
            <Competency>SKILL2</Competency>
        </Row>
        <Row>
            <login>NG5E059</login>
            <longName>---</longName>
            <Competency>SKILL3</Competency>
        </Row>

    </Rowset>
</Rowsets>
&#13;
&#13;
&#13;

我想要的输出是:

&#13;
&#13;
<Rowsets>
    <Rowset>
        <Columns>
            <Column Description="" MaxRange="1" MinRange="0" Name="login" SQLDataType="1" SourceColumn="User_login"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="longName" SQLDataType="1" SourceColumn="User_longName"/>
            <Column Description="" MaxRange="1" MinRange="0" Name="Competency" SQLDataType="1" SourceColumn="Competency"/>
        </Columns>
        <Row>
            <login>NG5E054</login>
            <longName>---</longName>
            <Competency>SKILL1</Competency>
        </Row>
        <Row>
            <login>NG5E059</login>
            <longName>---</longName>
            <Competency>SKILL1, SKILL2, SKILL3</Competency>
        </Row>
    </Rowset>
</Rowsets>
&#13;
&#13;
&#13;

谢谢

1 个答案:

答案 0 :(得分:-1)

试试这个:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="//Row/login[not(.=preceding::*)]">
            <xsl:variable name="login" select="."/>
            <Row>
                <login><xsl:value-of select="$login"/></login>
                <Competency>
                    <xsl:for-each select="//Row[login=$login]/Competency">
                        <xsl:value-of select="."/>
                        <xsl:if test="position() != last()">, </xsl:if>
                    </xsl:for-each>
                </Competency>
            </Row>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

基本上它为每个唯一登录创建一行。在每一行内部,它汇集了所有相关技能。

这适用于XSLT 1.0和2.0。