ยังไงฉัน dynamically เปลี่ยน XML โครงสร้างของข้อความในภาษา sql

0

คำถาม

ฉันต้องการเป็นภาษา sql สคริปต์นั่นมันจะต้องถอนข้อความ XML จาก DB[varchar(แม็กซ์)],ตรวจสอบมันและปรับปรุงมัน ถ้า มันพอแบบเจาะจงสถานการณ์.

จินตนาการว่าของ xml อยู่ในรูปแบบต่อไปนี้:

<root>
  <level1>
    <level2>
      <level3 />
      <level3 />
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for XYZ">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="this one is not of interest">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for ABC">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
</root>

ดังนั้น,สิ่งที่ฉันต้องการจะทำคือการปรับปรุงทั้งหมดส่วนประกอบที่ชื่อ"level6"และคนไหนมีแอททริบิวต์ที่เรียกว่า"ที่นี่"ของใครค่า เริ่มต้น กับ"ตอนนี้คือเวลา". ดังนั้น,นั่นควรจะตรงกันแค่สองส่วนประกอบทางด้านบน

แต่นั่นไม่ใช่คนเดียวที่รูปแบบการเลือก. รายการของตัวเลือกต้องไม่มีส่ว <option here="now" />. ดังนั้น,นั่นควรออกไปจากพวกเรากับหนึ่งอีลีเมนต์เพื่อปรับปรุง

<level6 here="now is the time for XYZ">
    <options>
        <option this="that" />
        <option me="you" />
    </options>
 </level6>

ไปที่ธาตุฉันก็ต้องการเพิ่มคนหายตัวไป <option here="now" />ดังนั้นกลายเป็น:

<level6 here="now is the time for XYZ">
    <options>
        <option this="that" />
        <option me="you" />
        <option here="now" />
    </options>
 </level6>

งั้นผลสุดท้ายก็จบลงควรจะเป็น:

 <root>
  <level1>
    <level2>
      <level3 />
      <level3 />
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for XYZ">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />      // <- this one new
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="this one is not of interest">
              <options>
                <option this="that" />
                <option me="you" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
  <level1>
    <level2>
      <level3>
        <level4>
          <level5>
            <level6 here="now is the time for ABC">
              <options>
                <option this="that" />
                <option me="you" />
                <option here="now" />
              </options>
            </level6>
          </level5>
        </level4>
      </level3>
    </level2>
  </level1>
</root>

คิดว่าฉันสามารถอ่านข้อมูลออกมาจาก DB เข้าไปในข้อความและผมรู้วิธีที่จะปรับปรุงที่ DB ดังนั้นมันเป็นจริงจัดการให้ xml ข้อความในภาษา sql(ภาษา sql เซิร์ฟเวอร์).

sql-server tsql xml xquery
2021-11-23 17:17:51
1

คำตอบที่ดีที่สุด

1

คุณสามารถใช้ XML DML(ทำการแก้ไขข้อมูล)บ .modify ฟังก์ชันเพื่อเปลี่ยน XML.

SET @xml.modify('
  insert <option here="now" />
  as last into
  ( /root/level1/level2/level3/level4/level5/level6
     [substring(@here, 1, 15) = "now is the time"]
     /options [not(/option[@here = "now"])]
   )[1]');

มันทำงานตาม:

  • insert <option here="now" /> นี่เป็นค่าเราเป็นฤติกรรมโรคจิต--การแทร
  • as last into มันเป็นหลังจากเด็กคนอื่นโหนดต่างๆของที่เลือกหนึ่ง
  • /root/level1/level2/level3/level4/level5/level6 เรื่องนี้หลุดว่าพวกเรา level6 โหนด
  • [substring(@here, 1, 15) = "now is the time"] predicates งโหนดที่มี here ลักษณะเฉพาะเริ่มต้นกับมันค่า. คุณต้องแก้ไขความยาวพารามิเตอร์ให้ตรงกับค่าที่คุณเปรียบเทียบ. มันไม่มี LIKE ใน XQuery
  • /options [not(/option[@here = "now"])] เรามองหาการ options โหนดซึ่ง ไม่มี option ลูกซึ่งส่มี here="now" แอททริบิวต์
  • [1] คนแรกที่ช่างโหนด

ถ้าคุณต้องการจะแก้ไขหลายโหนดต่างๆภายในหนึ่งเอกสาร XML,คุณต้องวิ่งนี้อยู่ในวง

DECLARE @i int = 20; --max nodes

WHILE @xml.exist('
  /root/level1/level2/level3/level4/level5/level6
     [substring(@here, 1, 15) = "now is the time"]
     /options [not(option[@here = "now"])]
   ') = 1
BEGIN

    SET @xml.modify('
      insert <option here="now" /> as last into
      ( /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       )[1]');
     
    SET @i -= 1;
    IF @i = 0
        BREAK;
END;

คุณยังสามารถทำเรื่องนี้สำหรับทั้งหมดโต๊ะ

DECLARE @i int = 20; --max nodes

WHILE EXISTS (SELECT 1
    FROM YourTable
    WHERE XmlColumn.exist('
      /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       ') = 1)
BEGIN

    UPDATE t
    SET XmlColumn.modify('
      insert <option here="now" /> as last into
      ( /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       )[1]')
    FROM YourTable t
    WHERE XmlColumn.exist('
      /root/level1/level2/level3/level4/level5/level6
         [substring(@here, 1, 15) = "now is the time"]
         /options [not(option[@here = "now"])]
       ') = 1;
     
    SET @i -= 1;
    IF @i = 0
        BREAK;
END;

สำหรับมากขนาดใหญ่ datasets มันอาจจะเป็นเร็วกว่าที่จะสร้างใหม่ทั้ง XML ใช้ XQuery กับคนพิเศษของโหนดเพิ่มการใช้ตู XML.

db<>ลื่นไหล

2021-11-23 23:41:04

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่