Ssis
使用 SSIS 從 XML 文件獲取數據的問題
我正在從 Internet 上的 XML 文件中獲取數據,如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?> -<exchangerates type="Exhange rates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" refamt="1" refcur="DKK" author="Bank"> -<dailyrates id="2016-07-22"> <currency rate="505.47" desc="Australian dollars" code="AUD"/> <currency rate="380.36" desc="Bulgarian lev" code="BGN"/> <currency rate="205.85" desc="Brazilian real" code="BRL"/> <currency rate="515.31" desc="Canadian dollars" code="CAD"/> <currency rate="684.99" desc="Swiss francs" code="CHF"/> <currency rate="101.21" desc="Chinese yuan renminbi" code="CNY"/> </dailyrates> </exchangerates>
XSD 文件如下所示:
<?xml version="1.0"?> <xs:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="exchangerates"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" name="dailyrates"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" name="currency"> <xs:complexType> <xs:attribute name="code" type="xs:string" use="optional" /> <xs:attribute name="desc" type="xs:string" use="optional" /> <xs:attribute name="rate" type="xs:string" use="optional" /> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="id" type="xs:date" use="optional" /> </xs:complexType> </xs:element> </xs:sequence> <xs:attribute name="type" type="xs:string" use="optional" /> <xs:attribute name="author" type="xs:string" use="optional" /> <xs:attribute name="refcur" type="xs:string" use="optional" /> <xs:attribute name="refamt" type="xs:unsignedByte" use="optional" /> </xs:complexType> </xs:element> </xs:schema>
但在 SSIS(來自 XML Source Task)中,我得到如下數據。最後一列顯示“1”而不是“日期”(在本例中為 2016-07-22)。我需要此日期位於“dailyrates_Id”列的每一行中。任何想法如何解決這個問題?
code desc rate dailyrates_Id AUD Australian dollars 505.47 1 BGN Bulgarian lev 380.36 1 BRL Brazilian real 205.85 1 CAD Canadian dollars 515.31 1
這些屬性位於不同的級別,因此這就是 XML Source 的工作方式。你有幾個選擇:
- 使用帶有合併連接的排序選項將兩個部分重新組合在一起,如下所示:
- 在變數中擷取 XML,將其作為參數傳遞給儲存的 proc,然後在 proc 中將其切碎,使用如下所示:
DECLARE @xml XML = '<?xml version="1.0" encoding="iso-8859-1"?> <exchangerates type="Exhange rates" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" refamt="1" refcur="DKK" author="Bank"> <dailyrates id="2016-07-22"> <currency rate="505.47" desc="Australian dollars" code="AUD"/> <currency rate="380.36" desc="Bulgarian lev" code="BGN"/> <currency rate="205.85" desc="Brazilian real" code="BRL"/> <currency rate="515.31" desc="Canadian dollars" code="CAD"/> <currency rate="684.99" desc="Swiss francs" code="CHF"/> <currency rate="101.21" desc="Chinese yuan renminbi" code="CNY"/> </dailyrates> </exchangerates>' SELECT dr.c.value('@id', 'DATE') id, c.c.value('@rate', 'MONEY') rate, c.c.value('@desc', 'VARCHAR(50)') [desc], c.c.value('@code', 'VARCHAR(50)') code FROM @xml.nodes('exchangerates/dailyrates') dr(c) CROSS APPLY dr.c.nodes('currency') c(c)
祝你好運!