我需要在称为Group的子元素中进行迭代。
DECLARE @XMLData XML = N'<Nodes>
<Sheet FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Model1">
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Consensus Model">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1225" NodeText="Net Revenue" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1226" NodeText="Cost of Revenue">
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="01" NodeText="As % of Net Revenue" />
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="02" NodeText="Year over Year Growth" />
</LineItem>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Test Group1">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1225" NodeText="Test_Group_LI" />
</Group>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Test Group2"/>
</Group>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Segment Details">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1227" NodeText="Cost of Revenue-GAAP" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1228" NodeText="Gross Profit" />
</Group>
</Sheet>
<Sheet FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Model2">
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Key Financials">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1235" NodeText="Total Operating Expenses-GAAP" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1236" NodeText="EBITDA">
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="01" NodeText="BM_Test1" />
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="02" NodeText="BM_Test2" />
</LineItem>
</Group>
</Sheet>
</Nodes>';
我在MSDN论坛上发布了此问题,并且Yitzhak Khabinsky先生在那里回答了我的情况。哪个可行,但是当存在嵌套组时,代码不会插入嵌套组数据。
我的XML将具有Sheet,Group,Lineitem和BM元素。只能嵌套组。一个组可以具有多个嵌套子组,而这些子组可以具有Lineitem或BM元素。
所以层次结构是:
所以只有Group可以嵌套Nth个子元素。
现在我正在共享从MSDN论坛获得的代码,该代码正在运行,但是当存在嵌套组时,则不会插入这些嵌套组数据。我的xquery
知识不好。我认为对代码的少量更改将使该代码适用于嵌套组。
DECLARE @XMLData XML = N'<Nodes>
<Sheet FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Model1">
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Consensus Model">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1225" NodeText="Net Revenue" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1226" NodeText="Cost of Revenue">
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="01" NodeText="As % of Net Revenue" />
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="02" NodeText="Year over Year Growth" />
</LineItem>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Test Group1">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1225" NodeText="Test_Group_LI" />
</Group>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Test Group2"/>
</Group>
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Segment Details">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1227" NodeText="Cost of Revenue-GAAP" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1228" NodeText="Gross Profit" />
</Group>
</Sheet>
<Sheet FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Model2">
<Group FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" NodeText="Key Financials">
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1235" NodeText="Total Operating Expenses-GAAP" />
<LineItem FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" LineItemID="1236" NodeText="EBITDA">
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="01" NodeText="BM_Test1" />
<BM FontName="" FontStyle="" FontSize="" UnderLine="false" BGColor="" BMID="02" NodeText="BM_Test2" />
</LineItem>
</Group>
</Sheet>
</Nodes>';
DECLARE @tblCSM_Details TABLE
(
[CSM_ID] [int] NOT NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[ParentID] [int] NULL,
[Type] [varchar](30) NULL,
[DisplayInCSM] [varchar](200) NULL,
[FontName] [varchar](max) NULL,
[FontStyle] [varchar](max) NULL,
[FontSize] [varchar](max) NULL,
[UnderLine] [varchar](max) NULL,
[BGColor] [varchar](max) NULL,
[LineItemID] [int] NULL,
[BMID] [int] NULL,
[ColOrder] [int] NULL
);
DECLARE @SheetID INT,@GroupID INT,@LineItemID INT, @BMID INT;
DECLARE @SheetStartIndex INT, @SheetCount INT;
DECLARE @GroupStartIndex INT, @GroupCount INT;
DECLARE @LineitemStartIndex INT, @LineitemCount INT;
DECLARE @BMStartIndex INT, @BMCount INT;
SET @SheetStartIndex = 1;
SET @SheetCount = @XMLData.value('count(/Nodes/Sheet)', 'INT');
WHILE @SheetStartIndex <= @SheetCount BEGIN --Inserting sheet data
INSERT INTO @tblCSM_Details(CSM_ID,[ParentID],[Type],[DisplayInCSM],[FontName],[FontStyle],[FontSize],[UnderLine],[BGColor],[LineItemID],[BMID],[ColOrder])
SELECT 1,0, c.value('local-name(.)','VARCHAR(30)') --'SHEET'
, c.value('@NodeText', 'VARCHAR(MAX)')
, c.value('@FontName', 'VARCHAR(MAX)')
, c.value('@FontStyle', 'VARCHAR(MAX)')
, c.value('@FontSize', 'VARCHAR(MAX)')
, c.value('@UnderLine', 'VARCHAR(MAX)')
, c.value('@BGColor', 'VARCHAR(MAX)')
, 0,0, @SheetStartIndex
FROM @XMLData.nodes('/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]') AS t(c);
SELECT @SheetID = SCOPE_IDENTITY();
--Inserting Group data
SET @GroupStartIndex = 1;
SET @GroupCount = @XMLData.value('count(/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group)', 'INT');
WHILE @GroupStartIndex <= @GroupCount BEGIN --Inserting Group data
INSERT INTO @tblCSM_Details(CSM_ID,[ParentID],[Type],[DisplayInCSM],[FontName],[FontStyle],[FontSize],[UnderLine],[BGColor],[LineItemID],[BMID],[ColOrder])
SELECT 1,@SheetID, c.value('local-name(.)','VARCHAR(30)') --'GROUP'
, c.value('@NodeText', 'VARCHAR(MAX)')
, c.value('@FontName', 'VARCHAR(MAX)')
, c.value('@FontStyle', 'VARCHAR(MAX)')
, c.value('@FontSize', 'VARCHAR(MAX)')
, c.value('@UnderLine', 'VARCHAR(MAX)')
, c.value('@BGColor', 'VARCHAR(MAX)')
, 0,0, @GroupStartIndex
FROM @XMLData.nodes('/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group[position() = sql:variable("@GroupStartIndex")]') AS t(c);
SELECT @GroupID = SCOPE_IDENTITY();
--Inserting LineItem data
SET @LineitemStartIndex = 1;
SET @LineitemCount = @XMLData.value('count(/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group[position() = sql:variable("@GroupStartIndex")]/LineItem)', 'INT');
WHILE @LineitemStartIndex <= @LineitemCount BEGIN
INSERT INTO @tblCSM_Details(CSM_ID,[ParentID],[Type],[DisplayInCSM],[FontName],[FontStyle],[FontSize],[UnderLine],[BGColor],[LineItemID],[BMID],[ColOrder])
SELECT 1,@GroupID, c.value('local-name(.)','VARCHAR(30)') --'LINEITEM'
, c.value('@NodeText', 'VARCHAR(MAX)')
, c.value('@FontName', 'VARCHAR(MAX)')
, c.value('@FontStyle', 'VARCHAR(MAX)')
, c.value('@FontSize', 'VARCHAR(MAX)')
, c.value('@UnderLine', 'VARCHAR(MAX)')
, c.value('@BGColor', 'VARCHAR(MAX)')
, c.value('@LineItemID', 'INT')
, 0, @LineitemStartIndex
FROM @XMLData.nodes('/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group[position() = sql:variable("@GroupStartIndex")]/LineItem[position() = sql:variable("@LineitemStartIndex")]') AS t(c);
SELECT @LineItemID = SCOPE_IDENTITY();
--Inserting BM data
SET @BMStartIndex = 1;
SET @BMCount = @XMLData.value('count(/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group[position() = sql:variable("@GroupStartIndex")]/LineItem[position() = sql:variable("@LineitemStartIndex")]/BM)', 'INT');
WHILE @BMStartIndex <= @BMCount BEGIN --Inserting sheet data
INSERT INTO @tblCSM_Details(CSM_ID,[ParentID],[Type],[DisplayInCSM],[FontName],[FontStyle],[FontSize],[UnderLine],[BGColor],[LineItemID],[BMID],[ColOrder])
SELECT 1,@LineItemID, c.value('local-name(.)','VARCHAR(30)') --BM
, c.value('@NodeText', 'VARCHAR(MAX)')
, c.value('@FontName', 'VARCHAR(MAX)')
, c.value('@FontStyle', 'VARCHAR(MAX)')
, c.value('@FontSize', 'VARCHAR(MAX)')
, c.value('@UnderLine', 'VARCHAR(MAX)')
, c.value('@BGColor', 'VARCHAR(MAX)')
, 0
, c.value('@BMID', 'INT')
, @BMStartIndex
FROM @XMLData.nodes('/Nodes/Sheet[position() = sql:variable("@SheetStartIndex")]/Group[position() = sql:variable("@GroupStartIndex")]/LineItem[position() = sql:variable("@LineitemStartIndex")]/BM[position() = sql:variable("@BMStartIndex")]') AS t(c);
SET @BMStartIndex += 1;
END;
SET @LineitemStartIndex += 1;
END;
SET @GroupStartIndex += 1;
END;
SET @SheetStartIndex += 1;
END;
SELECT * FROM @tblCSM_Details;
请有人告诉我,代码应该进行哪些更改,以便它可以处理嵌套的组元素。
这里是另一种处理方式。没有循环。 😊