Frage

I have an xml with different name tags at different places. What I would need to do is, sort the needed one and get the max value of them.

Input XML:

<SUBSCRIBER>
    <OnPeakAccountID>10</OnPeakAccountID>
    <OnPeakSmsExpDate>**20640217172520**</OnPeakSmsExpDate>
    <UnliSmsOnCtl>20140204173322</UnliSmsOnCtl>
    <BucketMocOn>840</BucketMocOn>
    <BucketMocOnExp>20140204173322</BucketMocOnExp>
    <BucketMocTri>10000</BucketMocTri>
    <BucketMocTriExp>**20140210235959**</BucketMocTriExp>
    <UnliNxbFbcCtl>**20140210235959**</UnliNxbFbcCtl>
    <BucketIM6VolFbc>10000</BucketIM6VolFbc>
    <BucketIM6VolFbcExp>**20140210235959**</BucketIM6VolFbcExp>
    <UnliEmail1FbcCtl>**20140210235959**</UnliEmail1FbcCtl>
    <UnliIM2FbcCtl>**20140210235959**</UnliIM2FbcCtl>
    <UnliPhoto1FbcCtl>**20140210235959**</UnliPhoto1FbcCtl>
    <UnliSns1FbcCtl>**20140210235959**</UnliSns1FbcCtl>
    <UnliBoost1FbcCtl>**20140210235959**</UnliBoost1FbcCtl>
    <UnliBrws1FbcCtl>**20140210235959**</UnliBrws1FbcCtl>
</SUBSCRIBER>

For example, In the above XML, I need to find the greatest value from the elements marked with **<>**. To be honest, I am still fighting for the logic and nothing is working at my end. Any help/suggestions would be appreciated. Thanks for your support.

War es hilfreich?

Lösung

I think you want this template:

<xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

<xsl:strip-space elements="*"/>

    <xsl:template match="SUBSCRIBER">
        <xsl:for-each select="OnPeakSmsExpDate|UnliSmsOnCtl|BucketMocOnExp|BucketMocTriExp|UnliNxbFbcCtl|BucketIM6VolFbcExp|UnliEmail1FbcCtl|UnliIM2FbcCtl|UnliPhoto1FbcCtl|UnliSns1FbcCtl|UnliBoost1FbcCtl|UnliBrws1FbcCtl">
            <xsl:sort order="descending"/>
            <xsl:if test="position() = 1">
                <greatest><xsl:value-of select="."/></greatest>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>


</xsl:stylesheet>

Andere Tipps

XSLT 2.0

<xsl:template match="/SUBSCRIBER">
    <max><xsl:value-of select="max((BucketMocTriExp, UnliNxbFbcCtl, BucketIM6VolFbcExp, UnliEmail1FbcCtl, UnliIM2FbcCtl, UnliPhoto1FbcCtl, UnliSns1FbcCtl, UnliBoost1FbcCtl, UnliBrws1FbcCtl))"/></max>
</xsl:template>

EDIT:

To include a node conditionally, use the following construct:

<xsl:value-of select="max((a, b, c, if(d > 0) then e else ())) "/>

This XSLT sorts elements with value (containing 2 asterisk at start and at end) in descending order and extrats the first one:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output method="text" indent="yes" />
  <xsl:strip-space elements="*"/>

<xsl:template match="SUBSCRIBER">
    <xsl:for-each select="*[starts-with(.,'**') and substring(.,string-length(.) - 1) = '**']">
        <xsl:sort select="number(substring(substring(.,3),1,string-length(substring(.,3)) - 2))" order="descending"/>
        <xsl:if test="position() = 1">
            <xsl:value-of select="substring(substring(.,3),1,string-length(substring(.,3)) - 2)"/>
        </xsl:if>
    </xsl:for-each>

</xsl:template>
</xsl:stylesheet>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top