738560
738560

Reputation: 129

how to sort elements and group them based on unique element or ID, which has namespace

i have asked with the same question but not with the namespace

i have an xml like..this

<?xml version = '1.0' encoding = 'UTF-8'?>
<FinalDbGetUserId>
  <FinalDbGetUserIdOutputCollection xmlns:ns0="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId"
                                    xmlns="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId">
    <ns0:USERUBSCRIBERS>
      <ns0:USER_ID>237</ns0:USER_ID>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201114</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>207</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
    </ns0:USERUBSCRIBERS>
    <ns0:USERUBSCRIBERS>
      <ns0:USER_ID>237</ns0:USER_ID>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201119</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>212</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
    </ns0:USERUBSCRIBERS>
    <ns0:USERUBSCRIBERS>
      <ns0:USER_ID>237</ns0:USER_ID>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201129</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>230</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
    </ns0:USERUBSCRIBERS>
  </FinalDbGetUserIdOutputCollection>
</FinalDbGetUserId>

output should be like

<FinalDbGetUserId>
  <FinalDbGetUserIdOutputCollection xmlns:ns0="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId"
                                    xmlns="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId">
    <ns0:USERUBSCRIBERS>
      <ns0:USER_ID>237</ns0:USER_ID>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201114</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>207</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201119</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>212</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
      <ns0:BusinessEntity>
        <ns0:NEVADA_BUSINESS_ID>NV0511201129</ns0:NEVADA_BUSINESS_ID>
        <ns0:BUSINESS_ENTITY_ID>230</ns0:BUSINESS_ENTITY_ID>
      </ns0:BusinessEntity>
    </ns0:USERUBSCRIBERS>
  </FinalDbGetUserIdOutputCollection>
</FinalDbGetUserId>

Following is the below xslt, that i was trying and not getting the desired result

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

 <xsl:key name="kuserID" match="USERUBSCRIBERS"  use="USER_ID"/>

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


 <xsl:template match=
 "USERUBSCRIBERS|USER_ID
 |BusinessEntity"/>

 <xsl:template match=
  "USERUBSCRIBERS
    [generate-id()
    =
     generate-id(key('kuserID', USER_ID)[1])
     ]">
  <USERUBSCRIBERS>
   <xsl:copy-of select="USER_ID"/>
   <xsl:apply-templates mode="copy" select="key('kuserID',USER_ID)" />
  </USERUBSCRIBERS>
 </xsl:template>

 <xsl:template match="USERUBSCRIBERS" mode="copy">
  <BusinessEntity>
   <xsl:apply-templates/>
  </BusinessEntity>
 </xsl:template>
</xsl:stylesheet>

i am getting the output same as input and there is no change. may be i am doing mistake, but not getting what the mistake is .... trying to find it out

Upvotes: 2

Views: 587

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167401

Define xmlns:ns0="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId" on your xsl:stylesheet element, then use the prefix ns0 anywhere in your stylesheet where you match or select elements from that namespace e.g. <xsl:key name="kuserID" match="ns0:USERUBSCRIBERS" use="ns0:USER_ID"/> and <xsl:sort select="ns0:USER_ID" data-type="number"/>.

Upvotes: 1

Related Questions