TSQL Shred XML - 使用命名空间 [英] TSQL Shred XML - Working with namespaces

查看:36
本文介绍了TSQL Shred XML - 使用命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我之前关于同一代码块的问题的链接,其中包含一个有效的 shred 示例

好的,我是 C# ASP.NET 开发人员,遵循以下命令:这些命令是获取给定的数据集、分解 XML 并返回列.我认为在 ASP.NET 端进行粉碎更容易,因为我们已经可以访问反序列化器等,以及已知类型的整个复合体,但不,老板说在服务器上粉碎它,返回一个数据集,将数据集绑定到 gridview 的列",所以现在,我正在做我被告知的事情.这一切都是为了阻止那些会过来说糟糕的要求"的人.

Ok, I'm a C# ASP.NET dev following orders: The orders are to take a given dataset, shred the XML and return columns. I've argued that it's easier to do the shredding on the ASP.NET side where we already have access to things like deserializers, etc, and the entire complex of known types, but no, the boss says "shred it on the server, return a dataset, bind the dataset to the columns of the gridview" so for now, I'm doing what I was told. This is all to head off the folks who will come along and say "bad requirements".

当前无效的代码:

如果我们修改上一篇文章以在 XML 元素上包含命名空间,我们就会失去上一篇文章具有的功能......

And if we modify the previous post to include namespaces on the XML elements, we lose the functionality that the previous post has...

DECLARE @table1 AS TABLE (
    ProductID    VARCHAR(10)
  , Name         VARCHAR(20)
  , Color        VARCHAR(20)
  , UserEntered  VARCHAR(20)
  , XmlField     XML
)

INSERT INTO @table1 SELECT '12345','ball','red','john','<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><size xmlns="http://example.com/ns" name="medium"><price>10</price></size><size xmlns="http://example.com/ns" name="large"><price>20</price></size></sizes>'
INSERT INTO @table1 SELECT '12346','ball','blue','adam','<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><size xmlns="http://example.com/ns" name="medium"><price>12</price></size><size xmlns="http://example.com/ns" name="large"><price>25</price></size></sizes>'
INSERT INTO @table1 SELECT '12347','ring','red','john','<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><size xmlns="http://example.com/ns" name="medium"><price>5</price></size><size xmlns="http://example.com/ns" name="large"><price>8</price></size></sizes>'
INSERT INTO @table1 SELECT '12348','ring','blue','adam','<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><size xmlns="http://example.com/ns" name="medium"><price>8</price></size><size xmlns="http://example.com/ns" name="large"><price>10</price></size></sizes>'
INSERT INTO @table1 SELECT '23456','auto','black','ann','<auto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><type xmlns="http://example.com/ns">car</type><wheels xmlns="http://example.com/ns">4</wheels><doors xmlns="http://example.com/ns">4</doors><cylinders xmlns="http://example.com/ns">3</cylinders></auto>'
INSERT INTO @table1 SELECT '23457','auto','black','ann','<auto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><type xmlns="http://example.com/ns">truck</type><wheels xmlns="http://example.com/ns">4</wheels><doors xmlns="http://example.com/ns">2</doors><cylinders xmlns="http://example.com/ns">8</cylinders></auto><auto xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><type xmlns="http://example.com/ns">car</type><wheels xmlns="http://example.com/ns">4</wheels><doors xmlns="http://example.com/ns">4</doors><cylinders xmlns="http://example.com/ns">6</cylinders></auto>'

DECLARE @x XML
-- I think I'm supposed to use WITH XMLNAMESPACES(...) here but I don't know how
SELECT @x = (
    SELECT 
        ProductID
      , Name
      , Color
      , UserEntered
      , XmlField.query('
            for $vehicle in //auto
            return <auto 
                type = "{$vehicle/type}"
                wheels = "{$vehicle/wheels}"
                doors = "{$vehicle/doors}"
                cylinders = "{$vehicle/cylinders}"
            />')
    FROM @table1 table1
    WHERE Name = 'auto'
    FOR XML AUTO
)

SELECT @x

SELECT 
    ProductID    = T.Item.value('../@ProductID', 'varchar(10)')
  , Name         = T.Item.value('../@Name', 'varchar(20)')
  , Color        = T.Item.value('../@Color', 'varchar(20)')
  , UserEntered  = T.Item.value('../@UserEntered', 'varchar(20)')
  , VType        = T.Item.value('@type' , 'varchar(10)')
  , Wheels       = T.Item.value('@wheels', 'varchar(2)')
  , Doors        = T.Item.value('@doors', 'varchar(2)')
  , Cylinders    = T.Item.value('@cylinders', 'varchar(2)')
FROM   @x.nodes('//table1/auto') AS T(Item)

如果我之前的帖子显示有更好的方法来做到这一点,那么我真的需要修改这个问题,但如果这种编码风格很好,我可能会继续这样做.

If my previous post shows there's a much better way to do this, then I really need to revise this question as well, but on the off chance this coding-style is good, I can probably go ahead with this as-is.

推荐答案

DECLARE @x XML;
with xmlnamespaces ('http://www.w3.org/2001/XMLSchema-instance' as xsi
    , 'http://www.w3.org/2001/XMLSchema' as xsd
    , 'http://example.com/ns' as ns) 
SELECT @x = (
    SELECT 
        ProductID
      , Name
      , Color
      , UserEntered
      , XmlField.query('
            for $vehicle in //auto
            return <auto 
                type = "{$vehicle/ns:type}"
                wheels = "{$vehicle/ns:wheels}"
                doors = "{$vehicle/ns:doors}"
                cylinders = "{$vehicle/ns:cylinders}"
            />')
    FROM @table1 table1
    WHERE Name = 'auto'
    FOR XML AUTO
)

这篇关于TSQL Shred XML - 使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆