Adding new node into xml data in SQL server - sql-server-2008

declare #xml xml = '<?xml version="1.0"?> <dashboardsettings> <widgets>
<widget userwidgetuniqueid="9" widgetid="9" daterange="1" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="10" widgetid="10" daterange="0" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="11" widgetid="11" daterange="0" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="12" widgetid="12" daterange="3" />
<widget userwidgetuniqueid="13" widgetid="13" daterange="3" /> </widgets> </dashboardsettings>'
We wanted to add extra node to this xml (at last or first does not matter)
output to be as
declare #xml xml =
'
<?xml version="1.0"?>
<dashboardsettings>
<widgets>
<widget userwidgetuniqueid="9" widgetid="9" daterange="1" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="10" widgetid="10" daterange="0" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="11" widgetid="11" daterange="0" startdate="2016-02-06" enddate="2017-02-06" />
<widget userwidgetuniqueid="12" widgetid="12" daterange="3" />
<widget userwidgetuniqueid="13" widgetid="13" daterange="3" />
<widget userwidgetuniqueid="14" widgetid="14" daterange="3" />
</widgets>
</dashboardsettings>
'
--then
i wrote code to find max value of userwidgetuniqueid and adding +1 and prepared new node also but i could not add this new node to xml value , the error
says : Incorrect use of the XML data type method 'modify'. A non-mutator method is expected in this context.
--below is my code
declare #TopUserwidgetuniqueid int
SELECT top 1 #TopUserwidgetuniqueid = x.y.value('.','int')
FROM #XML.nodes('/dashboardsettings/widgets/widget/#userwidgetuniqueid') AS x ( y )
order by x.y.value('.','int') desc
select #TopUserwidgetuniqueid=#TopUserwidgetuniqueid+1
declare #xNewNode xml = '<widget widgetid="'+cast(#TopUserwidgetuniqueid as varchar(10)) +'" daterange="2"></widget>'
SET #xml = #xml.modify('insert sql:variable("#xNewNode") as last into /dashboardsettings/widgets[1]')
select #xml
The last xml.modify to insert new node is not working , please help ,
Many thanks.

Related

remove duplicate entry in xml in mysql

I have duplicate entry in my xml column in my sql table see material8 key.I remove one entry.I am trying below query its removing both.Is there any way i can remove only one entry.
Update mytable set xml = replace (xml, "<Book key=\"material8\" active=\"true\" displayOrder=\"5\" />", "") where id = 9 and type_key="mykey1";
<?xml version="1.0" encoding="UTF-8"?>
<Type key="test1" publicKey="test2" >
<UIProperties>
<label locale="en_US">My book</label>
</UIProperties>
<Books>
<Book key="material1" active="true" displayOrder="0" >
<UIProperties>
<label locale="en_US">My Books</label>
</UIProperties>
</Book>
<Book key="material2" />
<Book key="material3" active="true" displayOrder="3" >
<Pages>
<Page key="material4" active="true" displayOrder="0" />
<Page key="material5" active="true" displayOrder="1" />
</Pages>
</Book>
<Book key="material6" active="true" displayOrder="4" />
<Book key="material7" active="true" displayOrder="2" />
<Book key="material8" active="true" displayOrder="5" />
<Book key="material8" active="true" displayOrder="5" />
</Books>
<Attributes>
<Attribute key="desc" />
<Attribute key="date1" />
<Attribute key="date2" />
</Attributes>
</Type>
A simple solution is to replace 2 books to 1, instead of 1 to none:
UPDATE mytable
SET xml = REPLACE(xml, "<Book key=\"material8\" active=\"true\" displayOrder=\"5\" /><Book key=\"material8\" active=\"true\" displayOrder=\"5\" />", "<Book key=\"material8\" active=\"true\" displayOrder=\"5\" />")
WHERE id = 9
AND type_key="mykey1";

Print XML attribute in SQL Server

I am trying to get value of an attribute from XML in SQL table
<?xml version="1.0" encoding="utf-8"?>
<container>
<Property Name="paramA" Vocabulary="someVocu">
<Property Name="paramB" Value="valueA" />
<Property Name="paramC" Value="valueB" />
</Property>
<Property Name="paramA" Vocabulary="anotherVocu">
<Property Name="paramB" Value="valueY" />
<Property Name="paramC" Value="valueZ" />
</Property>
</container>
select x.XmlCol.value('(Property[#Name="paramB"]/#Value)[1]', 'varchar(50)') from tempTbl CROSS APPLY rawxml.nodes('/container') AS x(XmlCol)
I am trying to print "valueA" and "valueY" I am getting a NULL.
How can I do this?
Thanks
I cannot tell you what in particular is wrong with your statement since I am still learning how to query XML too. I was able to come up with some SQL which I think should work for by referencing this question...
How to query for Xml values and attributes from table in SQL Server?
Here it is.
CREATE TABLE tempTbl
(
id INT,
data XML
)
INSERT INTO dbo.tempTbl
(id, data)
SELECT 1, '<?xml version="1.0" encoding="utf-8"?>
<container>
<Property Name="paramA" Vocabulary="someVocu">
<Property Name="paramB" Value="valueA" />
<Property Name="paramC" Value="valueB" />
</Property>
<Property Name="paramA" Vocabulary="anotherVocu">
<Property Name="paramB" Value="valueY" />
<Property Name="paramC" Value="valueZ" />
</Property>
</container>'
SELECT
x.XmlCol.value('#Value', 'varchar(25)') AS Value
FROM tempTbl AS t
CROSS APPLY t.data.nodes('/container/Property/Property') AS x(XmlCol)
WHERE x.XmlCol.value('#Name', 'varchar(25)') = 'paramB'
To learn more about querying XML I am working through Stairway to XML.
Noel

How to do deep traversals with FindItem queries

I am sending the following XML query to EWS using the Office.context.mailbox.makeEwsRequestAsync method. It works as long as Traversal is set to "Shallow". I get a "The request is invalid" error when I set Traversal to "Deep".
How do I recursively search subfolders if I can't do deep searches?
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<t:RequestServerVersion Version="Exchange2013_SP1" />
</soap:Header>
<soap:Body>
<m:FindItem Traversal="Shallow">
<m:ItemShape>
<t:BaseShape>AllProperties</t:BaseShape>
</m:ItemShape>
<m:IndexedPageItemView MaxEntriesReturned="10" Offset="0" BasePoint="Beginning" />
<m:Restriction>
<t:Contains ContainmentMode="FullString" ContainmentComparison="IgnoreCase">
<t:FieldURI FieldURI="item:Categories" />
<t:Constant Value="MyCategory" />
</t:Contains>
</m:Restriction>
<m:ParentFolderIds>
<t:DistinguishedFolderId Id="inbox" />
<t:DistinguishedFolderId Id="sentitems" />
</m:ParentFolderIds>
</m:FindItem>
</soap:Body>
</soap:Envelope>
The FindItem EWS call does not support Traversal="Deep". See FindItem.
This is because the call will search for items in the folders specified in ParentFolderIds. In order to do a recursive search, you will need to get the list of folders to include in the search by using the FindFolder EWS Operation which supports Traversal="Deep" on non public folders. See FindFolder for additional documentation.

How to find whether an appointment exists by ApointmentId

I used Appointment.Bind() & pass in ApointmentId. If it exists it return all the details about the appointment, if not it doesn't throw any exception but gives some appointment object.
I want to how to figure out if appointment exists or not?
If the ID represents an item that's not there, you should get a ServiceResponseException with the message The specified object was not found in the store. The SOAP response looks like this:
<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ServerVersionInfo MajorVersion="15" MinorVersion="1" MajorBuildNumber="207" MinorBuildNumber="19" Version="V2_47" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<m:GetItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
<m:ResponseMessages>
<m:GetItemResponseMessage ResponseClass="Error">
<m:MessageText>The specified object was not found in the store.</m:MessageText>
<m:ResponseCode>ErrorItemNotFound</m:ResponseCode>
<m:DescriptiveLinkKey>0</m:DescriptiveLinkKey>
<m:Items />
</m:GetItemResponseMessage>
</m:ResponseMessages>
</m:GetItemResponse>
</s:Body>
</s:Envelope>

JSON: How to log a JSON object value from a vxml?

My aim is to log a JSON object value fethed from a jsp file from the vxml. Is there any way to do that. I see that there is a function called JSON.stringify but thats giving me nothing as a log.
Below is my code:
<?xml version="1.0"?>
<vxml version="2.0" xmlns="http://www.w3.org/2001/vxml">
<var name="userId" expr="1" />
<!--form id="get_location"-->
<data name="userData" method="get" src="http://localhost:5000/userLocation.jsp" nameList="userId" />
<property name="inputmodes" value="dtmf"/>
<menu id="menuZero">
<choice dtmf="1" next="#choice1"/>
<choice dtmf="2" next="#choice2"/>
</menu>
<!--/form-->
<form id="choice1">
<block>
<if cond="userData.HttpResponse.do_queryResponse[&apos;return&apos;].errorMsg.result_code != &apos;0&apos;">
<goto next="welcome.vxml"/>
<else/>
<goto next="welcome.vxml"/>
</if>
</block>
</form>
<form id="choice2">
<block>
<log expr="JSON.stringify(userData.HttpResponse)"/>
</block>
</form>
</vxml>
Maybe, VoiceXML is not supported "JSON.stringify".
Try to get "json2.js" and add code.
<script src="json2.js" />
For example,
<?xml version="1.0" encoding="UTF-8"?>
<vxml
version="2.0"
xmlns="http://www.w3.org/2001/vxml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<script src="json2.js" />
<var name="messageObject" expr="{keyA:'valueA',keyB:'valueB',keyC:'valueC'}" />
<form>
<block><prompt>Write Log!</prompt></block>
<block><log expr="JSON.stringify(messageObject)"/></block>
</form>
</vxml>
I tested this code on "Voxeo Prophecy 13".
I tried json2.js as described above and I had the same issue, 'assignment to undeclared variable JSON'. To fix this, I just declared in the same file (json2.js):
var JSON;
Then it worked properly. In the vxml:
<script><![CDATA[
prueba = new Object();
prueba.pepito = 1234;
prueba.OtraPrueba = "lalalapepe";
]]></script>
<log label="IVB_HISTORY">
<value expr="JSON.stringify(prueba)"/>
</log>
It gets logged as followed:
{"pepito":1234,"OtraPrueba":"lalalapepe"}
I'm using Convergy's InVision Studio