XML to nodes() Query Generator
Paste a sample XML document and get a SQL Server nodes()/value() query, with column types inferred from the data and child rows shredded with CROSS APPLY.
- Updated
- Markdown
Paste a supplier feed, a SOAP response, or an export file and get a nodes() query that turns it into rows with typed columns. Everything runs in your browser; nothing is sent anywhere.
Notes
- Rows: if you leave the path empty, the rows are the first element that repeats, such as each
<order>in<orders>. Set the path yourself, for example/orders/order, to choose a different one. Paths go down from the root element and are case-sensitive. - Child rows: enter a path relative to each row, such as
lines/line, to get one row per child with the parent’s columns repeated alongside, usingCROSS APPLY. Without one, repeated elements come back as anXMLcolumn fromquery(), and the tool suggests the path to use. - Types are a starting point: they come from the sample only. Values with leading zeros, such as
00123, stayNVARCHARso the zeros aren’t lost. A column that is mostly numbers with the oddn/ais flagged, because a numeric type would fail the whole query on that one value. - Dates: ISO 8601 values become
DATE,DATETIME2, orDATETIMEOFFSET(when they includeZor an offset).trueandfalsebecomeBIT. - Nested elements are flattened into
parent_childcolumns. Untick the option to get each nested element back whole as anXMLcolumn instead. - Namespaces are declared with
WITH XMLNAMESPACES, using the document’s own prefixes where it has them andnsfor a default namespace. - The sample in
DECLAREhas its<?xml ... encoding="UTF-8"?>declaration removed, because SQL Server refuses to cast anN'...'string that declares a different encoding.
The generated query follows Turn XML Into Rows with nodes(). For files of tens of megabytes, use the #temp table option and see Query Large XML Documents Without Timeouts. To see the document as JSON instead, use the XML to JSON Converter.
Was this useful?
Thanks. What was the problem?