1
我有一個需求來生成一個XML文件,其中包含一個頭文件,詳細信息和尾部部分。此XML文件的源數據將從數據庫中提取,因此我將使用TSQL。數據規範提供了示例XSD文件以及示例XML文件。我創建的XML文件與示例XML文件不匹配。標籤不完全相同。我將如何去複製示例XML文件?我覺得我需要將示例XSD文件合併到一些方法中,但我沒有太多的使用XML的經驗來確切知道。 到目前爲止,我有這樣的事情:用提供的XSD文件創建一個包含Header,Detail和Trailer部分的XML文件
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')
但我需要產生更多類似這樣提供的示例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
下面是示例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>
任何反饋將不勝感激,謝謝!
謝謝您的回覆!腳本的輸出與示例文件幾乎完美匹配,除了第一行。我會如何去頂部添加<?xml version =「1.0」encoding =「UTF-8」standalone =「no」?>? – jaguin
@jaguin,使用'FOR XML PATH',可以將所謂的「處理指令」添加到您的XML('<?把東西放在這裏?>'),但不幸的是不在XML之外。閱讀:http://stackoverflow.com/q/33806454/5089204,這是一個很好的解決方法。請注意,SQL-Server無法直接處理UTF-8。 XML將始終轉換爲unicode。你確定,你需要這條線嗎? – Shnugo
我不確定我是否絕對需要它,因爲 「處理說明」在示例中,所以我只想要它。我找到了一種不同的解決方法,將XML聲明連接到頂部,並將XML作爲varchar(max)類型返回。我相信我也假設返回NULL值,所以我將ELEMENTS XSINIL添加到@Dt XML中,但現在它聲明瞭另一個名稱空間。我目前正在試圖弄清楚如何在沒有聲明另一個名稱空間的情況下獲取那裏的NULL,因爲這已經在根級別完成了。 – jaguin