title: 323.信XML,得自信 outline: deep

XML可能是计算有史以来最NB的发明了,以至于我们以没有XML的程序是难登大堂的程序,不用XML,你都不好意思当程序员。于是,我们看到了很多很雷人的用法(《信XML,得永生》),当然一些朋友当时并没有看懂,不过我不怪大家,因为我们依然深信使用XML可以让你有强大的Zhuangbility,于是我们有下面这两种相当Geiliable的用法。

一、XML中的XML

这个例子是某公司的一个SOAP实现——我们的Webservice需要返回一个XML字符串,这怎么办呢?其实很容易,因为——XML是无所不能的,那怕是封装自己。

‹!-- ED: soap envelope omitted for readability --› ‹string xmlns="urn:Initech.Global.Services"› <CompanyGetConnector> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="InitechGetConnector"> <xs:complexType> <xs:choice maxOccurs="unbounded"> <xs:element name="employees"> <xs:complexType> <xs:sequence> <xs:element name="EmployerName" type="xs:string" minOccurs="0"/> <xs:element name="Employee" type="xs:string" minOccurs="0"/> <xs:element name="Firstname" type="xs:string" minOccurs="0"/> <xs:element name="Prefix" type="xs:string" minOccurs="0"/> <xs:element name="Lastname" type="xs:string" minOccurs="0"/> <xs:element name="Org._unit" type="xs:string" minOccurs="0"/> <xs:element name="Function" type="xs:string" minOccurs="0"/> <xs:element name="E-mail_work" type="xs:string" minOccurs="0"/> <xs:element name="Telephone_work" type="xs:string" minOccurs="0"/> <xs:element name="Mobile_work" type="xs:string" minOccurs="0"/> <xs:element name="Birthdate" type="xs:date" minOccurs="0"/> <xs:element name="Hired_since__irt._yearsemployed_" type="xs:date" minOccurs="0"/> <xs:element name="Image" type="xs:base64Binary" minOccurs="0"/> </xs:sequence> </xs:complexType> </xs:element> </xs:choice> </xs:complexType> </xs:element> </xs:schema>

&lt;employees&gt;
  &lt;EmployerName&gt;
    My Client
  &lt;/EmployerName&gt;
  &lt;Employee&gt;
    100001
  &lt;/Employee&gt;
&lt;/employees&gt;

</CompanyGetConnector> ‹/string›

二、一切皆为配置

没有hard code这是一个优秀程序员在入门时就要学习的,对于Hard Coder的东西最好写在配置文件中,这样修改这些参数就不需要修改代码而需要重新编译了。自从有了XML之后,我们的配置文件就不在使用像ini文件或是Unix下在conf文件那样的易读,我们认为,使用XML作为配置文件的格式是大势所趋,而且,我们要让我们的代码尽量的可以高度的配置,于是我们出现了下面的代码——这是一个强大的尝试,其标志着,我们完全可以以不久的未来用XML来编写一切语言的代码。

注:下面的代码最强大的应该是XML中的那个SQL。

‹add key="sqlSource" value=" SELECT TOP REPLACE_NUMBER_OF_ROWS_TO_RETRIEVE History.handle AS ID_FAX_LOG, CASE isnumeric(SUBSTRING (Notes_Doc.Text ,1,8)) WHEN 1 then SUBSTRING (Notes_Doc.Text ,1,8) ELSE NULL END AS ID_STAGE, DocumentUsers.UserName AS NM_DOCUMENTUSER_USERNAME, DocumentUsers.UserID AS TXT_DOCUMENTUSER_USERID, DocumentUserGroups.GroupID AS TXT_DOCUMENTUSERGROUP_GROUPID, Documents.UniqueID AS TXT_DOCUMENTS_UNIQUE_ID, History.TRDateTime AS DT_HISTORY_TRANSACTION_DATE, CASE COALESCE(HistoryPrint.handle,0) WHEN 0 THEN CASE COALESCE(HistoryGeneric.handle,0) WHEN 0 THEN CASE COALESCE(HistoryTRX.handle,0) WHEN 0 THEN '??' ELSE CASE (Documents.Flags & 0x10) WHEN 0 THEN 'Send' ELSE 'Recieve' END END ELSE CAST(HistoryGeneric_Short.Data AS varchar(32)) END ELSE 'Print' END AS TXT_TRANSACTION_TYPE,

       CASE COALESCE(HistoryPrint.handle,0)
          WHEN 0 THEN
             CASE COALESCE(HistoryGeneric.handle,0)
                WHEN 0 THEN
                   CASE COALESCE(HistoryTRX.handle,0)
                      WHEN 0 THEN '??'
                      ELSE
                         CASE Documents\_Term.TermStatStr
                            WHEN 'Success' THEN 'Success'
                            ELSE 'Fail'
                            END
                      END
                ELSE
                   CASE HistoryGeneric.ErrCode
                      WHEN 0 THEN 'Success'
                   ELSE 'Fail'
                   END
                END
          ELSE
             CASE SUBSTRING(HistoryPrint.Msg,1,7)
                WHEN 'Success' THEN 'Success'
                ELSE 'Fail'
                END
          END AS TXT\_TRANSACTION\_STATUS,

       CASE COALESCE(HistoryPrint.handle,0)
          WHEN 0 THEN
             CASE COALESCE(HistoryGeneric.handle,0)
                WHEN 0 THEN
                   CASE COALESCE(HistoryTRX.handle,0)
                      WHEN 0 THEN '??'
                      ELSE COALESCE(HistoryTRX\_Term.TermStatStr,CONVERT(varchar,Documents.TermStat))
                      END
                ELSE REPLACE(REPLACE(CAST(HistoryGeneric\_Detail.Data AS varchar(192)) ,'\\t',''), '~u', HistoryGeneric.UserID )
                END
          ELSE HistoryPrint.Msg
          END AS TXT\_TRANSACTION\_MESSAGE,

       CASE COALESCE(HistoryPrint.handle,0)
          WHEN 0 THEN
             CASE COALESCE(HistoryGeneric.handle,0)
                WHEN 0 THEN
                   CASE COALESCE(HistoryTRX.handle,0)
                      WHEN 0 THEN Documents.ElapsedSendTime
                      ELSE
                         CASE COALESCE(HistoryTRX.handle,0)
                            WHEN 0 THEN Documents.ElapsedSendTime
                            ELSE HistoryTRX.ElapsedTime
                            END
                      END
                ELSE NULL
                END
          ELSE HistoryPrint.TimeToPrint
          END AS NBR\_TRANSACTION\_ELAPSEDTIME,

       CASE COALESCE(HistoryGeneric.handle,0)
          WHEN 0 THEN
             CASE substring(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE
                           (REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                              Documents.Destination,' ',''),')',''),'(',''),
                              '-',''),'/',''),'.',''),'\*',''),',',''),';',''),
                              '\\',''),'-',''),1,1)
                WHEN '1' THEN substring(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                                        REPLACE(REPLACE(REPLACE( REPLACE(REPLACE(
                                        REPLACE(Documents.Destination,' ',''),')',
                                        ''),'(',''),'-',''),'/',''),'.',''),'\*',''),
                                        ',',''),';',''),'\\',''),'-',''), 2, len(
                                        REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                                        REPLACE(REPLACE(REPLACE( REPLACE(REPLACE(
                                        REPLACE(Documents.Destination,' ',''),')',
                                        ''),'(',''),'-',''),'/',''),'.',''),'\*','')
                                        ,',',''),';',''),'\\',''),'-','')) )
                ELSE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
                     REPLACE(REPLACE(REPLACE(Documents.Destination,' ',''),'-',''),')',
                     ''),'(',''),'/',''),'.',''),'\*',''),',',''),';',''),'\\',''),'-','')
                END
          ELSE HistoryGeneric.UserID
          END AS TXT\_TRANSACTION\_DESTINATION,

       CASE (Documents.Flags & 0x8)
          WHEN 0 THEN 'N'
          ELSE 'Y' END AS NBR\_DOCUMENTS\_DELETED,

       CASE (Documents.Flags & 0x4)
          WHEN 0 THEN 'N'
          ELSE 'Y' END AS NBR\_DOCUMENTS\_VIEWED,

       /\* Fax Destination \*/
       Documents.ToName AS TXT\_DOCUMENTS\_TO\_NAME,
       Documents.ToContactNum AS TXT\_DOCUMENTS\_TO\_CONTACT\_NUM,
       Documents.ToCompany AS TXT\_DOCUMENTS\_TO\_COMPANY,
       Documents.ToCityState AS TXT\_DOCUMENTS\_TO\_CITY\_STATE,
       Documents.FaxDIDNum AS TXT\_DOCUMENTS\_FAX\_DID\_NUM,
       Documents.FromPhoneNum AS TXT\_DOCUMENTS\_FROM\_PHONE\_NUM,
       Documents.GeneralFaxNum AS TXT\_DOCUMENTS\_GENERAL\_FAX\_NUM,
       HistoryPrint.NetPrintID AS TXT\_HISTORYPRINT\_NETPRINTID,

       /\* Number of pages \*/
       DocFiles.NumPages AS NBR\_DOCFILES\_TOTAL\_PAGE\_COUNT,
       HistoryTRX.GoodPageCount AS NBR\_HISTORYTRX\_GOOD\_PAGE\_COUNT,
       HistoryTRX.BadPageCount AS NBR\_HISTORYTRX\_BAD\_PAGE\_COUNT,
       HistoryPrint.PagesPrinted AS NBR\_HISTORYPRINT\_PAGESPRINTED,
       HistoryPrint.CopiesPrinted AS NBR\_HISTORYPRINT\_COPIESPRINTED,
       /\* location of fax image \*/ DTConfigurations.ServerName AS TXT\_DOCFILES\_SERVER\_NAME,
       DTConfigurations.ImageDir AS TXT\_DOCFILES\_IMAGE\_DIR,
       DocFiles.BodyFilename AS TXT\_DOCFILES\_BODY\_FILENAME,
       Documents.FCSFile AS TXT\_DOCFILES\_FCS\_FILE,
       REPLACE( DTConfigurations.ImageDir, 'D:\\Data', '\\\\'+ServerName )
          + '\\'+DocFiles.BodyFilename+'\*' AS TXT\_DOCFILES\_PATH\_BODY\_NAME,
       REPLACE( DTConfigurations.ImageDir, 'D:\\Data', '\\\\'+ServerName )
          + '\\'+Documents.FCSFile+'\*' AS TXT\_DOCUMENTS\_PATH\_FCSFILE,
       Notes\_Doc.Text AS TXT\_NOTES\_DOC\_TEXT,
       Notes\_CCList.Text AS TXT\_NOTES\_CCLIST\_TEXT,
       DocumentUsers.RouteInfo AS TXT\_DOCUMENTUSER\_ROUTEINFO,
       DocumentUsers.RouteType AS NBR\_DOCUMENTUSER\_ROUTETYPE,
       DocumentUsers.EmailAddr AS TXT\_DOCUMENTUSER\_EMAILADDR,

       /\* misc Documents data \*/
       Documents.CreationTime AS DT\_DOCUMENTS\_CREATION\_TIME,
       Documents.FRFlags2 AS NBR\_DOCUMENTS\_FRFLAGS2,
       Documents.Flags AS NBR\_DOCUMENTS\_FLAGS,
       Documents.ErrorCode AS NBR\_DOCUMENTS\_ERROR\_CODE,
       Documents.TermStat AS NBR\_DOCUMENTS\_TERMSTAT,

       /\* misc HistoryTRX data \*/
       HistoryTRX.RemoteID AS TXT\_HISTORYTRX\_REMOTE\_ID,
       HistoryTRX.RemoteServer AS TXT\_HISTORYTRX\_REMOTE\_SERVER,
       HistoryTRX.Flags AS NBR\_HISTORYTRX\_FLAGS,
       HistoryTRX.TermStat AS NBR\_HISTORYTRX\_TERMSTAT,

       /\* misc HistoryTRX data \*/
       HistoryGeneric.ErrCode AS NBR\_HISTORYGENERIC\_ERRCODE,
       HistoryGeneric.GenType AS NBR\_HISTORYGENERIC\_GENTYPE,
       HistoryGeneric.UserID AS TXT\_HISTORYGENERIC\_USERID,

       /\* Handles \*/ Documents.handle AS ID\_DOCUMENTS\_HANDLE,
       History.handle AS ID\_HISTORY\_HANDLE,
       HistoryTRX.handle AS ID\_HISTORYTRX\_HANDLE,
       HistoryGeneric.handle AS ID\_HISTORYGENERIC\_HANDLE,
       HistoryPrint.handle AS ID\_HISTORYPRINT\_HANDLE

FROM Documents
        INNER JOIN Users DocumentUsers ON Documents.OwnerID = DocumentUsers.handle
        INNER JOIN History ON Documents.handle = History.Owner
        LEFT OUTER JOIN DocFiles ON Documents.DocFileDBA = DocFiles.handle
        LEFT OUTER JOIN Groups DocumentUserGroups ON DocumentUsers.GroupID = DocumentUserGroups.handle
        LEFT OUTER JOIN HistoryPrint ON HistoryPrint.handle = History.handle
        LEFT OUTER JOIN HistoryGeneric ON HistoryGeneric.handle = History.handle
        LEFT OUTER JOIN Notes Notes\_Doc ON Notes\_Doc.handle = Documents.NoteDBA
        LEFT OUTER JOIN Notes Notes\_CCList ON Notes\_CCList.handle = Documents.CCListDBA
        LEFT OUTER join DTConfigurations ON DTConfigurations.ServerGUID = Documents.ServerGUID
        LEFT OUTER JOIN Globalization HistoryGeneric\_Detail ON
           HistoryGeneric\_Detail.Namespace = 'RightFax.SQL.HistoryGeneric'
           AND SUBSTRING(HistoryGeneric\_Detail.LocKey,5,20) = 'DetailMsg'
           AND SUBSTRING(HistoryGeneric\_Detail.LocKey,1,3) = CAST(HistoryGeneric.GenType AS varchar)
           AND HistoryGeneric\_Detail.IsoLanguageName = 'en-us'
        LEFT OUTER JOIN Globalization HistoryGeneric\_Short ON
           HistoryGeneric\_Short.Namespace = 'RightFax.SQL.HistoryGeneric'
           AND SUBSTRING(HistoryGeneric\_Short.LocKey,5, 20) = 'ShortMsg'
           AND SUBSTRING(HistoryGeneric\_Short.LocKey,1, 3) = CAST(HistoryGeneric.GenType AS varchar)
           AND HistoryGeneric\_Short.IsoLanguageName = 'en-us'
        LEFT OUTER JOIN HistoryTRX ON HistoryTRX.handle = History.handle
        LEFT OUTER JOIN (
           SELECT distinct CONVERT(varchar,G.Data) AS TermStatStr,
                  T.StatusCode AS TermStatCode,
                  T.handle AS TermStat
             FROM Globalization G
                     INNER JOIN TermStatToStatusCode T ON
                        ( G.LocKey = 'HistoryTRX.BTHUSTAT'
                             + RIGHT('0000'
                             + LTRIM(RTRIM(CONVERT(char(3),T.StatusCode))), 3)
                          AND G.IsoLanguageName = 'en-us'
                          AND G.LocKey like 'HistoryTRX.BTHUSTAT%' )
              ) AS HistoryTRX\_Term ON HistoryTRX.TermStat = HistoryTRX\_Term.TermStat
        LEFT OUTER JOIN (
           SELECT distinct CONVERT(varchar,G.Data) AS TermStatStr,
                  T.StatusCode AS TermStatCode,
                  T.handle AS TermStat
             FROM Globalization G
                     INNER JOIN TermStatToStatusCode T ON
                        ( G.LocKey = 'HistoryTRX.BTHUSTAT'
                             + RIGHT('0000'
                             + LTRIM(RTRIM(CONVERT(char(3),T.StatusCode))), 3)
                          AND G.IsoLanguageName = 'en-us'
                          AND G.LocKey like 'HistoryTRX.BTHUSTAT%' )
              ) AS Documents\_Term ON Documents.TermStat = Documents\_Term.TermStat
WHERE
   NOT (
      /\* The outer join on the HistoryPrint, HistoryGeneric, and HistoryTRX results in
       \* rows that just have null history data. One of the three must have a value. If
       \* all are null, the row is a result of the outer joins and the rows have no useable data so they
       \* filtered out. \*/
      HistoryTRX.handle IS NULL
         AND HistoryGeneric.handle IS NULL
         AND HistoryPrint.handle IS NULL )
   AND DocumentUsers.UserName IS NOT NULL

   /\* THIS VALUE is inserted into a NON NULL column in the FAX\_LOG table. \*/
   AND DocumentUsers.UserID IS NOT NULL

   /\* THIS VALUE is inserted into a NON NULL column in the FAX\_LOG table. \*/
   AND Documents.UniqueID IS NOT NULL

   /\* THIS VALUE is inserted into a NON NULL column in the FAX\_LOG table. \*/
   AND History.TRDateTime › 'REPLACE\_WHERE\_CLAUSE\_CRITERIA'

ORDER BY History.TRDateTime" /›

来源:文章一文章二