none
How do I count the existing element? RRS feed

  • Question

  • I have a function that is requesting statutes by chapter and gets a response back. If a chapter exist, I am getting the correct output in a response message. However if the chapter does not exist nothing is returned. So I would like in the response message (xml) add what was requested i.e chapter, section and subdivision instead. 

    For example if the request message had the following chapter element and it was not found, I want to add those elements in the response instead of having an empty response.

    Here is the response I am getting if chapter does not exist. So I want to add the elements found in the request message below.

    <?xml version="1.0"?>
    <ns1:Statutes xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0"/>

    The following xml is what is in the request message. If chapter 14511 is not found, then I would like to return the same xml elements. 

    <ns1:Statute xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0">
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">14511</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">86B</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">331</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(a)</Subdivision>
    </ns1:Statute>

    Here is my function

     Function GetStatutesByChapter(ByVal aobjXmlGetStatuteRequestNode As XmlNode, ByVal aobjXMLNameSpaceManager As XmlNamespaceManager, ByVal aobjBroker As ServiceCatalog.Library.v4.Broker) As XmlDocument
                Dim objXmlRequestMessageDoc As XmlDocument
                Dim objXmlResponseMessageDoc As XmlDocument
                Dim intCount As Integer
                Dim objXmlStatuteNode As XmlNode
                Dim objXmlBcaResponseNode As XmlNode
                Dim objXMLConfig As XmlDocument = Msc.Integration.Utility.Library.v4.ProgramExecution.GetConfigFile("Msc.Integration.Bca.Library.Config.xml")
                Dim strUrl As String = objXMLConfig.DocumentElement.SelectSingleNode("SSURL").InnerText
                'Create an instance of the statute service
                Dim objSS As New StatuteServiceV4.StatuteServiceV4
                objSS.Url = strUrl
    
                objSS.Timeout = 1000 * 60 * 3 ' 3 minutes
                'End of new code
    
                aobjBroker.PostMessageWarehouseInformationalMessage("Chapter found.", 1)
    
                'Build the initial response document
                'objXmlResponseMessageDoc.Load("\\j00000swebint\mscapps\deve\appfiles\temp\BcaStatutes.xml")
                objXmlResponseMessageDoc = New XmlDocument
                'Add the first element into the document GetStatutesResponse with its namespace
                objXmlResponseMessageDoc.AppendChild(objXmlResponseMessageDoc.CreateElement("GetStatutesResponse", "http://www.courts.state.mn.us/StatuteService/1.0"))
                'Add a child node to the GetStatutesResponse node
                objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.CreateElement("StatutesXml", "http://www.courts.state.mn.us/StatuteService/1.0"))
                objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.CreateElement("Statutes", "http://www.courts.state.mn.us/StatuteService/1.0"))
                'Convert the node Statutes into an element and set the runType attribute (runType="Request") by adding it's value Request 
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("runType", "Request")
                'Convert the node Statutes into an element and set the attribute (runDateTime="2015-03-05T10:29:40") by adding it 
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("runDateTime", Format(Now, "yyyy-MM-ddTHH:mm:ss"))
    
                intCount = 0
    
                'Create the BCA request message
    
                objXmlRequestMessageDoc = New XmlDocument
                objXmlRequestMessageDoc.AppendChild(objXmlRequestMessageDoc.CreateElement("ns:BasicSearchQueryRequest", aobjXMLNameSpaceManager.LookupNamespace("ns")))
                objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns1:BasicSearchCriteria", aobjXMLNameSpaceManager.LookupNamespace("ns1")))
                objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Chapter", aobjXMLNameSpaceManager.LookupNamespace("st")))
                objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Chapter", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Chapter", aobjXMLNameSpaceManager).InnerText
    
    
                If Not aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager) Is Nothing Then
                    objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Section", aobjXMLNameSpaceManager.LookupNamespace("st")))
                    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Section", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Section", aobjXMLNameSpaceManager).InnerText
                End If
    
                If Not aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager) Is Nothing Then
                    objXmlRequestMessageDoc.SelectSingleNode("ns:BasicSearchQueryRequest/ns1:BasicSearchCriteria", aobjXMLNameSpaceManager).AppendChild(objXmlRequestMessageDoc.CreateElement("ns2:Subdivision", aobjXMLNameSpaceManager.LookupNamespace("st")))
                    objXmlRequestMessageDoc.DocumentElement.SelectSingleNode("ns1:BasicSearchCriteria/st:Subdivision", aobjXMLNameSpaceManager).InnerText = aobjXmlGetStatuteRequestNode.SelectSingleNode("ss:Statute/ss:Subdivision", aobjXMLNameSpaceManager).InnerText
                End If
    
                aobjBroker.PostMessageWarehouseSnapshot(objXmlRequestMessageDoc.OuterXml, "Request Message", 3)
    
                'Make the request here
                objXmlBcaResponseNode = objSS.BasicSearchQuery(objXmlRequestMessageDoc.DocumentElement)
                aobjBroker.PostMessageWarehouseSnapshot(objXmlBcaResponseNode.OuterXml, "Bca response", 3)
    
                'In the BcaResponseNode check the StatuteNode for ns1:Statute there could be one or more
                For Each objXmlStatuteNode In objXmlBcaResponseNode.SelectNodes("ns1:Statute", aobjXMLNameSpaceManager)
                    intCount = intCount + 1
    
                    objXmlResponseMessageDoc.DocumentElement.SelectSingleNode("ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.ImportNode(objXmlStatuteNode, True))
                Next
    
                'Count how many StatuteNodes are found in the BcaResponseNode
                CType(objXmlResponseMessageDoc.SelectSingleNode("ss:GetStatutesResponse/ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager), System.Xml.XmlElement).SetAttribute("totalCount", CStr(intCount))
    
                'Here is the ResponseMessageDoc with Statute, Chapter and oprional (if available) Section and Subdivision
                Return objXmlResponseMessageDoc
    
            End Function

    I think I will have to count to see if there is <ns1:Statute> node. If it is there, then I will not do anything because this line of code  

    objXmlResponseMessageDoc.DocumentElement.SelectSingleNode("ss:StatutesXml/ss:Statutes", aobjXMLNameSpaceManager).AppendChild(objXmlResponseMessageDoc.ImportNode(objXmlStatuteNode, True)) 

     is working by displaying (returning) the statute . If  there is no chapter found which means the element <ns1:Statute> is empty, then I want to add the elements I have stated above (xml)

    Thursday, April 9, 2015 1:59 PM

Answers

  • I'm not sure about the requirement of your question, but if you looking to count elements, lets say you have the below xml:

    <?xml version="1.0"?>
    <ns1:Statute xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0">
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">14511</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">86B</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">331</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(a)</Subdivision>
    </ns1:Statute>
    To count elements within descendant statute:
    doc.Descendants("{http://crimnet.state.mn.us/mnjustice/statute/messages/4.0}Statute").Elements.Count
    The result is 4

    If you want to add elements you can use CreateElement and I'm seeing you already used it.


    Fouad Roumieh

    • Marked as answer by winkimjr2 Friday, April 10, 2015 6:28 PM
    Friday, April 10, 2015 8:16 AM

All replies

  • I'm not sure about the requirement of your question, but if you looking to count elements, lets say you have the below xml:

    <?xml version="1.0"?>
    <ns1:Statute xmlns:ns1="http://crimnet.state.mn.us/mnjustice/statute/messages/4.0">
        <StatuteId xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">14511</StatuteId>
        <Chapter xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">86B</Chapter>
        <Section xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">331</Section>
        <Subdivision xmlns="http://crimnet.state.mn.us/mnjustice/statute/4.0">1(a)</Subdivision>
    </ns1:Statute>
    To count elements within descendant statute:
    doc.Descendants("{http://crimnet.state.mn.us/mnjustice/statute/messages/4.0}Statute").Elements.Count
    The result is 4

    If you want to add elements you can use CreateElement and I'm seeing you already used it.


    Fouad Roumieh

    • Marked as answer by winkimjr2 Friday, April 10, 2015 6:28 PM
    Friday, April 10, 2015 8:16 AM
  • Thanks for the suggestion. It is now working.
    Friday, April 10, 2015 6:29 PM