TSQL XML - 将属性添加到父节点

时间:2017-06-12 15:27:53

标签: sql sql-server xml tsql

我正在尝试向最外面的父节点添加属性。附上是我的代码,我的结果和我的预期结果,以便更好地理解我想要做的事情。非常感谢任何帮助!

我的代码:

        SELECT

         (
         SELECT
            (SELECT 
               'quoteRequest' AS '@service'
               FOR XML PATH('request'), TYPE),

        (SELECT
          (SELECT 
            'Add' as Action,
            'generation' AS 'Date/@type',
            'MM/DD/YYYY HH:MM' as Date
        FOR XML PATH(''), TYPE, ROOT ('Header')),

        (SELECT 
          (SELECT 
            '' AS Status, 
            'CustAcctNum' AS 'Enterprise/@customerAcctNum',
            'Name' AS 'Enterprise/@name',
            'Enterprise Name' AS 'Enterprise'
                FOR XML PATH(''), TYPE),
         (SELECT 
        'quoteNumber' AS 'ReferenceNumber/@type',
        'true' AS 'ReferenceNumber/@isPrimary',
        'VALUE' AS ReferenceNumber
        FOR XML PATH('ReferenceNumbers'),
        TYPE),

        (SELECT 
        'Special Instructions' AS 'Comment/@type',
        'Comment' AS Comment
        FOR XML PATH('Comments'),
        TYPE),

        (SELECT 
        'Forklift' AS 'EquipmentCode/@desc',
        '1' AS 'EquipmentCode/@qty',
        'DV' AS EquipmentCode
        FOR XML PATH('EquipmentList'),
        TYPE),

        (SELECT(
           SELECT(
             SELECT(
                SELECT
                  'earliest' AS '@type',
                  GETDATE()
                   FOR XML PATH('Date'), TYPE),
               (SELECT
                  'latest' AS '@type',
                  GETDATE()
                   FOR XML PATH('Date'), TYPE)
                FOR XML PATH('Pickup'),TYPE),
          (SELECT(
            SELECT(
              SELECT
                'earliest' AS '@type',
                GETDATE()
                 FOR XML PATH('Date'), TYPE),
             (SELECT
                'latest' AS '@type',
                GETDATE()
                 FOR XML PATH('Date'), TYPE)
               FOR XML PATH('Drop'),TYPE))
             FOR XML PATH('Dates'), TYPE)),

        (SELECT
          (SELECT
            (SELECT 
            'false' AS '@isresidential',
            '' AS 'Alias',
            'Name' AS 'Name',
            'AddrLine1' AS 'AddrLine1',
            'AddrLine2' AS 'AddrLine2',
            'City' AS 'City',
            'StateProvince' AS 'StateProvince',
            'PostalCode' AS 'PostalCode',
            'CountryCode' AS 'CountryCode',
            (SELECT
               'Name' AS 'Name',
                 (SELECT 
                    'phone' AS 'ContactMethod/@type',
                    '1' AS 'ContactMethod/@sequenceNum',
                    '8675309' AS 'ContactMethod'
                     FOR XML PATH('ContactMethods'), TYPE)
                FOR XML PATH('Contact'), TYPE, ROOT('Contacts')),
            (SELECT 
               'Comments' as 'Comments'
                FOR XML PATH (''), TYPE)

            FOR XML PATH('Address'), TYPE)

        FOR XML PATH(''), TYPE, ROOT('Shipper'))),

        (SELECT
           (SELECT

            (SELECT 
            'false' AS '@isresidential',
            '' AS 'Alias',
            'Name' AS 'Name',
            'AddrLine1' AS 'AddrLine1',
            'AddrLine2' AS 'AddrLine2',
            'City' AS 'City',
            'StateProvince' AS 'StateProvince',
            'PostalCode' AS 'PostalCode',
            'CountryCode' AS 'CountryCode',
            (SELECT
               'Name' AS 'Name',
                 (SELECT 
                    'phone' AS 'ContactMethod/@type',
                    '1' AS 'ContactMethod/@sequenceNum',
                    '8675309' AS 'ContactMethod'
                     FOR XML PATH('ContactMethods'), TYPE)
                FOR XML PATH('Contact'), TYPE, ROOT('Contacts')),
            (SELECT 
               'Comments' as 'Comments'
                FOR XML PATH (''), TYPE)

            FOR XML PATH('Address'), TYPE)

        FOR XML PATH(''), TYPE, ROOT('Consignee'))),
        (
        SELECT(

        SELECT(
            (SELECT 
            'false' AS '@stackable',
            '1' AS '@sequence',
            (SELECT 
               'UOM' AS 'Quantity/@units',
               '1' AS 'Quantity'
                FOR XML PATH(''), TYPE),
            (SELECT 
               'UOM' AS 'Weight/@units',
               '1' AS 'Weight'
                FOR XML PATH(''), TYPE),
                (SELECT 
                '1' AS '@height',   
                '1' AS '@units',   
                '1' AS '@width',
                '1' AS '@length'  
                FOR XML PATH('Dimensions'), TYPE),
                (SELECT
                'freghtClass' AS '@freightClass',
                '1' AS '@sequence',
                (SELECT 
                   'UOM' AS 'Weight/@units',
                   '1' AS 'Weight'
                    FOR XML PATH(''), TYPE),
                    (SELECT 
                    '1' AS '@height',   
                    '1' AS '@units',   
                    '1' AS '@width',
                    '1' AS '@length'  
                    FOR XML PATH('Dimensions'), TYPE),
                '1' AS 'Quantity'
                FOR XML PATH('Item'), TYPE, ROOT('Items'))

            FOR XML PATH('HandlingUnit'), TYPE))

          FOR XML PATH(''), TYPE, ROOT('HandlingUnits'))),

        (
        SELECT(
          SELECT 'Method' AS Method
             FOR XML PATH(''), TYPE),
            (SELECT 'true' AS '@thirdParty',

            (SELECT 
            'false' AS '@isresidential',
            '' AS 'Alias',
            'Name' AS 'Name',
            'AddrLine1' AS 'AddrLine1',
            'AddrLine2' AS 'AddrLine2',
            'City' AS 'City',
            'StateProvince' AS 'StateProvince',
            'PostalCode' AS 'PostalCode',
            'CountryCode' AS 'CountryCode',
            (SELECT
               'Name' AS 'Name',
                 (SELECT 
                    'phone' AS 'ContactMethod/@type',
                    '1' AS 'ContactMethod/@sequenceNum',
                    '8675309' AS 'ContactMethod'
                     FOR XML PATH('ContactMethods'), TYPE)
                FOR XML PATH('Contact'), TYPE, ROOT('Contacts'))

            FOR XML PATH('Address'), TYPE)
           FOR XML PATH('BillTo'), TYPE)

        FOR XML PATH(''), TYPE, ROOT('Payment'))

        FOR XML PATH(''), TYPE, ROOT('Shipment'))

        FOR XML PATH(''), TYPE, ROOT('QuoteRequest'))

        FOR XML PATH(''), TYPE, ROOT('request'))

我的结果。请注意前两行:

      **<request>
        <request service="quoteRequest" />**
        <QuoteRequest>
          <Header>
            <Action>Add</Action>
            <Date type="generation">MM/DD/YYYY HH:MM</Date>
          </Header>
          <Shipment>
            <Status />
            <Enterprise customerAcctNum="CustAcctNum" name="Name">Enterprise Name</Enterprise>
            <ReferenceNumbers>
              <ReferenceNumber type="quoteNumber" isPrimary="true">VALUE</ReferenceNumber>
            </ReferenceNumbers>
            <Comments>
              <Comment type="Special Instructions">Comment</Comment>
            </Comments>
            <EquipmentList>
              <EquipmentCode desc="Forklift" qty="1">DV</EquipmentCode>
            </EquipmentList>
            <Dates>
              <Pickup>
                <Date type="earliest">2017-06-12T08:20:27.107</Date>
                <Date type="latest">2017-06-12T08:20:27.107</Date>
              </Pickup>
              <Drop>
                <Date type="earliest">2017-06-12T08:20:27.107</Date>
                <Date type="latest">2017-06-12T08:20:27.107</Date>
              </Drop>
            </Dates>
            <Shipper>
              <Address isresidential="false">
                <Alias />
                <Name>Name</Name>
                <AddrLine1>AddrLine1</AddrLine1>
                <AddrLine2>AddrLine2</AddrLine2>
                <City>City</City>
                <StateProvince>StateProvince</StateProvince>
                <PostalCode>PostalCode</PostalCode>
                <CountryCode>CountryCode</CountryCode>
                <Contacts>
                  <Contact>
                    <Name>Name</Name>
                    <ContactMethods>
                      <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                    </ContactMethods>
                  </Contact>
                </Contacts>
                <Comments>Comments</Comments>
              </Address>
            </Shipper>
            <Consignee>
              <Address isresidential="false">
                <Alias />
                <Name>Name</Name>
                <AddrLine1>AddrLine1</AddrLine1>
                <AddrLine2>AddrLine2</AddrLine2>
                <City>City</City>
                <StateProvince>StateProvince</StateProvince>
                <PostalCode>PostalCode</PostalCode>
                <CountryCode>CountryCode</CountryCode>
                <Contacts>
                  <Contact>
                    <Name>Name</Name>
                    <ContactMethods>
                      <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                    </ContactMethods>
                  </Contact>
                </Contacts>
                <Comments>Comments</Comments>
              </Address>
            </Consignee>
            <HandlingUnits>
              <HandlingUnit stackable="false" sequence="1">
                <Quantity units="UOM">1</Quantity>
                <Weight units="UOM">1</Weight>
                <Dimensions height="1" units="1" width="1" length="1" />
                <Items>
                  <Item freightClass="freghtClass" sequence="1">
                    <Weight units="UOM">1</Weight>
                    <Dimensions height="1" units="1" width="1" length="1" />
                    <Quantity>1</Quantity>
                  </Item>
                </Items>
              </HandlingUnit>
            </HandlingUnits>
            <Payment>
              <Method>Method</Method>
              <BillTo thirdParty="true">
                <Address isresidential="false">
                  <Alias />
                  <Name>Name</Name>
                  <AddrLine1>AddrLine1</AddrLine1>
                  <AddrLine2>AddrLine2</AddrLine2>
                  <City>City</City>
                  <StateProvince>StateProvince</StateProvince>
                  <PostalCode>PostalCode</PostalCode>
                  <CountryCode>CountryCode</CountryCode>
                  <Contacts>
                    <Contact>
                      <Name>Name</Name>
                      <ContactMethods>
                        <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                      </ContactMethods>
                    </Contact>
                  </Contacts>
                </Address>
              </BillTo>
            </Payment>
          </Shipment>
        </QuoteRequest>
      </request>

我的预期结果,请注意第一行/节点是请求。与上面得到的结果不同,我需要节点和属性在同一行:

      **<request service=quoteRequest">**
        <QuoteRequest>
          <Header>
            <Action>Add</Action>
            <Date type="generation">MM/DD/YYYY HH:MM</Date>
          </Header>
          <Shipment>
            <Status />
            <Enterprise customerAcctNum="CustAcctNum" name="Name">Enterprise Name</Enterprise>
            <ReferenceNumbers>
              <ReferenceNumber type="quoteNumber" isPrimary="true">VALUE</ReferenceNumber>
            </ReferenceNumbers>
            <Comments>
              <Comment type="Special Instructions">Comment</Comment>
            </Comments>
            <EquipmentList>
              <EquipmentCode desc="Forklift" qty="1">DV</EquipmentCode>
            </EquipmentList>
            <Dates>
              <Pickup>
                <Date type="earliest">2017-06-12T08:20:27.107</Date>
                <Date type="latest">2017-06-12T08:20:27.107</Date>
              </Pickup>
              <Drop>
                <Date type="earliest">2017-06-12T08:20:27.107</Date>
                <Date type="latest">2017-06-12T08:20:27.107</Date>
              </Drop>
            </Dates>
            <Shipper>
              <Address isresidential="false">
                <Alias />
                <Name>Name</Name>
                <AddrLine1>AddrLine1</AddrLine1>
                <AddrLine2>AddrLine2</AddrLine2>
                <City>City</City>
                <StateProvince>StateProvince</StateProvince>
                <PostalCode>PostalCode</PostalCode>
                <CountryCode>CountryCode</CountryCode>
                <Contacts>
                  <Contact>
                    <Name>Name</Name>
                    <ContactMethods>
                      <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                    </ContactMethods>
                  </Contact>
                </Contacts>
                <Comments>Comments</Comments>
              </Address>
            </Shipper>
            <Consignee>
              <Address isresidential="false">
                <Alias />
                <Name>Name</Name>
                <AddrLine1>AddrLine1</AddrLine1>
                <AddrLine2>AddrLine2</AddrLine2>
                <City>City</City>
                <StateProvince>StateProvince</StateProvince>
                <PostalCode>PostalCode</PostalCode>
                <CountryCode>CountryCode</CountryCode>
                <Contacts>
                  <Contact>
                    <Name>Name</Name>
                    <ContactMethods>
                      <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                    </ContactMethods>
                  </Contact>
                </Contacts>
                <Comments>Comments</Comments>
              </Address>
            </Consignee>
            <HandlingUnits>
              <HandlingUnit stackable="false" sequence="1">
                <Quantity units="UOM">1</Quantity>
                <Weight units="UOM">1</Weight>
                <Dimensions height="1" units="1" width="1" length="1" />
                <Items>
                  <Item freightClass="freghtClass" sequence="1">
                    <Weight units="UOM">1</Weight>
                    <Dimensions height="1" units="1" width="1" length="1" />
                    <Quantity>1</Quantity>
                  </Item>
                </Items>
              </HandlingUnit>
            </HandlingUnits>
            <Payment>
              <Method>Method</Method>
              <BillTo thirdParty="true">
                <Address isresidential="false">
                  <Alias />
                  <Name>Name</Name>
                  <AddrLine1>AddrLine1</AddrLine1>
                  <AddrLine2>AddrLine2</AddrLine2>
                  <City>City</City>
                  <StateProvince>StateProvince</StateProvince>
                  <PostalCode>PostalCode</PostalCode>
                  <CountryCode>CountryCode</CountryCode>
                  <Contacts>
                    <Contact>
                      <Name>Name</Name>
                      <ContactMethods>
                        <ContactMethod type="phone" sequenceNum="1">8675309</ContactMethod>
                      </ContactMethods>
                    </Contact>
                  </Contacts>
                </Address>
              </BillTo>
            </Payment>
          </Shipment>
        </QuoteRequest>
      </request>

我尝试过很多不同的东西,但仍然无法让它正常工作。有人有什么想法吗?非常感谢!

1 个答案:

答案 0 :(得分:0)

很抱歉,我想退后一步,质疑你为什么要使数据变得复杂,因为选择一遍又一遍?我会把它分解成可能是Shipment,Shipper等的部分,如果它非常复杂并且制作一个巨大的Tablix但你应该能够使元素独立于任何树结构,然后根据需要读取它们。在模拟中,我可以表明我的意思。使用更明确的XML路径()&#39;你有更多选择来创建人工节点,并且不必为了创建节点名而嵌套选择。 EG:

DECLARE @People TABLE (PersonId INT IDENTITY, PersonName VARCHAR(128));

INSERT INTO @People (PersonName) VALUES ('Brett'), ('John'), ('Mark'), ('Shawn'), ('Ryan'), ('Kevin');

DECLARE @XmlResults XML = 

(
SELECT 
  CAST(
    Replace(
    (SELECT 
      'quoteRequest' AS "@service"
    , 'Add' AS "QuoteRequest/Header/Action/*"
    , 'generation' AS "QuoteRequest/Header/Date/@type"
    , 'MM/DD/YYYY HH:MM' AS "QuoteRequest/Header/Date/*"
    , '' AS "QuoteRequest/Shipment/Status/*"
    , 'CustAcctNum' AS "QuoteRequest/Shipment/Enterprise/@customerAcctNum"
    , 'Name' AS "QuoteRequest/Shipment/Enterprise/@name"
    , PersonName AS "QuoteRequest/Shipment/Enterprise/*"
    , 'earliest' AS "QuoteRequest/Dates/Pickup/Date/@type"
    , FORMAT(GETDATE(), 'yyyy-MM-ddTHH:mm:ss.fff', 'en-US') AS "QuoteRequest/Dates/Pickup/Date/*"
    , 'latest' AS "QuoteRequest/Dates/Pickup/DateTwo/@type"
    , FORMAT(GETDATE(), 'yyyy-MM-ddTHH:mm:ss.fff', 'en-US') AS "QuoteRequest/Dates/Pickup/DateTwo/*"
    From @People
    FOR XML PATH('request'), ROOT('Requests'))
    , 'DateTwo', 'Date')
  AS XML)
)

--XML as is
SELECT @XmlResults

--XML parsed for one request at a time in my series.
SELECT x.query('.')
FROM @XmlResults.nodes('Requests/request') AS y(x)