Condition - If XML contains ABC?

Post Reply
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Condition - If XML contains ABC?

Post by magnussandstrom »

Hi, I need to sort PDF files dependent if there are other job-lines in the same order that contains a certain type-value (lets say ABC).

The number of job-lines can vary (Example XML below).

My take on solving this is to inject the XML as private data (see screenshot) and then use the condition: if [Job.PrivateData:Key="FileContent"] contains ABC. This way I can look for a specific value anywhere in the XML-order. But it's an awkward solution.

There is probably a much more simple way of doing this. Any suggestions?

Code: Select all

<order>
 <orderid>0001</orderid>
  <jobs>
   <job>
    <file>A.pdf</file>
    <type>123</type>
   </job>
   <job>
    <file>B.pdf</file>
    <type>ABC</type>
   </job>
  </jobs>
</order>
xml_condition.png
xml_condition.png (43.76 KiB) Viewed 866 times
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Condition - If XML contains ABC?

Post by freddyp »

I am not sure it is perse simpler, but it is certainly cleaner if you use an XPath expression. What you do in the left-hand side of a condition is choose Metadata - Integer; in building the location path you choose XPath expression not XML location path which is the default. The expression is (not tested on your data, but you will understand how to interpret it):

Code: Select all

count(/order/jobs/job[type='ABC'])
If it is greater than 0 it means there is a job with type equal to ABC and that takes in one direction, all other jobs in another.
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: Condition - If XML contains ABC?

Post by magnussandstrom »

Freddy, this is exactly the type of answer I was hoping for. Thanks!
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Re: Condition - If XML contains ABC?

Post by JimmyHartington »

Sorry to intrude on this solution.

But could it be used to count how many times a key is found in JSON?

I have an order file which consists of different orderlines. Some is for production. Others are just for invoicing.

So I would like a count of every time "Buchs_Nr" is pressent. So in the case of the the code below the count should be 2.

Code: Select all

{
  "Ordre": {
    "GUID": "A842AA5F-F8F9-4BE3-B9BD-2FE10FB57F58",
    "OrdreNr": "999995",
    "OrdreDato": "2023-12-07",
    "Send_Ordrebekræftelse": true,
    "Tid_HP": 25.0,
    "Tid_Efterbehandling": 35.0,
    "Tid_Opskæring": 30.0
  },
  "Linjer": [
    {
      "GUID": "CD871AF3-12B2-4843-BF75-D5B63E6D9569",
      "Produkt": [
        {
          "Buchs_Nr": "999991",
          "Materiale": "MC Primecoat OPQ BL S2000 N-BG40",
          "Stans": [
            {
              "StanseNr": "STA0077",
              "StanseType": "Firkant afr. 2 mm"
            }
          ],
          "Foil": null,
          "Farve": "Sort + Blank TTR LakSort TTR Lak"
        }
      ],
      "Antal": 10000.0,
      "Enhed": "stk.",
      "Enhedspris": 0.5,
      "PrisEnhed": 1
    },
    {
      "GUID": "C4663089-0E11-4853-87B1-93308A539A10",
      "Produkt": [
        {
          "Buchs_Nr": "888881",
          "Materiale": "MC Primecoat OPQ BL S2000 N-BG40",
          "Stans": [
            {
              "StanseNr": "STA0077",
              "StanseType": "Firkant afr. 2 mm"
            }
          ],
          "Foil": null,
          "Farve": "Sort + Blank TTR LakSort TTR Lak"
        }
      ],
      "Antal": 10000.0,
      "Enhed": "stk.",
      "Enhedspris": 0.5,
      "PrisEnhed": 1
    },
     {
      "GUID": "05C4CB0B-0C0E-4AC9-9E2B-2947288949B3",
      "Produkt": null,
      "Beskrivelse": "Andel af stans"
    },
    {
      "GUID": "195708AF-C90E-4BC0-8C0E-BE99A2787610",
      "Produkt": null,
      "Beskrivelse": "Tilretning af filer"
    }
  ]
}
I have a solution using the command line tool "jq" running with the "Run Command" program.

Code: Select all

"C:\ProgramData\chocolatey\lib\jq\tools\jq.exe" "[.. | .Buchs_Nr? | select(.!=null)] | length"  "%%InputFilePath%%"
But as Magnus I would like a more native solution.
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Condition - If XML contains ABC?

Post by freddyp »

No need to be sorry, Jimmy, that is actually a very good question.

The extensive support for functions in XPath (there is a lot more than just count) is why I often prefer working with XML datasets instead of with JSON datasets (in scripting it is the other way around). With the "JSON pickup" app you can convert a JSON to XML and then you can use all the features of XPath.

For the longer term there is a plan to support a technology in Switch that allows to query JSON datasets in a way that is similar to that of XPath for XML. One such example is JSONata (https://jsonata.org, but there are others. I do not know in which version or how it will be implemented, but you have a workaround for now by using JSON pickup.
User avatar
JimmyHartington
Advanced member
Posts: 310
Joined: Tue Mar 22, 2011 7:38 am

Re: Condition - If XML contains ABC?

Post by JimmyHartington »

Thanks, Freddy.

What I have done is using the JSON Pickup with these settings.
Image

And on a folder I set private data:

Code: Select all

ProductCount=[Metadata.Integer:Dataset="OrderDataXML",Model="XML",Path="count(/JSON/Linjer/Produkt/Buchs_Nr)"]
Post Reply