Remove few lines from file by means of powershell - powershell

If I want to remove from document lines with some string key ("foo" for example) I use this:
$content = Get-Content 'C:/fake.txt' | Where-Object {$_ -notmatch 'foo'}
$content | Out-File 'C:/fake.txt'
But now I have file with this scheme:
...
<data name="BLABLA" xml:space="preserve">
<value>some data here</value>
</data>
...
<data name="BLABLA22" xml:space="preserve">
<value>some data</value>
<comment>some comment</comment>
</data>
And I need to remove for key "BLABLA" this three lines
<data name="BLABLA" xml:space="preserve">
<value>some data here</value>
</data>
And for key "BLABLA2" this four lines
<data name="BLABLA22" xml:space="preserve">
<value>some data</value>
<comment>some comment</comment>
</data>
How can I do this by means of powershell?

If you want to delete the complete node, then the following should get you there.
# load the file into xml
[xml]$dom = gc file.xml
# find the node
$nod = $dom.SelectSingleNode("/root/data[#name='BLABLA']")
# remove the node from the parent
$nod.ParentNode.RemoveChild($nod)
# save the xml
$dom.save("file.xml")
I've assumed your data looks a bit like this:
<root>
<data name="BLABLA" xml:space="preserve">
<value>some data here</value>
</data>
<data name="BLABLA22" xml:space="preserve">
<value>some data</value>
<comment>some comment</comment>
</data>
</root>

Related

Caml query filtering doesn't work, showing all items

I am using a caml query to filter items that were modified within the last 7 days. Somehow it returns all my items not only the modified ones.
var viewXml = "<Query><Where><And><Gt>" + "<FieldRef Name=\"Modified\"/><Value Type=\"DateTime\"/><Today OffsetDays=\"-NumDays\"/>" + "</Value></Gt><Lt><FieldRef Name=\"Modified\"/><Value Type=\"DateTime\"/><Today OffsetDays=\"NumDays\"/>" + "</Value></Lt></And></Where></Query>";
Why doesn't the filter work?
Marco
Try this:
<Where>
<Gt>
<FieldRef Name='Modified' />
<Value Type='DateTime'>
<Today Offset='-7' />
</Value>
</Gt>
</Where>

SQL Server 2016 XML Shredding

Have been trying to figure this out for a while without success, read like 10 posts and some other examples and the MS help, not resonating, need to shred some xml data with the following format:
<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
<admin>
<quoteback name="abcd">ABCD A</quoteback>
<product_group>Abcd ABcd Abcd</product_group>
<pnc_account>123456</pnc_account>
<pnc_account_name>ABC</pnc_account_name>
<product_reference>123456789</product_reference>
<report_type>ABCDE</report_type>
<status>ABCDE</status>
<ownership>ABCD</ownership>
<report_code>1234</report_code>
<report_description>Abcde/report_description>
<purpose>ABCDEFGH</purpose>
<date_request_ordered>05/05/2020</date_request_ordered>
<date_request_received>05/05/2020</date_request_received>
<date_request_completed>05/05/2020</date_request_completed>
<time_report_processed>1028</time_report_processed>
<multiple_scores_ordered>false</multiple_scores_ordered>
<vendor name="Abcd" address="Abcd" />
<report>
<sequence>0000000001</sequence>
<count>0000000001</count>
</report>
</admin>
<report>
<alerts_scoring>
<scoring>
<score status="Abcd">
<model_label>ABCD</model_label>
<score>123</score>
<rating_state>AB</rating_state>
<classification> ABCD </classification>
<reason_codes>
<code>12</code>
<description>ABCD</description>
</reason_codes>
<reason_codes>
<code>12</code>
<description>ABCD</description>
</reason_codes>
<reason_codes>
<code>12</code>
<description>ABCD ABCD ABCD</description>
</reason_codes>
<reason_codes>
<code>12</code>
<description>ABCD ABCD ABCD</description>
</reason_codes>
</score>
</scoring>
<general>ABCD ABCD ABCD ORIGINAL REPORT DATE: 12/12/2000</general>
<general>ABCD ABCD ABCD</general>
<general> ABCD ABCD ABCD</general>
<general narrativeCode="Abcd Abcd">ABCD ABCD ABCD</general>
<general narrativeCode=" Abcd Abcd">ABCD ABCD ABCD</general>
<general narrativeCode=" Abcd Abcd">ABCD ABCD ABCD</general>
</alerts_scoring>
<vendor_dataset>
<subjects>
<subject type="Abcd" relationship_to_data="Abcd">
<name type="Abcd">
<first>XXXX</first>
<middle>X</middle>
<last>XXXX</last>
</name>
<birth_date>01/01/1900</birth_date>
<ssn>999999999</ssn>
<address type="Abcd" ref="1" />
<address type="Abcd" ref="2" />
<address type="Abcd" ref="3" />
</subject>
</subjects>
<addresses>
<address id="1">
<street1>ABCD</street1>
<city>ABCD</city>
<state>AB</state>
<postalcode>12345</postalcode>
<zip4>1234</zip4>
<date_first_at_address>01/02/1900</date_first_at_address>
<date_last_at_address>01/02/1900</date_last_at_address>
</address>
<address id="2">
<house>123</house>
<street1>ABCDE</street1>
<city>ABCDE</city>
<state>AB</state>
<postalcode>12345</postalcode>
<zip4>1234</zip4>
<date_first_at_address>00/00/1900</date_first_at_address>
<date_last_at_address>00/00/1900</date_last_at_address>
</address>
<address id="3">
<street1>ABCDE</street1>
<city>ABCDE</city>
<state>AB</state>
<postalcode>12345</postalcode>
<zip4>1234</zip4>
<date_first_at_address>00/00/1900</date_first_at_address>
<date_last_at_address>00/00/1900</date_last_at_address>
</address>
</addresses>
</vendor_dataset>
<summary>
<date_oldest_trade>00/00/1900</date_oldest_trade>
<date_latest_trade>00/00/1900</date_latest_trade>
<date_latest_activity>00/00/1900</date_latest_activity>
<includes_bankruptcies flag="true" date="02/02/2009" />
<includes_other_records public_records="false" collection="true" consumer_statement="false" />
<credit_range high="123456" low="1234" number_trade_lines="12" />
**<account_status_counters>
<account type="current" description="Pays Account as Agreed" status="1">12</account>
<account type="current" description="Status Not Known" status=" ">7</account>
<account type="former" description="Pays/Paid 30-60 Days or Max 2 Payments Past Due" status="2">5</account>
<account type="former" description="Pays/Paid 60-90 Days or Max 3 Payments Past Due" status="3">4</account>
<account type="former" description="Bad Debt" status="9">6</account>
</account_status_counters>**
I currently going down the path of trying to use the xml procedure but I could not get to the finish line with openxml as well. Trying to extract data in highlighted at bottom of xml
EXEC sp_xml_preparedocument #hdoc OUTPUT, #CreditScoreXML
SELECT * FROM OPENXML(#hdoc, '/<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">/admin/summary/account_status_counters')
WITH
(
[Ref_Number] VARCHAR(10) 'product_reference',
[current_account_type] VARCHAR(10) './account/#type',
[current_account_type_description] VARCHAR(50) './account/#description',
[current_account_type_description] VARCHAR(1) './account/#status'
You can define the namespace for your XML using WITH XMLNAMESPACES statement, then you can extract the values you need with .value().
I don't understand exactly the information you are trying to extract, but this should put you on the right track (I only put the first row of your xml to save space, you should put the entire XML fragment in the #xml variable):
declare #xml xml set #xml='
<ncf_report xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cp.com/rules/client">
...
'
;WITH XMLNAMESPACES ('http://cp.com/rules/client' as ns1)
select
#xml.value('(ns1:ncf_report/ns1:admin/ns1:product_reference)[1]', 'varchar(10)') as Ref_Number
,#xml.value('(ns1:ncf_report/ns1:report/ns1:summary/ns1:account_status_counters/ns1:account[#type="current" and #status ="1"]/#description)[1]', 'varchar(50)') as CurrentDescription
,#xml.value('(ns1:ncf_report/ns1:report/ns1:summary/ns1:account_status_counters/ns1:account[#type="current" and #status ="1"])[1]', 'int') as CurrentStatus
,#xml.value('(ns1:ncf_report/ns1:report/ns1:summary/ns1:account_status_counters/ns1:account[#type="current" and #status =" "]/#description)[1]', 'varchar(50)') as CurrentDescription_2
,#xml.value('(ns1:ncf_report/ns1:report/ns1:summary/ns1:account_status_counters/ns1:account[#type="current" and #status =" "])[1]', 'int') as CurrentStatus_2
This sample query would extract:

TSQL Parse XML with namespace

I'm trying to parse some xml that's stored in the database inside of stored procedure. The procedure should return 2 columns, id and value. I'm only part of the way through this, I'm stuck on the fact that I can't list even list the "Setting" nodes.
declare #PolicySettingsXml xml
set #PolicySettingsXml = '<?xml version="1.0" encoding="utf-8"?>
<Policy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.myurl.com/app/Policies">
<Setting id="VendorInfo0">
<string>fsdfdsfds</string>
</Setting>
<Setting id="VendorInfo1">
<string />
</Setting>
<Setting id="VendorInfo2">
<string />
</Setting>
<Setting id="SendSurchargeAsSeparateTransaction">
<boolean>false</boolean>
</Setting>
<Setting id="VendorSurchargeInfo0">
<string />
</Setting>
<Setting id="VendorSurchargeInfo1">
<string />
</Setting>
<Setting id="VendorSurchargeInfo2">
<string />
</Setting>
</Policy>'
select T.c.query('string') as value, T.c.query('#id') as id from #PolicySettingsXml.nodes('/Policy/Setting') T(c)
I'm getting an error, "XQuery [query()]: Attribute may not appear outside of an element," I'd expect to see:
id | value
VendorInfo0 | fsdfdsfds
VendorInfo1 | <null>
VendorInfo2 | <null>
SendSurchargeAsSeparateTransaction | <null>
VendorSurchargeInfo1 | <null>
VendorSurchargeInfo2 | <null>
The first issue is related to syntax.
This:
from PSXML.x.nodes('/*:Policy/*:Setting') T(c);
should be:
from (VALUES(#PolicySettingsXml)) AS PSXML(x)
CROSS APPLY PSXML.x.nodes('/*:Policy/*:Setting') T(c);
For what you are doing you can use the value method for the id column.
select
id = T.c.value('(#id)[1]', 'varchar(100)'),
[value] = T.c.query('(*:string/text())[1]')
from (VALUES(#PolicySettingsXml)) AS PSXML(x)
CROSS APPLY PSXML.x.nodes('/*:Policy/*:Setting') T(c);
Note that I am cheating on the namespaces using the "all namespaces" syntax: * :Object. A google search for "sql server t-sql xml namespaces" will show you the way you're supposed to do it. I cheated due to time constraints.

Iterate over a dataset that was populated with READ-XML without like columns for parent-child relationship?

I'm currently on Openedge 10.1c. I'm using READ-XML to populate a dataset. I can't change the source XML and was was wondering how you would iterate over the dataset when there isn't a parent child relationship unique keys to use in my join.
I'll include sample XML and the display loop when I get to my desk.
In the meantime it's something like this...
<data>
<fulfillments>
<field1>field1</field1>
<field2>field2</field2>
<field3>field3</field3>
<customer>
<name>test</name>
</customer>
</fulfillments>
<fulfillments>
<field1>field11</field1>
<field2>field22</field2>
<field3>field33</field3>
<customer>
<name>test2</name>
</customer>
</fulfillments>
</data>
After I use read XML I get all the data but trying to iterate over it I don't know how to display customer in to each fulfillment. Instead it will just show all customers for each fulfillment.
Sample Code reading and displaying the dataset...
PROCEDURE _read_xml:
DEFINE INPUT-OUTPUT PARAMETER DATASET-HANDLE idshndl.
DEFINE INPUT PARAMETER ifileloc AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER oreturn AS LOGICAL NO-UNDO.
DEFINE VARIABLE cSourceType AS CHARACTER NO-UNDO.
DEFINE VARIABLE cReadMode AS CHARACTER NO-UNDO.
DEFINE VARIABLE lOverrideDefaultMapping AS LOGICAL NO-UNDO.
DEFINE VARIABLE cFile AS CHARACTER NO-UNDO.
DEFINE VARIABLE cEncoding AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSchemaLocation AS CHARACTER NO-UNDO.
DEFINE VARIABLE cFieldTypeMapping AS CHARACTER NO-UNDO.
DEFINE VARIABLE cVerifySchemaMode AS CHARACTER NO-UNDO.
IF SEARCH(ifileloc) <> ? THEN
DO:
ASSIGN
cSourceType = "file"
cFile = ifileloc
cReadMode = "empty"
cSchemaLocation = ?
lOverrideDefaultMapping = ?
cFieldTypeMapping = ?
cVerifySchemaMode = ?.
oreturn = idshndl:READ-XML(cSourceType,
cFile,
cReadMode,
cSchemaLocation,
lOverrideDefaultMapping,
cFieldTypeMapping,
cVerifySchemaMode).
END.
ELSE
DO:
oreturn = FALSE.
END.
END PROCEDURE.
PROCEDURE _fulfillment_display_data:
DISPLAY "DATA".
FOR EACH tt_biz_fulfillments NO-LOCK,
EACH tt_biz_ship_to:
DISPLAY "FULFILLMENTS".
DISPLAY tt_biz_fulfillments WITH SIDE-LABELS.
DISPLAY tt_biz_ship_to WITH SIDE-LABELS.
END.
END PROCEDURE.
Sample XML...
<?xml version="1.0" encoding="UTF-8"?>
<data>
<fulfillments>
<shipping_method>AIM - Direct</shipping_method>
<picker_id />
<sales_order_id>234722</sales_order_id>
<packaging_id>1</packaging_id>
<scheduled_delivery_date>07/01/2016</scheduled_delivery_date>
<net_weight>225</net_weight>
<shipper_name>TEST</shipper_name>
<external_note>Test Note</external_note>
<packaging_type>box</packaging_type>
<bill_to>sender</bill_to>
<shipping_cost />
<fulfillment_id>12345</fulfillment_id>
<ship_to>
<city>Pittsburgh</city>
<name>Bizowie</name>
<zip>15219</zip>
<is_residential>0</is_residential>
<company />
<address>429 Fourth Avenue Suite 1206</address>
<phone_extension />
<contact_id>8</contact_id>
<phone />
<state>PA</state>
<country />
<email />
</ship_to>
<location_id>1</location_id>
<parent_fulfillment_id />
<location_name>201 - PA</location_name>
<manifest_number />
<picker_name />
<scheduled_pick_date />
<pick_timestamp />
<delivery_timestamp />
<packaging_weight>0.150000</packaging_weight>
<insured_value />
<bill_to_zip />
<internal_note>test note internal</internal_note>
<bill_to_account />
<status>shipped</status>
<dock_number />
<route_id>1</route_id>
<delivery_latitude />
<shipper_id>1</shipper_id>
<scheduled_ship_date>06/29/2016</scheduled_ship_date>
<acceptance_timestamp />
<shipping_carrier />
<packaging_height>4.0000</packaging_height>
<packaging_length>4.0000</packaging_length>
<gross_weight>225.15</gross_weight>
<packaging_width>4.0000</packaging_width>
<delivery_longitude />
<delivery_signer_location />
<packing_layer />
<truck_number />
<route_name />
<flag_message />
<delivery_signer_name />
<tracking_number />
<packing_row />
<ship_timestamp>06/09/2016 07:15:15 AM</ship_timestamp>
</fulfillments>
<fulfillments>
<shipping_method>TEST</shipping_method>
<picker_id />
<sales_order_id>234722</sales_order_id>
<packaging_id>1</packaging_id>
<scheduled_delivery_date>08/05/2016</scheduled_delivery_date>
<net_weight>440</net_weight>
<shipper_name>BLAH</shipper_name>
<external_note>TESTING</external_note>
<packaging_type>box</packaging_type>
<bill_to>sender</bill_to>
<shipping_cost />
<fulfillment_id>12346</fulfillment_id>
<ship_to>
<city>Wyoming</city>
<name />
<zip>18644</zip>
<is_residential />
<company>Walmart</company>
<address>10 Moosic St</address>
<phone_extension />
<contact_id>1226058</contact_id>
<phone />
<state>PA</state>
<country />
<email />
</ship_to>
<location_id>1</location_id>
<parent_fulfillment_id />
<location_name>201 - PA</location_name>
<manifest_number />
<picker_name />
<scheduled_pick_date />
<pick_timestamp />
<delivery_timestamp />
<packaging_weight>0.150000</packaging_weight>
<insured_value />
<bill_to_zip />
<internal_note>TESTING NOTE</internal_note>
<bill_to_account />
<status>shipped</status>
<dock_number />
<route_id>1</route_id>
<delivery_latitude />
<shipper_id>1</shipper_id>
<scheduled_ship_date>07/18/2016</scheduled_ship_date>
<acceptance_timestamp />
<shipping_carrier />
<packaging_height>4.0000</packaging_height>
<packaging_length>4.0000</packaging_length>
<gross_weight>440.15</gross_weight>
<packaging_width>4.0000</packaging_width>
<delivery_longitude />
<delivery_signer_location />
<packing_layer />
<truck_number />
<route_name />
<flag_message />
<delivery_signer_name />
<tracking_number />
<packing_row />
<ship_timestamp>06/09/2016 07:15:15 AM</ship_timestamp>
</fulfillments>
</data>
I'm not 100% sure if this works in 10.c or not. It certainly works in 11.6.
If you use a PARENT-ID-RELATION when you define the dataset a record id of the parent buffer will be added to all children when you READ-XML
I've added a simplified version here. You might want to change data types. The PARENT-FIELDS-BEFORE and PARENT-FIELDS-AFTER are just needed to make the output dataset look the same (basically if places the "ship_to" part in the right place). It's not needed for just reading the xml and iterating through the records.
DEFINE TEMP-TABLE ttFulfillments NO-UNDO SERIALIZE-NAME "fulfillments"
FIELD shipping_method AS CHARACTER
FIELD picker_id AS CHARACTER
FIELD sales_order_id AS CHARACTER
FIELD packaging_id AS CHARACTER
FIELD scheduled_delivery_date AS CHARACTER
FIELD net_weight AS CHARACTER
FIELD shipper_name AS CHARACTER
FIELD external_note AS CHARACTER
FIELD packaging_type AS CHARACTER
FIELD bill_to AS CHARACTER
FIELD shipping_cost AS CHARACTER
FIELD fulfillment_id AS CHARACTER
FIELD location_id AS CHARACTER
FIELD parent_fulfillment_id AS CHARACTER
FIELD location_name AS CHARACTER
FIELD manifest_number AS CHARACTER
FIELD picker_name AS CHARACTER
FIELD scheduled_pick_date AS CHARACTER
FIELD pick_timestamp AS CHARACTER
FIELD delivery_timestamp AS CHARACTER
FIELD packaging_weight AS CHARACTER
FIELD insured_value AS CHARACTER
FIELD bill_to_zip AS CHARACTER
FIELD internal_note AS CHARACTER
FIELD bill_to_account AS CHARACTER
FIELD fullfill_status AS CHARACTER SERIALIZE-NAME "status"
FIELD dock_number AS CHARACTER
FIELD route_id AS CHARACTER
FIELD delivery_latitude AS CHARACTER
FIELD shipper_id AS CHARACTER
FIELD scheduled_ship_date AS CHARACTER
FIELD acceptance_timestamp AS CHARACTER
FIELD shipping_carrier AS CHARACTER
FIELD packaging_height AS CHARACTER
FIELD packaging_length AS CHARACTER
FIELD gross_weight AS CHARACTER
FIELD packaging_width AS CHARACTER
FIELD delivery_longitude AS CHARACTER
FIELD delivery_signer_location AS CHARACTER
FIELD packing_layer AS CHARACTER
FIELD truck_number AS CHARACTER
FIELD route_name AS CHARACTER
FIELD flag_message AS CHARACTER
FIELD delivery_signer_name AS CHARACTER
FIELD tracking_number AS CHARACTER
FIELD packing_row AS CHARACTER
FIELD ship_timestamp AS CHARACTER.
DEFINE TEMP-TABLE ttShipTo NO-UNDO SERIALIZE-NAME "ship_to"
FIELD parent_recid AS RECID SERIALIZE-HIDDEN
FIELD city AS CHARACTER
FIELD shipto_name AS CHARACTER SERIALIZE-NAME "name"
FIELD zip AS CHARACTER
FIELD is_residential AS CHARACTER
FIELD company AS CHARACTER
FIELD address AS CHARACTER
FIELD phone_extension AS CHARACTER
FIELD contact_id AS CHARACTER
FIELD phone AS CHARACTER
FIELD state AS CHARACTER
FIELD country AS CHARACTER
FIELD email AS CHARACTER.
DEFINE DATASET dsData SERIALIZE-NAME "data"
FOR ttFulfillments, ttShipTo
PARENT-ID-RELATION pr1 FOR ttFulFillments, ttShipTo
PARENT-ID-FIELD parent_recid
PARENT-FIELDS-BEFORE (shipping_method, picker_id, sales_order_id, packaging_id, scheduled_deliv, net_weight, shipper_name, external_note, packaging_type, bill_to, shipping_cost, fulfillment_id )
PARENT-FIELDS-AFTER (location_id,parent_fulfillment_id,location_name,manifest_number,picker_name,scheduled_pick_date,pick_timestamp, delivery_timestamp, packaging_weight, insured_value, bill_to_zip, internal_note, bill_to_account, fullfill_status, dock_number, route_id, delivery_latitude, shipper_id, scheduled_ship_date, acceptance_timestamp, shipping_carrier, packaging_height, packaging_length, gross_weight, packaging_width, delivery_longitude, delivery_signer_location, packing_layer, truck_number, route_name, flag_message, delivery_signer_name, tracking_number, packing_row, ship_timestamp).
DATASET dsData:READ-XML("file", "c:\temp\sample-data.xml", "empty", ?, ?, ?, ?).
/* Output dataset just to have a reference to compare to the input... */
DATASET dsData:WRITE-XML("file","c:\temp\output-data.xml").
/* Iterate on temp-tables */
FOR EACH ttFulfillments :
DISPLAY ttFulfillments.
FOR EACH ttShipTo WHERE ttShipTo.parent_recid = RECID(ttFulfillments):
DISPLAY ttShipTo EXCEPT ttShipTo.parent_recid.
END.
END.

Select multiple rows from xml database column using xpath (possible without cursor?)

Say I have the following table (where History is an xml column):
Id Value History
1 "Hello" <History>
<Node date="1-1-2011">World</Node>
<Node date="1-2-2011">Foo</Node>
<Node date="1-3-2011">Bar</Node>
</History>
2 "Baz" <History>
<Node date="1-1-2011">Buzz</Node>
<Node date="1-2-2011">Fizz</Node>
<Node date="1-3-2011">Beam</Node>
</History>
And from that I wanted to select a new table like:
HistoryId Id Value Date
1 1 "World" 1-1-2011
2 1 "Foo" 1-2-2011
3 1 "Bar" 1-3-2011
4 2 "Buzz" 1-1-2011
5 2 "Fizz" 1-2-2011
6 2 "Beam" 1-3-2011
How would I do that?
If it were just a standalone xml value I could do something like this:
DECLARE #xml2 XML = '
<History>
<Node date="1-1-2011">World</Node>
<Node date="1-2-2011">Foo</Node>
<Node date="1-3-2011">Bar</Node>
</History>'
SELECT
x.value('(#date)[1]','date') AS [Date]
,x.value('.', 'nvarchar(50)') AS Value
FROM #xml2.nodes('/History/Node') temp(x)
But I'm not sure how to do it when the XML data is part of a table column. I could probably figure out a way to do it imperatively with a cursor but I was wondering if there's a more elegant declarative solution that I'm not aware of.
Use a cross apply
declare #T table (Id int, Value nvarchar(50), History xml)
insert into #T values (1, 'Hello','
<History>
<Node date="1-1-2011">World</Node>
<Node date="1-2-2011">Foo</Node>
<Node date="1-3-2011">Bar</Node>
</History>')
insert into #T values (2, 'Baz','
<History>
<Node date="1-1-2011">Buzz</Node>
<Node date="1-2-2011">Fizz</Node>
<Node date="1-3-2011">Beam</Node>
</History>')
select
Id,
h.n.value('.', 'varchar(10)') as Value,
h.n.value('#date', 'varchar(10)') as Date
from #T
cross apply history.nodes('History/Node') h(n)