IT Process Automation

 View Only
Expand all | Collapse all

applyXPath and convertXml functions

  • 1.  applyXPath and convertXml functions

    Posted Aug 06, 2015 10:30 AM

    I'm wondering if there's a way to use the applyXPath and/or convertXml functions to parse the data below? It's being passed from Catalog into PAM in a start request form.

     

    {"180062" : [{"name" : "area",

    "value" : "Laptop",

    "type" : "5"},{"name" : "desc",

    "value" : "desc",

    "type" : "8"},{"name" : "FormList",

    "value" : "desc*Description",

    "type" : "5"},{"name" : "fulfillmentGrp",

    "value" : "Internal Hardware Support",

    "type" : "5"},{"name" : "hardware_nhw",

    "value" : "Laptop",

    "type" : "12"},{"name" : "OtherList",

    "value" : "SN_lookup*zSerial_Number*model_nhw*zModelType*ipAdd_nhw*zIPAddress",

    "type" : "5"}]}



  • 2.  Re: applyXPath and convertXml functions
    Best Answer

    Broadcom Employee
    Posted Aug 06, 2015 11:04 AM

    I believe the convertXML function will get you what you want.  To see it in action, go the PAM library and open the PAM_PreDefinedContent folder.  Under PAMJavaScriptFunctions there is a folder called "06 XML Conversion Functions".  In this folder is an example where they feed PAM XML and it uses the convertXML function to create a PAM valuemap out of the XML. 

     

    If you don't have the PAM_PreDefinedContent folder, go to the Home screen of PAM and click on the button that says "Browse Out-of-the-box Content" and that will import this content into your library.



  • 3.  Re: applyXPath and convertXml functions

    Posted Aug 11, 2015 11:10 AM

    So the convertXML function changed the format of the information from text(xmlTest) to a value map(xmlResult > Attributes > text), the part I still haven't been able to do is actually parse the data. Is that what I'll need the applyXPath for? Can you give me an example of the code to use based on the code in the original post?

    Capture.PNG



  • 4.  Re: applyXPath and convertXml functions

    Broadcom Employee
    Posted Aug 11, 2015 11:18 AM

    I have found the following site to be helpful for formatting xpath expressions:

    XPath Syntax



  • 5.  Re: applyXPath and convertXml functions

    Posted Aug 11, 2015 10:41 PM

    I may be mistaken but I think XPath and convertXML won’t help you until you get that data formatted with XML tags in place of the ‘syntactic tags’ that the record currently contains.  I would personally attack your data with Regular Expression matching and substitution to get it into XML format, and then you can apply XPath to extract the attributes.  Or skip the XML bit altogether and just parse the attributes straight out of the record.

     

    Regards,

    James



  • 6.  Re: applyXPath and convertXml functions

    Posted Aug 12, 2015 09:26 AM

    James,

     

    I had gone down that path a little and ended up with the result below that looked pretty easily accessible:

     

    Process.data = Process.zFieldList.split('},{');

    Capture.PNG

    I hadn't tried to use a regex expression at that point but in this format it looks like I should be able to access the "name: value:" pairs fairly easily.

     

    I'll try looping over this result and see what I can do, any advice on the regex based on your experience?

     

    Thanks!

    Elwynn.



  • 7.  Re: applyXPath and convertXml functions

    Posted Aug 12, 2015 10:07 PM

    Something along these lines might do it.  A lot of characters need to be escaped, which makes the regular expression look a bit messy.  Just in case I've got the escapes wrong - it often takes a couple of goes to get them right - you might want to start with just the 'name' portion and perhaps test it in a PAM process with just the 'Utilities/Run JavaScript' operator, passing some test examples.  The character lists below are just based on what I can see in your example, so they may need to be extended depending on what else you find.

     

    var i=0;

    var myFields = [];

    var reX1 = new RegExp("\"name\" \: \"([a-zA-Z_\$])\"\, \"value\" \: \"([a-ZA-Z0-9\*\|])\"\, \"type\" \: \"(\d+)\"");


    for (i=0; i < Process.data.length; i++)

    {

      myFields = Process.data[i].match(reX1);

      if (myFields !== null)

      {

         // Element myFields[0] should now hold the 'name', myFields[1] the 'value'

         // and myFields[2] the 'type'. 

         // If myFields.length is not 3 then something has failed to parse.

      }

    }

     

    Hope that helps, anyway.

    Cheers,

    James





  • 8.  Re: applyXPath and convertXml functions

    Posted Aug 20, 2015 03:04 AM

    Aha! Pity I didn't spot that earlier - I'm not that familiar with JSON, but that string seems to be in JSON notation, so you should be able to just do JSON.parse(string) to separate out the elements into an array. If the Javascript that PAM uses has JSON support, anyway.

     

    JSON.parse() - JavaScript | MDN

     

    Cheers,

    James



  • 9.  Re: applyXPath and convertXml functions

    Posted Aug 20, 2015 08:55 AM

    James,

     

    Thanks for your suggestions!

     

    I'm still chipping away at this problem in my spare time, but haven't made much significant progress yet with any of the approaches I've tried.

    I gave the JSON parse a try and got the complaint below back from pam:

     

    Failed to execute code:

    Process.A = JSON.parse(Process.xmlTest);

     

    -- ReferenceError: "JSON" is not defined. (#2)

    Capture.PNG

     

    Maybe I'm handleing the JSON.parse incorrectly? I'll report back here when I figure something out...

     

    Elwynn.



  • 10.  Re: applyXPath and convertXml functions

    Posted Aug 20, 2015 07:25 PM

    Please refer to the CA Process Automation Content Designer Reference, chapter 16: CA Process Automation System Functions, for the list of the built-in functions.

     

    As there is a convertXml function that will convert and XML string to a valuemap, there is a convertJson function that will convert a JSON string (and that is a JSON string in your original post) to a valuemap.



  • 11.  Re: applyXPath and convertXml functions

    Posted Aug 12, 2015 10:58 AM

    Thanks for sharing this.