简体   繁体   English

使用提供的 XSD 文件创建包含 Header、Detail 和 Trailer 部分的 XML 文件

[英]Creating an XML file containing Header, Detail, and Trailer sections with provided XSD file

I have a requirement to produce an XML file that contains a header, detail, and trailer section.我需要生成一个包含标题、详细信息和预告片部分的 XML 文件。 The source data for this XML file will be extracted from a database so I will be using TSQL.此 XML 文件的源数据将从数据库中提取,因此我将使用 TSQL。 The data specifications provide a sample XSD file as well as a sample XML file.数据规范提供了一个示例 XSD 文件以及一个示例 XML 文件。 The XML file I have created does not match the sample XML file.我创建的 XML 文件与示例 XML 文件不匹配。 The tags aren't quite the same.标签不太一样。 How would I go about replicating the sample XML file?我将如何复制示例 XML 文件? I am feeling like I need to incorporate the sample XSD file some how but I don't have much experience working with XML to know for sure.我觉得我需要以某种方式合并示例 XSD 文件,但我没有太多使用 XML 的经验来确定。 So far I have something like this:到目前为止,我有这样的事情:

DECLARE @tmpHeader TABLE
([Header Code] varchar(15) NULL,
[Preferred Provider List Creation Date] datetime NULL,
[ACO Program Code] int NULL)

INSERT INTO @tmpHeader
([Header Code],[Preferred Provider List Creation Date],[ACO Program Code])
VALUES
('HDR_PFPRVDR',CONVERT(date,GETDATE()),'21')

DECLARE @tmpTrailer TABLE
([Trailer Code] varchar(15) NULL,
[Preferred Provider List File Creation Date] datetime NULL,
[Detail Record Count] int NULL)

INSERT INTO @tmpTrailer
([Trailer Code],[Preferred Provider List File Creation Date],[Detail Record Count])
SELECT 
'TRL_PFPRVDR',CONVERT(date,GETDATE()),(select count(*) from (
SELECT distinct
     [ACO Identifier] = 'V130'
    ,[ACO Preferred Provider TIN] = case when TIN.VendorTaxID is NULL then VEN.VendorTaxID else TIN.VendorTaxID end
    ,[Old ACO Preferred Provider TIN] = TIN.Old_TaxID
    ,[ACO Organization Preferred Provider NPI] = NULL
    ,[ACO Individual Preferred Provider NPI] = PRV.NPINumber
    ,[ACO Preferred Provider Shared_Savings Program Effective Date] = CDA.EffDate
    ,[ACO Preferred Provider Shared Savings Program Termination Date] = nullif(CDA.TermDate,'')
FROM Provider PRV (readuncommitted)
    LEFT JOIN Vendor VEN (readuncommitted) ON 
        PRV.Vendor_UniqueID = VEN.Vendor_UniqueID
        and
        VEN.ods_row_current = PRV.ods_row_current
    LEFT JOIN TIN (readuncommitted) ON
        TIN.Vendor_UniqueID = PRV.Vendor_UniqueID
    JOIN CDA (readuncommitted) ON
        CDA.LicenseID = TIN.VendorShortName and CDA.TaxID = TIN.VendorTaxID
    WHERE 
        PRV.ods_row_current = 1
        ) as A)

DECLARE @TempExportTable TABLE
(
  Header XML,
  Detail XML,
  Trailer XML
)


INSERT INTO @TempExportTable VALUES
(
(SELECT [Header Code],[Preferred Provider List Creation Date],[ACO Program Code] FROM @tmpHeader FOR XML AUTO, ELEMENTS),
(SELECT distinct
     [ACO Identifier] = 'V130'
    ,[ACO Preferred Provider TIN] = case when TIN.VendorTaxID is NULL then VEN.VendorTaxID else TIN.VendorTaxID end
    ,[Old ACO Preferred Provider TIN] = TIN.Old_TaxID
    ,[ACO Organization Preferred Provider NPI] = NULL
    ,[ACO Individual Preferred Provider NPI] = PRV.NPINumber
    ,[ACO Preferred Provider Shared_Savings Program Effective Date] = CDA.EffDate
    ,[ACO Preferred Provider Shared Savings Program Termination Date] = nullif(CDA.TermDate,'')
FROM PROVIDER PRV (readuncommitted)
    LEFT JOIN VENDOR (readuncommitted) ON 
        PRV.Vendor_UniqueID = VEN.Vendor_UniqueID
        and
        VEN.ods_row_current = PRV.ods_row_current
    LEFT JOIN TIN (readuncommitted) ON
        TIN.Vendor_UniqueID = PRV.Vendor_UniqueID
    JOIN CDA (readuncommitted) ON
        CDA.LicenseID = TIN.VendorShortName and CDA.TaxID = TIN.VendorTaxID
    WHERE 
        PRV.ods_row_current = 1
        FOR XML AUTO, ELEMENTS),
(SELECT [Trailer Code],[Preferred Provider List File Creation Date],[Detail Record Count] FROM @tmpTrailer FOR XML AUTO, ELEMENTS)
)
SELECT 
   Header as '*',
   Detail as '*',
   Trailer as '*' 
from @TempExportTable 
FOR XML PATH('ExportList')

But I need to produce something more similar to this provided sample XML file:但我需要生成与此提供的示例 XML 文件更相似的内容:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<ACOParticipantData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Header>
    <HeaderCode>HDR_PFPRVDR</HeaderCode>
    <FileCreationDate>20160101</FileCreationDate>
    <ACOProgCode>21</ACOProgCode>
  </Header>
  <Participants>
    <Participant>
      <ACO_ID>V199</ACO_ID>
      <TIN>123456789</TIN>
      <Old_TIN>987654321</Old_TIN>
      <Org_NPI>1234567890</Org_NPI>
      <Ind_NPI>1234567890</Ind_NPI>
      <CCN>123456</CCN>
      <PRG_Eff_Dt>20160101</PRG_Eff_Dt>
      <PRG_Term_Dt>20161231</PRG_Term_Dt>
    </Participant>
  </Participants>
  <Trailer>
    <TrailerCode>TRL_PFPRVDR</TrailerCode>
    <FileCreationDate>20160101</FileCreationDate>
    <RecordCount>1</RecordCount>
  </Trailer>
</ACOParticipantData>Sample 

Here is the sample XSD file:这是示例 XSD 文件:

<?xml version="1.0" encoding="UTF-8"?>           
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="qualified">
<xsd:element name="Header" type="HeaderType"/>
        <xsd:element name="Participants" type="ParticipantsType"/>
        <xsd:element name="Participant" type="ParticipantType"/>
        <xsd:element name="Trailer" type="TrailerType"/>

        <xsd:element name="ACOParticipantData">                
    <xsd:complexType>
                                <xsd:sequence>
            <xsd:element ref="Header"/>
                                            <xsd:element ref="Participants" minOccurs="0" maxOccurs="1"/>
                                            <xsd:element ref="Trailer"/>
                                </xsd:sequence>
                    </xsd:complexType>
        </xsd:element>

        <xsd:complexType name="HeaderType">
                    <xsd:sequence>
                                <xsd:element name="HeaderCode" type="HeaderCodeENUM"/>
                                <xsd:element name="FileCreationDate">
            <xsd:simpleType>
                                                        <xsd:restriction base="xsd:string"/>
                                            </xsd:simpleType>
                                </xsd:element>
                                <xsd:element name="ACOProgCode" type="ACOProgCodeType"/>                                    
                    </xsd:sequence>
        </xsd:complexType>

        <xsd:complexType name="ParticipantsType">
                    <xsd:sequence>
                                <xsd:element ref="Participant" maxOccurs="unbounded"/>
                    </xsd:sequence>
        </xsd:complexType>

        <xsd:complexType name="ParticipantType">                   
    <xsd:sequence>  
        <xsd:element name="ACO_ID" type="OrgType"/>
        <xsd:element name="TIN" type="xsd:string"/>
        <xsd:element name="Old_TIN" nillable="true" type="xsd:string"/>
        <xsd:element name="Org_NPI" nillable="true" type="xsd:string"/>
        <xsd:element name="Ind_NPI" nillable="true" type="xsd:string"/>
        <xsd:element name="CCN" nillable="true" type="xsd:string"/>
        <xsd:element name="PRG_Eff_Dt" type="DateType"/>
        <xsd:element name="PRG_Term_Dt" nillable="true" type="xsd:string"/>
    </xsd:sequence>
        </xsd:complexType>

        <xsd:complexType name="TrailerType">
    <xsd:sequence>
                                <xsd:element name="TrailerCode" type="TrailerCodeENUM"/>
                                <xsd:element name="FileCreationDate">                                     
                                            <xsd:simpleType>
                                                        <xsd:restriction base="xsd:string"/>
                                            </xsd:simpleType>
                                </xsd:element>                         
                                <xsd:element name="RecordCount">
                                            <xsd:simpleType>
                                                        <xsd:restriction base="xsd:integer">
                                                                    <xsd:minInclusive value="0"/>
                                                                    <xsd:maxInclusive value="9999999"/>
                                                        </xsd:restriction>
                                            </xsd:simpleType>
                                </xsd:element>                         
                    </xsd:sequence>
        </xsd:complexType>

        <xsd:simpleType name="HeaderCodeENUM">
                    <xsd:restriction base="xsd:string">
                       <xsd:pattern value="HDR_PFPRVDR"/>
                    </xsd:restriction>
        </xsd:simpleType>

        <xsd:simpleType name="ACOProgCodeType">
                    <xsd:restriction base="xsd:string">
                       <xsd:pattern value="21"/>
                    </xsd:restriction>
        </xsd:simpleType>

        <xsd:simpleType name="TrailerCodeENUM">
                    <xsd:restriction base="xsd:string">
                                <xsd:pattern value="TRL_PFPRVDR"/>
                    </xsd:restriction>
        </xsd:simpleType>

        <xsd:simpleType name="DateType">
                    <xsd:restriction base="xsd:string">
                                <xsd:pattern value="\d{8}"/>
                    </xsd:restriction>
        </xsd:simpleType>

        <xsd:simpleType name="OrgType">
                    <xsd:restriction base="xsd:string">
                                <xsd:pattern value="V\d{3}"/>
                    </xsd:restriction>
        </xsd:simpleType>

</xsd:schema>

Any feedback would be greatly appreciated, thank you!任何反馈将不胜感激,谢谢!

Without your real table's structure and sample data it is like reading the magic glass bulb, but this should be close:如果没有真实表的结构和示例数据,就像阅读魔法玻璃灯泡一样,但这应该很接近:

I created another temp-table to mock your details data我创建了另一个临时表来模拟您的详细信息数据

You might avoid the declared XML-variables, but in this case you'd get the declaration of "xsi" in each sub-node repeatedly.您可能会避免声明 XML 变量,但在这种情况下,您会在每个子节点中重复获得“xsi”的声明。 No error, but annoying...没有错误,但很烦人...

Maybe you do not even need the "xsi"-namespace...也许您甚至不需要“xsi”-命名空间...

DECLARE @tmpHeader TABLE
([Header Code] varchar(15) NULL,
[Preferred Provider List Creation Date] datetime NULL,
[ACO Program Code] int NULL);

INSERT INTO @tmpHeader
([Header Code],[Preferred Provider List Creation Date],[ACO Program Code])
VALUES
('HDR_PFPRVDR',CONVERT(date,GETDATE()),'21');

DECLARE @tmpTrailer TABLE
([Trailer Code] varchar(15) NULL,
[Preferred Provider List File Creation Date] datetime NULL,
[Detail Record Count] int NULL);

INSERT INTO @tmpTrailer
([Trailer Code],[Preferred Provider List File Creation Date],[Detail Record Count])
VALUES ('TRL_PFPRVDR',CONVERT(date,GETDATE()),100); --Replaced your `SELECT COUNT(*) FROM ...` with a fix value

DECLARE @tmpDetail TABLE(ACO_ID VARCHAR(100),TIN BIGINT,Old_TIN BIGINT,Org_NPI BIGINT,Ind_NPI BIGINT,CCN INT,PRG_Eff_Dt DATE,PRG_Term_Dt DATE);
INSERT INTO @tmpDetail VALUES('V199',123456789,987654321,1234567890,1234567890,123456,'20160101','20161231');

--To avoid repeated namespace declarations:
DECLARE @Hd XML=
      ( 
        SELECT h.[Header Code] AS HeaderCode
              ,h.[Preferred Provider List Creation Date] AS FileCreationDate
              ,h.[ACO Program Code] AS ACOProgCode
        FROM @tmpHeader AS h      
        FOR XML PATH('Header'),TYPE
      );
DECLARE @Dt XML=
      (
        SELECT d.ACO_ID
              ,d.TIN
              ,d.Old_TIN
              ,d.Org_NPI
              ,d.Ind_NPI
              ,d.CCN
              ,d.PRG_Eff_Dt
              ,d.PRG_Term_Dt 
        FROM @tmpDetail AS d
        FOR XML PATH('Participant'),ROOT('Participants'),TYPE
      );
DECLARE @Tr XML=
      (
        SELECT t.[Trailer Code] AS TrailerCode
              ,t.[Preferred Provider List File Creation Date] AS FileCreationDate
              ,t.[Detail Record Count] AS RecordCount
        FROM @tmpTrailer AS t
        FOR XML PATH('Trailer')
      ); 

WITH XMLNAMESPACES('http://www.w3.org/2001/XMLSchema-instance' AS xsi)
SELECT @Hd,@Dt,@Tr
FOR XML PATH('ACOParticipantData');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM