sql-server - tsql XML 显式路径
问题描述
我有疑问:
select 1 as Tag ,
0 as Parent ,
'<P>' + 'Name' + ' ' + 'SurName' + '</P>' as "name!1!!CDATA"
from tPA_SysParamSys
for xml explicit, root('customers')
输出是:
<customers>
<name><![CDATA[<P>Name SurName</P>]]></name>
</customers>
但相反,我想:
<customer>
<customers>
<name><![CDATA[<P>Name SurName</P>]]></name>
</customers>
</customer>
没有 EXPLICIT 和 CDATA 但使用 PATH 我可以做到这一点,但不能使用 CDATA。
解决方案
有了这个查询...
with c
as
(
select 1 as CustomerId, 'Name1' as Name1, 'Name2' as Name2
union select 2, 'Name1', 'Name2'
)
select 1 as Tag
, 0 as Parent
, CustomerId as "customer!1!customer_id!hide"
, null as "name!2!!CDATA"
from c
union
select 2 as Tag
, 1 as Parent
, CustomerId
, Name1 + ' ' + Name2 as "name!2!!CDATA"
from tCustomers c
order by "customer!1!customer_id!hide", Tag
for xml explicit, root('customers')
你得到这个 XML ...
<customers>
<customer>
<name><![CDATA[Comp1Name1 Comp1Name2]]></name>
</customer>
<customer>
<name><![CDATA[Comp2Name1 Comp2Name2]]></name>
</customer>
</customers>
推荐阅读
- javascript - 仅选择组中的一个复选框不工作 100%
- django - 如何迭代两个具有相同for循环的子类模型?
- tsql - 如何计算最大值并使sql显示单行
- unit-testing - Intellij:如何在文件夹中运行所有 main() 方法?
- bixby - 是否可以使用列表中的项目动态添加对话驱动程序?
- sql-server - SSRS 订阅表和 ExecutionLog3 视图
- python - 字符串列表,想将它们分成两个列表
- ruby - 如何在 Ruby 中制作以下模式?
- javascript - 无法将 req.body 中的值添加到 mongoose Schema
- xml - fo:inline 内的多行文本