如何使XML EXPLICIT * NOT *按字母顺序重新排序?

时间:2011-04-20 21:59:29

标签: sql sql-server xml

我正在使用XML EXPLICIT生成一个发送到另一个应用程序的XML文档。标签是标签/值,顺序很重要,因为其他应用程序将在收到的订单中显示标签和值。

这是我使用

信息的一个例子
N     |    V
_______________
Objective - XXX
State     - New York
Country   - US

我希望得到

<X>
   <T>
     <N>Objective</N>
     <V>XXX</V>
   </T>
   <T>
     <N>State</N>
     <V>New York</V>
   </T>
   <T>
     <N>Country</N>
     <V>US</V>
   </T>
</X>

然而我会按N字母按字母顺序排序

<X>
   <T>
     <N>Country</N>
     <V>US</V>
   </T>
   <T>
     <N>Objective</N>
     <V>XXX</V>
   </T>
   <T>
     <N>State</N>
     <V>New York</V>
   </T>

</X>

这是查询

CREATE PROCEDURE [dbo].[XML]    
 @iPackageId INT,     
 @iUserId INT    
AS    
BEGIN    
 SET NOCOUNT ON    

 DECLARE @iPluginId INT, @iIndex INT, @sLabel VARCHAR(100), @sValue VARCHAR(200), @sField VARCHAR(4000), @sQuery VARCHAR(8000)    

 DECLARE @MySettings TABLE(iIndex INT PRIMARY KEY IDENTITY, iSettingsId INT, sLabel VARCHAR(100), sField VARCHAR(4000))    
 DECLARE @Fields TABLE(sLabel VARCHAR(100), sValue VARCHAR(400))    

 SET @iPluginId = (SELECT iPluginId FROM trnPackage WHERE iPackageId = @iPackageId)    
 SET @iIndex = 1    
 SET @sQuery = ''    

//WE get for the N and a sqlstatement to get the V from the catPluginSettings table, and order them by iDisplayOrder, and insert them in temp MySettings table.
//By executing that sql statement we get the actual value of for V, and save the N and V in @Fields table

 INSERT INTO @MySettings    
 SELECT     
  iSettingsId,sLabel, sField    
 FROM catPluginSettings    
 WHERE iPluginId = @iPluginId    
  AND bActive = 1    
 ORDER BY iDisplayOrder    



 WHILE EXISTS (SELECT iIndex FROM @MySettings)    
 BEGIN    
  SELECT     
   @sLabel = sLabel,     
   @sField = sField + CAST(@iPackageId AS varchar)    
  FROM @MySettings T    
  WHERE iIndex = @iIndex    

  -- GET VALUE FROM sFiled     
  DECLARE @sResult NVARCHAR(400)    

  DECLARE @SelectStatement NVARCHAR(4000)    
  DECLARE @FullStatement NVARCHAR(4000)    
  DECLARE @ParameterList NVARCHAR(500)         

  SET @ParameterList = '@sResult NVARCHAR(400) OUTPUT'         
  SET @FullStatement = @sField     

  EXECUTE sp_executesql @FullStatement, @ParameterList, @sResult = @sResult OUTPUT    
 IF NOT @sLabel IS NULL AND NOT @sResult IS NULL BEGIN  
  INSERT INTO @Fields VALUES (@sLabel ,@sResult)    
 END  
  -- END GET VALUE FROM sField    

  DELETE @MySettings WHERE iIndex = @iIndex    

  SET @iIndex = @iIndex + 1    
 END    



 -- ROOT    
 SELECT  distinct  
  1 as Tag,     
  NULL as Parent,    
  NULL [X!1],     
  BB.iBBId as [X!1!A!element],    
  T.sPckCode as [X!1!B!element],    
  SUBSTRING(cU.sNetLogin, CHARINDEX('\',cU.sNetLogin )+1, LEN(cU.sNetLogin)) as [X!1!C!element],    
  cUR.sFullName as [X!1!D!element],    
 CAST(DATEPART(YEAR, T.dCreationDate) AS VARCHAR)    
   + dbo.PadLeft(CAST(DATEPART(MONTH, T.dCreationDate) AS VARCHAR),2,'0')    
   + dbo.PadLeft(CAST(DATEPART(DAY, T.dCreationDate) AS VARCHAR),2,'0')     
   + dbo.PadLeft(CAST(DATEPART(HOUR, T.dCreationDate) AS VARCHAR),2,'0')    
   + dbo.PadLeft(CAST(DATEPART(MINUTE, T.dCreationDate) AS VARCHAR),2,'0')   
AS [X!1!E!element],    
  cP.sAppShortName as [X!1!F!element],    
  CONVERT (DECIMAL (16, 2), T.mAmountUSD) as [X!1!G!element], ---> CAST a 2 Decimales <--    
  NULL AS [T!2!N!element],    
  NULL AS [T!2!V!element]    
 FROM trnPackage T    
 INNER JOIN trnPkgBB BB ON T.iPackageId = BB.iPackageId    
 INNER JOIN relPackageUser R ON T.iPackageId = R.iPackageId --AND R.bIsSysAdmin = 0 AND R.bIsBackupUser = 0    
 INNER JOIN catUser cU ON R.iUserId = cU.iUserId      
 INNER JOIN catUser cUR ON T.iUserId = cUR.iUserId     
 INNER JOIN catPlugin cP ON T.iPluginId = cP.iPluginId    
 WHERE (T.iPackageId = @iPackageId AND R.iUserId = @iUserId)    
  AND (BB.iPackageId = @iPackageId AND BB.iApproverId = @iUserId)    
  AND BB.iBBStatusId <> (SELECT iStatusId FROM catStatus WHERE sStatusName = 'Completed')    

-- EXTRA INFO    
 UNION     
 SELECT     
  2 AS Tag,    
  1 AS Parent,    
  NULL,    
  NULL,     
  NULL,    
  NULL,    
  NULL,    
  NULL,    
  NULL,     
  NULL,    
  sLabel,    
  sValue    
 FROM @Fields T    
 FOR XML EXPLICIT    
    select * from @Fields
END    

1 个答案:

答案 0 :(得分:4)

我在最后的输出中没有看到订单...

没有它你就无法控制生成xml的顺序。

如果您不知道如何向工会申请订单,请参阅此问题:SQL Query - Using Order By in UNION