如何对元素进行排序并根据具有命名空间的唯一元素或ID对它们进行分组

时间:2011-07-01 07:19:30

标签: xslt

我问了同样的问题,但没有使用命名空间

我有一个像xt这样的xml

<?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>

输出应该像

<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>

以下是下面的xslt,我正在尝试而没有得到所需的结果

<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>

我得到的输出与输入相同,没有变化。 可能是我做错了,但没有弄错了......试图找出它

1 个答案:

答案 0 :(得分:1)

xmlns:ns0="http://xmlns.oracle.com/pcbpel/adapter/db/FinalDbGetUserId"元素上定义xsl:stylesheet,然后在您匹配的样式表中的任意位置使用前缀ns0,或者从该命名空间中选择元素,例如<xsl:key name="kuserID" match="ns0:USERUBSCRIBERS" use="ns0:USER_ID"/><xsl:sort select="ns0:USER_ID" data-type="number"/>