【问题标题】:xml as <option type="x">y</option>xml 作为 <option type="x">y</option>
【发布时间】:2015-12-04 12:05:20
【问题描述】:

我有一个带有属性的表

prodid  type    val
1       colour  red
1       size    large
2       colour  blue

作为一个更大的查询的一部分,我有一个 xml 结构:

(SELECT type "@type",val  from t_prodattr pa where pa.prodid=product.id 
 FOR xml path('attribute'), root('attributes'), type),

给予

 <attributes>
    <attribute type="colour">
      <val>red</val>
    </attribute>
    <attribute type="size">
      <val>small</val>
    </attribute>
  </attributes>

我希望 xml 成为

<attributes>
    <attribute type="colour">red</attribute>
    <attribute type="size">small</attribute>
</attributes>

甚至

<attributes>
    <colour>red</colour>
    <size>small</size>
</attributes>

【问题讨论】:

    标签: sql-server xml sql-server-2012


    【解决方案1】:

    使用'*' 作为val 的别名。

    SELECT type '@type',
           val as '*'  
    from ...
    

    Columns with a Name Specified as a Wildcard Character

    更新:

    如果您想要 type 列中的元素名称,您必须部分通过连接字符串来构建 XML。

    declare @T table
    (
      prodid int,
      type varchar(10),
      val varchar(10)
    );
    
    insert into @T values
    (1,       'colour',  'red  '),
    (1,       'size',    'la&ge'),
    (2,       'colour',  'blue ');
    
    select cast('<'+T.type+'>'+(select T.val as '*' for xml path(''))+'</'+T.type+'>' as xml)
    from @T as T
    for xml path(''), root('attributes'), type
    

    结果:

    <attributes>
      <colour>red  </colour>
      <size>la&amp;ge</size>
      <colour>blue </colour>
    </attributes>
    

    这部分(select T.val as '*' for xml path('')) 负责为&amp;&lt;&gt; 等XML 特殊字符创建实体。

    【讨论】:

    • 所有答案都很好,选择这个是因为它回答了两个选项
    【解决方案2】:

    架构

    DECLARE @t TABLE
      (
              [type]    VARCHAR(10) ,
              Val VARCHAR(10)
      )
    
    INSERT  INTO @t
    VALUES  ( 'colour', 'red' ),
            ( 'size', 'large' ),
            ( 'colours', 'blue' )
    

    查询

    SELECT  type "@type" ,
            Val AS '*'
    FROM    @t
    FOR     XML PATH('attribute') ,
                ROOT('attributes') ,
                TYPE;
    

    输出

    <attributes>
      <attribute type="colour">red</attribute>
      <attribute type="size">large</attribute>
      <attribute type="colours">blue</attribute>
    </attributes>
    

    【讨论】:

      【解决方案3】:
      DECLARE @t TABLE (
          prodid INT,
          [type] VARCHAR(50),
          val VARCHAR(50)
      )
      
      INSERT INTO @t
      VALUES
          (1, 'colour', 'red'),
          (1, 'size', 'large'),
          (2, 'colour', 'blue')
      
      SELECT
          (
              SELECT [@type] = [type], [text()] = val
              FROM @t t2
              WHERE t2.prodid = t1.prodid
              FOR XML PATH('attribute'), root('attributes'), TYPE
          )
      FROM (
          SELECT DISTINCT prodid
          FROM @t
      ) t1
      FOR XML PATH('')
      

      结果 -

      <attributes>
        <attribute type="colour">red</attribute>
        <attribute type="size">large</attribute>
      </attributes>
      <attributes>
        <attribute type="colour">blue</attribute>
      </attributes>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-14
        • 2022-07-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多