Parse XML value with Name Space in SQl -
i have following xml :
<productionschedule xmlns:inp2="http://www.wbf.org/xml/b2mml-v0401" xmlns="http://www.wbf.org/xml/b2mml-v0401"> <inp2:productionrequest> <inp2:id>0916a</inp2:id> <inp2:description>subh190916a</inp2:description> <inp2:location> <inp2:equipmentid>myequpiment</inp2:equipmentid> </inp2:location> <inp2:segmentrequirement> <inp2:id>000</inp2:id> <inp2:earlieststarttime>2015-10-17t12:00:00</inp2:earlieststarttime> <inp2:latestendtime>2015-10-19t12:00:00</inp2:latestendtime> <inp2:materialproducedrequirement> <inp2:materialdefinitionid>geec3ma0025emzi</inp2:materialdefinitionid> <inp2:quantity> <inp2:quantitystring>2</inp2:quantitystring> </inp2:quantity> <inp2:materialproducedrequirementproperty> <inp2:id>erpwostatus</inp2:id> <inp2:value> <inp2:valuestring>released</inp2:valuestring> </inp2:value> </inp2:materialproducedrequirementproperty> <inp2:materialproducedrequirementproperty> <inp2:id>routing</inp2:id> <inp2:value> <inp2:valuestring>somemprvaue</inp2:valuestring> </inp2:value> </inp2:materialproducedrequirementproperty> <inp2:materialproducedrequirementproperty> <inp2:id>mpvalue2</inp2:id> <inp2:value> <inp2:valuestring>2016-01-21t12:00:00</inp2:valuestring> </inp2:value> </inp2:materialproducedrequirementproperty> </inp2:materialproducedrequirement> </inp2:segmentrequirement> </inp2:productionrequest> </productionschedule>
i trying value mpvalue2 , xml.
i tried following:
select `@xml.value('(/productionschedule/inp2:productionrequest/inp2:segmentrequirement/inp2:materialproducedrequirement/inp2:materialproducedrequirementproperty)[1]','nvarchar(255)')`
your select ok, must consider/declare namespaces:
with xmlnamespaces(default 'http://www.wbf.org/xml/b2mml-v0401' ,'http://www.wbf.org/xml/b2mml-v0401' inp2) select @xml.value('(/productionschedule/inp2:productionrequest/inp2:segmentrequirement/inp2:materialproducedrequirement/inp2:materialproducedrequirementproperty)[1]','nvarchar(255)')
this works (wildcard) it's better specific possible:
select @xml.value('(/*:productionschedule/*:productionrequest/*:segmentrequirement/*:materialproducedrequirement/*:materialproducedrequirementproperty)[1]','nvarchar(255)')
the fast , lazy work :-) not fast in terms of performance...
select @xml.value('(//*:materialproducedrequirementproperty)[1]','nvarchar(255)')
update
this query properties:
with xmlnamespaces(default 'http://www.wbf.org/xml/b2mml-v0401' ,'http://www.wbf.org/xml/b2mml-v0401' inp2) select prop.value('(inp2:id)[1]','nvarchar(100)') property @xml.nodes('/productionschedule/inp2:productionrequest/inp2:segmentrequirement/inp2:materialproducedrequirement/inp2:materialproducedrequirementproperty') a(prop)
the result
property -------- erpwostatus routing mpvalue2
update 2: use id filter in xquery
see how added filter @ end of xpath
in .nodes()
.
nodes return sub-elements row-wise. filter reduce resultset 1 single row (if inp2:id
unique!) , read value/valuestring
.
i let namespace declaration default
, inp2
. but, @serf pointed out correctly, both urls equal. enough declare default
, query without namespace-prefixes...
declare @theid nvarchar(100)='mpvalue2'; xmlnamespaces(default 'http://www.wbf.org/xml/b2mml-v0401' ,'http://www.wbf.org/xml/b2mml-v0401' inp2) select prop.value('(inp2:value/inp2:valuestring)[1]','nvarchar(100)') property @xml.nodes('/productionschedule/inp2:productionrequest/inp2:segmentrequirement/inp2:materialproducedrequirement/inp2:materialproducedrequirementproperty[inp2:id=sql:variable("@theid")]') a(prop)
Comments
Post a Comment