cancel
Showing results for 
Search instead for 
Did you mean: 

Count How many times the userid is exist in the xml

former_member229127
Participant
0 Kudos

Hi All,
I have a small required in which I need to count how many times the user exists in the payload, am using the XSLT but still, it is not is overcoming the output.

XML:

<?xml version="1.0"?> <Request> <Request> <RequestUNav> <RequestUData> <businessUnit/> <isEffectiveDateType>false</isEffectiveDateType> <actionType></actionType> <isAssigned>false</isAssigned> <UserId>UserTest</UserId> <effectiveDate/> <subjectUserName>Testing 123</subjectUserName> </RequestUData> </RequestUNav> </Request> <Request> <RequestUNav> <RequestUData> <businessUnit/> <isEffectiveDateType>false</isEffectiveDateType> <actionType></actionType> <isAssigned>false</isAssigned> <UserId>UserTest</UserId> <effectiveDate/> <subjectUserName>123</subjectUserName> </RequestUData> </RequestUNav> </Request> <Request> <RequestUNav> <RequestUData> <businessUnit/> <isEffectiveDateType>false</isEffectiveDateType> <actionType></actionType> <isAssigned>false</isAssigned> <UserId>Test</UserId> <effectiveDate/> <subjectUserName>1234</subjectUserName> </RequestUData> </RequestUNav> </Request> </Request>

XSLT code:

<?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"/> <xsl:template match="/">UserId-- <xsl:value-of select= "count(/*/*/list/RequestUIData[contains(., 'UserId')]) "/> </xsl:template> </xsl:stylesheet>

Expected Output:

<?xml version="1.0" encoding="UTF-8"?><Response><UserId>UserTest</UserId><Reflect>2</Reflect><UserId>Test</UserId><Reflect>1</Reflect></Response>

Regards,
Sandhya

MortenWittrock
Active Contributor
0 Kudos

Hi Sandhya

The XML you are posting is not valid. Please update your question, and make sure to paste the XML into a code block. The same goes for your XSLT and your expected output.

Regards,

Morten

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Sandhya

This is easy to do in XSLT from version 2. Have a look at this stylesheet:

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

    <xsl:output method="xml" version="1.0" indent="yes" encoding="UTF-8"/>

    <xsl:template match="/">
        <Response>
            <xsl:for-each-group select="/Request/Request/RequestUNav/RequestUData" group-by="UserId">
                <UserId>
                    <xsl:value-of select="current-grouping-key()"/>
                </UserId>
                <Reflect>
                    <xsl:value-of select="count(current-group())"/>
                </Reflect>
            </xsl:for-each-group>
        </Response>
    </xsl:template>

</xsl:stylesheet>

Kind regards,

Morten

Answers (1)

Answers (1)

former_member229127
Participant
0 Kudos

Hi 7a519509aed84a2c9e6f627841825b5a,

I have updated the XML sorry it was a mistaken.

Regards,
Sandhya