简体   繁体   English

xml显式类型属性

[英]xml explicit type attribute

I try to conclude my SQL for a feed output... but one more complication rise to me. 我试图总结一下我的SQL的供稿输出...但是又增加了一个麻烦。 First of all I want to appreciate that I'm not an expert of SQL for XML output... but hope in the help of someone. 首先,我要感谢我不是XML输出SQL的专家...但是希望有人帮助。

Using this query 使用此查询

SELECT 
    1487997 as ExternalId,
    'This is a name' as [Name]
INTO #sample

DROP TABLE #colors
SELECT 
    1487997 as ExternalId,
    'BV_FE_FAMILY:Green' as [Color]
INTO #colors
UNION 
SELECT
    1487997 as ExternalId,
    'Green' as [Color]

SELECT
    1 as Tag,
    NULL as Parent,
    NULL AS [EliminaTAG!1], -- empty root element
    NULL AS [Product!2!ExternalId!element],
    NULL AS [Product!2!Name!cdata],
    NULL AS [Product!2!]
UNION ALL
SELECT 
    2 AS Tag,
    1 AS Parent,
    NULL,
    ExternalId,  
    [Name],
    (
     SELECT  
       c.[Color] as Value
     FROM #colors  c 
     WHERE (ff.ExternalId = c.ExternalId)   
     FOR XML PATH('Attribute'), ROOT('Attributes'), TYPE)
FROM #sample ff 
FOR XML EXPLICIT

I'm close to my scope... but I need a id="BV_FE_FAMILY" and id="BV_FE_EXPAND" into TAG... 我已经接近范围了...但是我需要将id =“ BV_FE_FAMILY”和id =“ BV_FE_EXPAND”放入TAG ...

<Attributes>
<Attribute id="BV_FE_FAMILY">
    <Value>Green</Value>
<Attribute>
<Attribute id="BV_FE_EXPAND">
    <Value>BV_FE_FAMILY:Green</Value>
<Attribute>
</Attributes>

My result is... miss "ID=....." 我的结果是...想念“ ID = .....”

    <EliminaTAG>
      <Product>
        <ExternalId>1487997</ExternalId>
        <Name><![CDATA[This is a name]]></Name>
        <Attributes xmlns="">
          <Attribute>
              <Value>Green</Value>
          </Attribute>
          <Attribute>
              <Value>BV_FE_FAMILY:Green</Value>
          </Attribute>
        </Attributes>
      </Product>
   </EliminaTAG>

Thanks to everyone who will give me right advise. 感谢所有会给我正确建议的人。 ALEN, Italy 意大利ALEN

As pointed out in my comment, FOR XML EXPLICIT is a rather clumsy choice. 正如我在评论中所指出的那样, FOR XML EXPLICIT是一个相当笨拙的选择。 But there are some requirements, which cannot be solved otherwise... 但是有一些要求,否则无法解决...

So, if you really need this, you might solve it a bit easier: 因此,如果您确实需要此功能,则可以更轻松地解决它:

SELECT
    1 as Tag,
    NULL as Parent,
    ff.ExternalId AS [Product!1!ExternalId!element],
    ff.Name AS [Product!1!Name!cdata],
    (
     SELECT  'SomeValueForId' AS [@id]
            ,c.Color AS [Value]
     FROM #colors c
     WHERE c.ExternalId=ff.ExternalId
     FOR XML PATH('Attribute'),ROOT('Attributes'),TYPE
    ) AS [Product!1!]
FROM #sample ff 
FOR XML EXPLICIT,ROOT('EliminaTAG');

The result 结果

<EliminaTAG>
  <Product>
    <ExternalId>1487997</ExternalId>
    <Name><![CDATA[This is a name]]></Name>
    <Attributes xmlns="">
      <Attribute id="SomeValueForId">
        <Value>BV_FE_FAMILY:Green</Value>
      </Attribute>
      <Attribute id="SomeValueForId">
        <Value>Green</Value>
      </Attribute>
    </Attributes>
  </Product>
</EliminaTAG>

I do not get the logic, where the content of your id attributes comes from, this is up to you. 我不了解逻辑, id属性的内容来自哪里,这取决于您。

Thank you very much for your help and advise... I resolved with his code, from yours: 非常感谢您的帮助和建议...我用您的代码解决了他的代码:

SELECT
    1 as Tag,
    NULL as Parent,
    ff.ExternalId AS [Product!1!ExternalId!element],
    ff.Name AS [Product!1!Name!cdata],
    (
     SELECT  
            CASE
                WHEN CHARINDEX('BV_FE_FAMILY', c.Color) > 0 THEN 'BV_FE_EXPAND' 
                ELSE 'BV_FE_FAMILY'
            END AS [@id]
            ,c.Color AS [Value]
     FROM #colors c
     WHERE c.ExternalId=ff.ExternalId
     FOR XML PATH('Attribute'),ROOT('Attributes'),TYPE
    ) AS [Product!1!]
FROM #sample ff 
FOR XML EXPLICIT;

this is the result of my output 这是我输出的结果

<Product>
  <ExternalId>1487997</ExternalId>
  <Name><![CDATA[This is a name]]></Name>
  <Attributes xmlns="">
    <Attribute id="BV_FE_EXPAND">
      <Value>BV_FE_FAMILY:Green</Value>
    </Attribute>
    <Attribute id="BV_FE_FAMILY">
      <Value>Green</Value>
    </Attribute>
  </Attributes>
</Product>

It's possible remove this xmlns="" ?. 可以删除此xmlns =“”?。 Currently, I'll remove it during ETL process for the creation of XML file directly into SSIS... but if there is a clean way directly from T-SQL, I would prefer... Thanks anyway Shnugo! 当前,我将在ETL过程中将其删除,以便直接将XML文件创建到SSIS中。但是如果直接从T-SQL中获得一种干净的方法,我还是希望...不管怎么说,Shnugo! ALEN 艾伦

PS: this is another version using (as you said) FROM XML PATH, but how can I insert cdata for some elements? PS:这是使用(如您所说)FROM XML PATH的另一个版本,但是如何为某些元素插入cdata? this choice has remove xmlns=""... 此选择已删除xmlns =“” ...

SELECT
    ff.ExternalId ,
    ff.Name,
    (
     SELECT  
            CASE
                WHEN CHARINDEX('BV_FE_FAMILY', c.Color) > 0 THEN 'BV_FE_EXPAND' 
                ELSE 'BV_FE_FAMILY'
            END AS [@id]
            ,c.Color AS [Value]
     FROM #colors c
     WHERE c.ExternalId=ff.ExternalId
     FOR XML PATH('Attribute'),ROOT('Attributes'),TYPE
    ) 
FROM #sample ff 
FOR XML PATH('Product'), ROOT('Products');

THIS is my final solution (CAST + REPLACE + CAST): 这是我的最终解决方案(CAST + REPLACE + CAST):

SELECT
    ff.ExternalId ,
    ff.Name,
    CAST(REPLACE(CAST((
     SELECT  
            CASE
                WHEN CHARINDEX('BV_FE_FAMILY', c.Color) > 0 THEN 'BV_FE_EXPAND' 
                ELSE 'BV_FE_FAMILY'
            END AS [@id]
            ,c.Color AS [Value]
     FROM #colors c
     WHERE c.ExternalId=ff.ExternalId
     FOR XML PATH('Attribute'),ROOT('Attributes'),TYPE
    )  as varchar(MAX)),' xmlns=""','') as xml)
FROM #sample ff 
FOR XML PATH('Product'), ROOT('Products');

my clean result output: 我的干净结果输出:

<Products>
  <Product>
    <ExternalId>1487997</ExternalId>
    <Name>This is a name</Name>
    <Attributes>
      <Attribute id="BV_FE_EXPAND">
        <Value>BV_FE_FAMILY:Green</Value>
      </Attribute>
      <Attribute id="BV_FE_FAMILY">
        <Value>Green</Value>
      </Attribute>
    </Attributes>
  </Product>
</Products>

ALEN 艾伦

This is the final code: 这是最终代码:

SELECT
    ff.ExternalId ,
    ff.Name as Name,
    'http://www.mysite.it/imgege.jpg' as URL,
    CAST(CAST((
     SELECT  
            CASE
                WHEN CHARINDEX('BV_FE_FAMILY', c.Color) > 0 THEN 'BV_FE_EXPAND' 
                ELSE 'BV_FE_FAMILY'
            END AS [@id]
            ,c.Color AS [Value]
     FROM #colors c
     WHERE c.ExternalId=ff.ExternalId
     FOR XML PATH('Attribute'),ROOT('Attributes'),TYPE
    )  as nvarchar(MAX)) as xml)
FROM #sample ff 
FOR XML PATH('Product'), ROOT('Products');

and my output... 和我的输出...

<Products>
  <Product>
    <ExternalId>1487997</ExternalId>
    <Name>This is a name</Name>
    <URL>http://www.mysite.it/imgege.jpg</URL>
    <Attributes>
      <Attribute id="BV_FE_EXPAND">
        <Value>BV_FE_FAMILY:Green</Value>
      </Attribute>
      <Attribute id="BV_FE_FAMILY">
        <Value>Green</Value>
      </Attribute>
    </Attributes>
  </Product>
</Products>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM