如何根据wso2esb中的数据对xml元素进行排序

时间:2014-08-23 19:04:47

标签: xml xslt xpath wso2 wso2esb

我正在使用wso2esb。 我希望使用wso2esb转换数据我已经完成了问题是在响应时我以递减的方式获取数据我将如何安排以升序方式。

<response>
    <customer-details>
        <cusfirstname>Empire Burlesque</cusfirstname>
        <delarname>Bob Dylan</delarname>
        <cusno>254</cusno>
    </customer-details>
    <customer>
        <cusId>6</cusId>
        <customername>Bonnie Tyler</customername>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </customer>
    <customer>
        <cusId>8</cusId>
        <customername>Dolly Parton</customername>
        <country>USA</country>
        <company>RCA</company>
        <price>9.90</price>
        <year>1982</year>
    </customer>
    <customer>
        <cusId>3</cusId>
        <customername>Gary Moore</customername>
        <country>UK</country>
        <company>Virgin records</company>
        <price>10.20</price>
        <year>1990</year>
    </customer>
    <customer>
        <cusId>5</cusId>
        <customername>Eros Ramazzotti</customername>
        <country>EU</country>
        <company>BMG</company>
        <price>9.90</price>
        <year>1997</year>
    </customer>
    <customer>
        <cusId>7</cusId>
        <customername>Bee Gees</customername>
        <country>UK</country>
        <company>Polydor</company>
        <price>10.90</price>
        <year>1998</year>
    </customer>
    <customer>
        <cusId>1</cusId>
        <customername>Dr.Hook</customername>
        <country>UK</country>
        <company>CBS</company>
        <price>8.10</price>
        <year>1973</year>
    </customer>
    <customer>
        <cusId>2</cusId>
        <customername>Rod Stewart</customername>
        <country>UK</country>
        <company>Pickwick</company>
        <price>8.50</price>
        <year>1990</year>
    </customer>
    <customer>
        <cusId>4</cusId>
        <customername>Andrea Bocelli</customername>
        <country>EU</country>
        <company>Polydor</company>
        <price>10.80</price>
        <year>1996</year>
    </customer>
    <customer>
        <cusId>12</cusId>
        <customername>Percy Sledge</customername>
        <country>USA</country>
        <company>Atlantic</company>
        <price>8.70</price>
        <year>1987</year>
    </customer>
    <customer>
        <cusId>9</cusId>
        <customername>Savage Rose</customername>
        <country>EU</country>
        <company>Mega</company>
        <price>10.90</price>
        <year>1995</year>
    </customer>
    <customer>
        <cusId>11</cusId>
        <customername>Many</customername>
        <country>USA</country>
        <company>Grammy</company>
        <price>10.20</price>
        <year>1999</year>
    </customer>
    <customer>
        <cusId>10</cusId>
        <customername>Kenny Rogers</customername>
        <country>UK</country>
        <company>Mucik Master</company>
        <price>8.70</price>
        <year>1995</year>
    </customer>
</response>

我希望根据 cusId 按升序排列客户标记数据,那么我如何安排这个我尝试使用xquery但我找不到任何功能关于元素排序,cusId应出现在1,2,3,...

我如何实现这一点我知道xpath中没有这个功能。

提前感谢。

2 个答案:

答案 0 :(得分:3)

在XSLT中,您可以在为<xsl:sort>使用<xsl:apply-templates>时使用customer。此样式表为客户订购:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="response">
        <xsl:copy>
            <xsl:apply-templates select="customer-details"/>
            <xsl:apply-templates select="customer">
                <xsl:sort select="cusId"/>
            </xsl:apply-templates>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

答案 1 :(得分:2)

&#34;我尝试使用xquery,但我无法找到有关元素排序的任何功能&#34;

在XQuery中,您使用order by进行排序:

for $i in /response/customer
order by $i/cusId
return $i