从 SQL 服务器中的 XML 文件中删除节点
Delete nodes from XML file in SQL Server
我有一个从 Excel 导出的 XML 文件,它存储在 SQL 服务器的 table 的 XML 列中。 Excel 文件有很多工作表,我只想存储其中的几个。有没有办法删除没有我想保留的名称的节点,类似于 NOT IN
?
XML 文件有这个 header:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
这是XML的基本结构:
<Workbook>
<DocumentProperties>
</DocumentProperties>
<ExcelWorkbook>
</ExcelWorkbook>
<Styles>
<Style>
</Style>
</Styles>
<Worksheet ss:Name="Worksheet1">
<Table>
<Column.../>
<Column.../>
<Column.../>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell.../>
</Row>
...
</Table>
</Worksheet>
并且该节点重复了几次。假设我只想保留 "Worksheet3"、"Worksheet4" 和 "Worksheet8"。我怎么能那样做?我想在这种情况下我想使用 update
和 modify()
,类似于 this question。唯一的区别是我想保留某些值,并删除其余值。
这样试试:
DECLARE @xml XML=
'<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties>
</DocumentProperties>
<ExcelWorkbook>
</ExcelWorkbook>
<Styles>
<Style>
</Style>
</Styles>
<Worksheet ss:Name="Worksheet1">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="Worksheet2">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="Worksheet3">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>';
SET @xml.modify('declare namespace dflt="urn:schemas-microsoft-com:office:spreadsheet";
declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet";
delete /dflt:Workbook/dflt:Worksheet[@ss:Name="Worksheet2"]');
SELECT @xml;
您可以将许多过滤器表达式与 "or"...
我有一个从 Excel 导出的 XML 文件,它存储在 SQL 服务器的 table 的 XML 列中。 Excel 文件有很多工作表,我只想存储其中的几个。有没有办法删除没有我想保留的名称的节点,类似于 NOT IN
?
XML 文件有这个 header:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
这是XML的基本结构:
<Workbook>
<DocumentProperties>
</DocumentProperties>
<ExcelWorkbook>
</ExcelWorkbook>
<Styles>
<Style>
</Style>
</Styles>
<Worksheet ss:Name="Worksheet1">
<Table>
<Column.../>
<Column.../>
<Column.../>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell.../>
</Row>
...
</Table>
</Worksheet>
并且该节点重复了几次。假设我只想保留 "Worksheet3"、"Worksheet4" 和 "Worksheet8"。我怎么能那样做?我想在这种情况下我想使用 update
和 modify()
,类似于 this question。唯一的区别是我想保留某些值,并删除其余值。
这样试试:
DECLARE @xml XML=
'<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties>
</DocumentProperties>
<ExcelWorkbook>
</ExcelWorkbook>
<Styles>
<Style>
</Style>
</Styles>
<Worksheet ss:Name="Worksheet1">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="Worksheet2">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
<Worksheet ss:Name="Worksheet3">
<Table>
<Column/>
<Column/>
<Column/>
<Row>
<Cell.../>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
<Cell><Data>...</Data></Cell>
</Row>
</Table>
</Worksheet>
</Workbook>';
SET @xml.modify('declare namespace dflt="urn:schemas-microsoft-com:office:spreadsheet";
declare namespace ss="urn:schemas-microsoft-com:office:spreadsheet";
delete /dflt:Workbook/dflt:Worksheet[@ss:Name="Worksheet2"]');
SELECT @xml;
您可以将许多过滤器表达式与 "or"...