【问题标题】:How to replace XML string into another XML string如何将 XML 字符串替换为另一个 XML 字符串
【发布时间】:2023-03-30 10:25:01
【问题描述】:
<recurrence>
  <interval>1</interval>
  <unit>O</unit>
  <firsttime>2021-02-12T17:42:00</firsttime>
  <lasttime>1900-01-01T12:00:00</lasttime>
</recurrence>
<output>
  <outputformat>TXT</outputformat>
  <delimiter>,</delimiter>
  <filename>testfile1</filename>
  <path>\\AIR-LAP-700053\TestGEMOutput</path>
  <appendtofile />
  <content>
    <type>NORMAL</type>
    <storedprocedure />
    <outputitems>
      <outputitem>
        <field>
          <tablename>VW_DOCUMENT</tablename>
          <fieldname>GUID</fieldname>
        </field>
        <format>
          <type>CHAR</type>
          <specification />
        </format>
      </outputitem>
      <outputitem>
        <field>
          <tablename>VW_DOCUMENT</tablename>
          <fieldname>PHYSICAL_DOC_GUID</fieldname>
        </field>
        <format>
          <type>CHAR</type>
          <specification />
        </format>
      </outputitem>
      <outputitem>
        <field>
          <tablename>VW_DOCUMENT</tablename>
          <fieldname>DOC_TYPE</fieldname>
        </field>
        <format>
          <type>CHAR</type>
          <specification />
        </format>
      </outputitem>
    </outputitems>
    <criteria>
      <criterion>
        <field>
          <tablename>VW_DOCUMENT</tablename>
          <fieldname>DOC_TYPE</fieldname>
        </field>
        <restriction>
          <type>=</type>
          <data>Default</data>
          <functioncode />
        </restriction>
      </criterion>
    </criteria>
  </content>
</output>

我想把上面的 XML 转换成下面的预期输出:

<ExportJobDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Recurrence>
    <Interval>1</Interval>
    <Unit>W</Unit>
    <FirstTime>2021-02-19T12:36:00Z</FirstTime>
    <LastTime>2021-02-24T12:36:00Z</LastTime>
  </Recurrence>
  <Output>
    <OutputFormat>TXT</OutputFormat>
    <Delimiter>/</Delimiter>
    <FileName>ColdIndexLog</FileName>
    <Path>\\MUM-LAP-1092\3PrtyUpgImages\Images\Data\cold\Logs</Path>
    <OverwriteFile>O</OverwriteFile>
    <Content>
      <ContentType>NORMAL</ContentType>
      <OutputItems>
        <ExportJobOutputItem>
          <Field>
            <TableName>Document</TableName>
            <FieldName>Document / Doc_Ref</FieldName>
          </Field>
          <Format>
            <Specification />
          </Format>
        </ExportJobOutputItem>
        <ExportJobOutputItem>
          <Field>
            <TableName>Document</TableName>
            <FieldName>Document / Doc_Type</FieldName>
          </Field>
          <Format>
            <Specification />
          </Format>
        </ExportJobOutputItem>
      </OutputItems>
      <Criteria>
        <ExportJobCriterion>
          <Field>
            <TableName>Document </TableName>
            <FieldName>Document / Doc_Type</FieldName>
          </Field>
          <Restriction>
            <RestrictionType>Less than</RestrictionType>
            <Data>ABC</Data>
          </Restriction>
        </ExportJobCriterion>
      </Criteria>
    </Content>
  </Output>
</ExportJobDefinition>

我已经检查了 SQL 中的替换功能,但 XML 中的“类型”等元素存在于不同的 xml 父标签中。

我试图获取所有属性,它的值也使用了下面的几个 XmlQueries 但没有任何结果:

--DECLARE @temp table (TableName VARCHAR(MAX), FieldName varchar(max))
--INSERT INTO @temp
SELECT tablename = Node.Data.value('(tablename)[1]', 'VARCHAR(MAX)'),
       fieldname = Node.Data.value('(fieldname)[1]', 'VARCHAR(MAX)')
FROM    @xmlData.nodes('/output/content/outputitems/outputitem/field') Node(Data)

--select * from @temp

--select * from @temp FOR XML PATH('')

--DECLARE @temp1 table (Type VARCHAR(MAX), Specification varchar(max))
--INSERT INTO @temp1
SELECT [type] = Node.Data.value('(type)[1]', 'VARCHAR(MAX)'),
       specification = Node.Data.value('(specification)[1]', 'VARCHAR(MAX)')
FROM    @xmlData.nodes('/output/content/outputitems/outputitem/format') Node(Data)

--select * from @temp1

--select * from @temp FOR XML PATH('')


--WITH R AS (
--SELECT
--    ElementName = T.x.value('local-name(.)', 'nvarchar(255)'),
--    ElementValue = T.x.value('text()[1]', 'nvarchar(255)'),

--    AttrName = R.x.value('local-name(.)', 'nvarchar(255)'),
--    AttrValue = R.x.value('(.)[1]', 'nvarchar(255)')
--FROM
--    @xmlData.nodes('//*') AS T(x)
--    OUTER APPLY
--    T.x.nodes('@*') AS R(x)
--)
--SELECT
--  CASE WHEN ElementValue IS NULL THEN AttrName ELSE ElementName END AS [Name],
--    COALESCE(ElementValue, AttrValue) AS [Value]
--FROM
--  R
--WHERE
--  ElementValue IS NOT NULL
--  OR AttrValue IS NOT NULL
--GO

                         

【问题讨论】:

    标签: c# asp.net xml


    【解决方案1】:

    记住这两点: 1. Microsoft SQL Server 是一个非常强大的工具,不仅因为 T-SQL,还因为它集成的工具。 2. XQuery & XPath 是你的朋友。 (:

    因此,如果 @Input 是包含您的第一个 Xml 数据的变量,您可以运行以下 Select 语句,并在您的 Xml 变量上应用 XPath query

    Declare @Input Xml = '<recurrence>
      <interval>1</interval>
      <unit>O</unit>......
    .......
    </recurrence>'
    
    Select  @Input.query('
    <ExportJobDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Recurrence>
        <Interval>{ data(/recurrence/interval[1]) }</Interval>
        <Unit>{ data(/recurrence/unit[1]) }</Unit>
        <FirstTime>{ data(/recurrence/firsttime[1]) }</FirstTime>
      </Recurrence>
      <Output>
        <OutputFormat>TXT</OutputFormat>
        <Delimiter>/</Delimiter>
        <FileName>ColdIndexLog</FileName>
        <Path>\\MUM-LAP-1092\3PrtyUpgImages\Images\Data\cold\Logs</Path>
        <OverwriteFile>O</OverwriteFile>
        <Content>
          <ContentType>NORMAL</ContentType>
          <OutputItems>
        {
            for $i in /output[1]/content[1]/outputitems[1]/outputitem
              return <ExportJobOutputItem>
                  <Field>
                    <TableName>{ data($i/field[1]/tablename) }</TableName>
                    <FieldName>{ data($i/field[1]/fieldname) }</FieldName>
                  </Field>
                  <Format>
                    <Specification />
                  </Format>
                  </ExportJobOutputItem>
        }
          </OutputItems>
        </Content>
      </Output>
    </ExportJobDefinition>
    ')
    

    结果将是一个标量 Xml 值:

        <ExportJobDefinition xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Recurrence>
        <Interval>1</Interval>
        <Unit>O</Unit>
        <FirstTime>2021-02-12T17:42:00</FirstTime>
      </Recurrence>
      <Output>
        <OutputFormat>TXT</OutputFormat>
        <Delimiter>/</Delimiter>
        <FileName>ColdIndexLog</FileName>
        <Path>\\MUM-LAP-1092\3PrtyUpgImages\Images\Data\cold\Logs</Path>
        <OverwriteFile>O</OverwriteFile>
        <Content>
          <ContentType>NORMAL</ContentType>
          <OutputItems>
            <ExportJobOutputItem>
              <Field>
                <TableName>VW_DOCUMENT</TableName>
                <FieldName>GUID</FieldName>
              </Field>
              <Format>
                <Specification />
              </Format>
            </ExportJobOutputItem>
            <ExportJobOutputItem>
              <Field>
                <TableName>VW_DOCUMENT</TableName>
                <FieldName>PHYSICAL_DOC_GUID</FieldName>
              </Field>
              <Format>
                <Specification />
              </Format>
            </ExportJobOutputItem>
            <ExportJobOutputItem>
              <Field>
                <TableName>VW_DOCUMENT</TableName>
                <FieldName>DOC_TYPE</FieldName>
              </Field>
              <Format>
                <Specification />
              </Format>
            </ExportJobOutputItem>
          </OutputItems>
        </Content>
      </Output>
    </ExportJobDefinition>
    

    您可以阅读有关 XQuery 的所有信息(特别是寻找 Xml 构造,并学习运算符/函数)here on Microsoft's website

    【讨论】:

    • 感谢您的回复,但它是否支持重复节点 DocumentDocument / Doc_Ref
    • 是的。您可以使用 for 循环,针对查询运行它并构建一组全新的元素。正如我所提到的,值得阅读参考资料以了解您需要什么。希望它有所帮助。 (:
    • 添加了一个示例以方便参考。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 2016-02-21
    • 2011-12-21
    • 2019-05-14
    • 2012-07-16
    • 2016-05-23
    相关资源
    最近更新 更多