13 Replies Latest reply on Dec 9, 2014 9:56 PM by Rajeev Gupta

    How to fetch data from XML

    Rajeev Gupta

      I have a workflow which fetches the output from the BladeLogic Network Automation (BNA) in XML format. For one CI, we are able to fetch the details. But in case of multiple CI's, we are not able to parse the output.

      XML output from BNA looks like:

       

      <transcript>

      abc data

      </transcript>

      xyz data

      <transcript>

      def data

      </transcript>

       

      We need to fetch out the data only between the <transcript> </transcript> and discard the others.

       

      When running it seperately, it is able to fetch, but during the execution of the workflow, its not able to.

        • 1. Re: How to fetch data from XML

          Hi,

           

          If i understand correctly your case,

          Try x path expression like "//transcript" which will fetch only particular nodes like below

          <result>

            <transcript>abc data</transcript>

            <transcript>def data</transcript>

          </result>

           

          If you want to fetch with data then //transcript/text() then result will be like,

          <result>abc datadef data</result>

           

          You can use Basic transform editor also to achieve same.

           

          Thanks,

          Surya.

          1 of 1 people found this helpful
          • 2. Re: How to fetch data from XML
            Aryan Anantwar

            any workflow should not return such XML, which you mentioned is coming from BNA as result.

            will you please share the response of the workflow after fetching the CI's from BNA?

             

            if you share what you want to do with transcript values, will provide you the exact XPATH expressions.

             

            Regards,

            Aryan Anantwar

            • 3. Re: How to fetch data from XML
              Gordon McKeown

              The issue you have here is that your output, when taken as a whole, is not well-formed XML. As such, AO can't just treat it as an XML document.

               

              You should aim to split out the individual <transcript> tags as Surya suggests, but how you do this will depend on the exact format of the response XML you are working with.

               

              My initial approach would be an XSLT with a for loop that iterates over each line of your input. If the line contains "<transcript>" (or "&lt;transcript&gt;") then create an element and place the next line in it. This assumes that your transcript elements are always a single line. If they are multi-line you'll need to do something a little more creative.

               

              For example, if your input data looks like this:

               

              <lines>
                <line>&lt;transcript&gt;</line>
                <line>abc data</line>
                <line>&lt;/transcript&gt;</line>
                <line>xyz data</line>
                <line>&lt;transcript&gt;</line>
                <line>def data</line>
                <line>&lt;/transcript&gt;</line>
              </lines>
              
              

               

              The following XSLT:

               

              <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                <xsl:output indent="no" />
                <xsl:template match="/">
                  <result>
                    <xsl:for-each select="//line">
                      <xsl:if test="contains(., &quot;&lt;transcript&gt;&quot;)">
                        <transcript>
                          <xsl:value-of select="./following-sibling::line" disable-output-escaping="no" />
                        </transcript>
                      </xsl:if>
                    </xsl:for-each>
                  </result>
                </xsl:template>
              </xsl:stylesheet>
              
              

               

              Will get you output that looks like this:

               

              <result>
                <transcript>abc data</transcript>
                <transcript>def data</transcript>
              </result>
              
              
              1 of 1 people found this helpful
              • 4. Re: How to fetch data from XML
                Rajeev Gupta

                I have added the transcript to the post itself. its too much of data and we only require stuff between <transcript></transcript>

                • 5. Re: How to fetch data from XML
                  Bruno Drugowick Muniz

                  So, I believe Suryanarayana Reddy is correct. Have you tried that?

                  • 6. Re: How to fetch data from XML
                    Rajeev Gupta

                    I am still trying the solutions provided.

                    • 7. Re: How to fetch data from XML
                      Aryan Anantwar

                      we will help you, if you share the response XML & result you are expecting.

                      • 8. Re: How to fetch data from XML
                        Rajeev Gupta

                        example:

                        the XML is like --

                         

                        <element>

                             <data>

                                  <transcript>

                                            actual data 1

                                  </transcript>

                             </data>

                        </element>

                        <element>

                             <data>

                                  <transcript>

                                            actual data 2

                                  </transcript>

                             </data>

                        </element>

                        <element>

                             <data>

                                  <transcript>

                                            actual data 3

                                  </transcript>

                             </data>

                        </element>

                        <element>

                             <data>

                                  <transcript>

                                            actual data 4

                                  </transcript>

                             </data>

                        </element>

                         

                         

                        The output expected is

                        actual data1

                        actual data2

                        actual data3

                        actual data4

                        • 9. Re: Re: How to fetch data from XML
                          Gordon McKeown

                          Again this is not a well-formed XML document. Assuming you have a parent tag to act as the document root, you can use //transcript as mentioned by Surya, but in order to add new-lines between items you will need to use XSLT. Create an XLST transform, add a root element (e.g. <result>) and place a for-loop under this. The selection criteria should be //transcript. Under the for-loop element, add a value-of element with selection criteria of normalize-space(.) and a text element in which you enter a blank line (i.e. just press enter once inside the text box).

                           

                          Using:

                           

                          <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
                            <xsl:output indent="no" />
                            <xsl:template match="/">
                              <result>
                                <xsl:for-each select="//transcript">
                                  <xsl:value-of select="normalize-space(.)" disable-output-escaping="no" />
                                  <xsl:text disable-output-escaping="no">
                          </xsl:text>
                                </xsl:for-each>
                              </result>
                            </xsl:template>
                          </xsl:stylesheet>
                          

                           

                          Gives:

                           

                          <result>actual data 1
                          actual data 2
                          actual data 3
                          actual data 4
                          </result>
                          

                           

                          When using this result, AO will ignore the result tags and treat it like a string as you require.

                          • 10. Re: Re: How to fetch data from XML
                            Bruno Drugowick Muniz

                            Rajeev,

                             

                            now I must agree with Gordon. Expanding on this, to help you with XML you can read something about it at W3Schools (which I personally don't like, but it'll be enough for now) and you can also use Notepad++ with some XML plugins.

                             

                            A XML document must have only one root element. A valid XML would be (with XML being the root element):

                             

                            <XML>
                                <element>
                                    <data>
                                        <transcript>
                                                actual data 1
                                        </transcript>
                                    </data>
                                </element>
                                <element>
                                    <data>
                                        <transcript>
                                                actual data 2
                                        </transcript>
                                    </data>
                                </element>
                                <element>
                                    <data>
                                        <transcript>
                                                actual data 3
                                        </transcript>
                                    </data>
                                </element>
                                <element>
                                    <data>
                                        <transcript>
                                                actual data 4
                                        </transcript>
                                    </data>
                                </element>
                            </XML>
                            

                             

                            Regards.

                            • 11. Re: Re: How to fetch data from XML
                              Rajeev Gupta

                              Yes, but this is the transcript which we are getting from BladeLogic Network Automation as output which we need to parse and send to remedy.

                              In case there are multiple CI's, the output is in such form only.

                              • 12. Re: Re: How to fetch data from XML
                                Aryan Anantwar

                                Did you get what you are expecting?

                                • 13. Re: Re: How to fetch data from XML
                                  Rajeev Gupta

                                  I am working on it to fetch the details as required. The XSLT/XPATH which we wrote earlier was able to fetch the details when working stand along, but when inserted in the workflow, it broke and was only giving output for first transcript data. It completely ignored the other data inside the txt.