【发布时间】:2017-06-09 19:56:24
【问题描述】:
我正在创建 T-SQL 代码来检索 XML 格式的信息。
下面显示了 T-SQL 代码的当前输出:
<ReportingGroup>
<AccountReport>
<AccountNumber>12312334</AccountNumber>
<AccountClosed>false</AccountClosed>
<AccountHolder>
<Individual>
<ResCountryCode>USA</ResCountryCode>
<TIN>23423545</TIN>
<Name>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Name>
<Address>
<CountryCode>USA</CountryCode>
<AddressFix>
<Street>112323 ljlkjlkyoid</Street>
<BuildingIdentifier>NULL</BuildingIdentifier>
<PostCode>NULL</PostCode>
<City>USA</City>
</AddressFix>
<AddressFree>NULL</AddressFree>
</Address>
<BirthInfo />
</Individual>
</AccountHolder>
<FINAL_BALANCE_USD></FINAL_BALANCE_USD>
<NAME_TEXT>John Doe</NAME_TEXT>
<NAME_ADDR_TEXT>87 ljhadliwhdlahd</NAME_ADDR_TEXT>
<NAME_CITY>U.S.A.</NAME_CITY>
</AccountReport>
</ReportingGroup>
如何修改 T-SQL 以在 xml 标记中添加 ftc: 和 sfc:,如下面的输出?
SELECT
AccountNumber
, AccountClosed
, (
SELECT --level 2
ResCountryCode
, TIN
, (
SELECT --level 3
FirstName
LastName
for xml path('Name'), TYPE
)
, (
SELECT --level 3
CountryCode
,(
SELECT --LEVEL 4
Street
, BuildingIdentifier
, PostCode
, City
for xml path('AddressFix'), TYPE
)
, AddressFree
for xml path('Address'), TYPE
)
, (
SELECT --level 3
BirthDate
for xml path('BirthInfo'), TYPE
)
for xml path('Individual'), root('AccountHolder'), TYPE
)
From test_table
for xml path('AccountReport'), root('ReportingGroup')
输出:
<ftc:ReportingGroup>
<ftc:AccountReport>
<ftc:AccountNumber>12312334</ftc:AccountNumber>
<ftc:AccountClosed>false</ftc:AccountClosed>
<ftc:AccountHolder>
<ftc:Individual>
<sfa:ResCountryCode>USA</sfa:ResCountryCode>
<sfa:TIN>23423545</sfa:TIN>
<sfa:Name>
<sfa:FirstName>John</sfa:FirstName>
<sfa:LastName>Doe</sfa:LastName>
</sfa:Name>
<sfa:Address>
<sfa:CountryCode>USA</sfa:CountryCode>
<sfa:AddressFix>
<sfa:Street>112323 ljlkjlkyoid</sfa:Street>
<sfa:BuildingIdentifier>NULL</sfa:BuildingIdentifier>
<sfa:PostCode>NULL</sfa:PostCode>
<sfa:City>USA</sfa:City>
</sfa:AddressFix>
<sfa:AddressFree>NULL</sfa:AddressFree>
</sfa:Address>
</ftc:Individual>
</ftc:AccountHolder>
<ftc:FINAL_BALANCE_USD></ftc:FINAL_BALANCE_USD>
<ftc:NAME_TEXT>John Doe</ftc:NAME_TEXT>
<ftc:NAME_ADDR_TEXT>87 ljhadliwhdlahd</ftc:NAME_ADDR_TEXT>
<ftc:NAME_CITY>U.S.A.</ftc:NAME_CITY>
</ftc:AccountReport>
</ftc:ReportingGroup>
【问题讨论】:
-
只是作为一个提示:你的输出是无效的......如果你想使用命名空间前缀,这些命名空间必须声明......
-
您好,感谢您的回答。请注意,我还没有测试发布的答案,我今天会。
标签: sql-server xml tsql xpath namespaces