XSLT Need Help

Post Reply
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

XSLT Need Help

Post by rgpepper »

I have XML files coming in that look like this:

<?xml version='1.0' encoding='UTF-8'?>
<fields>
<field name="customer">
<value>New Customer</value>
</field>
<field name="companyname">
<value>Pagliai's Pizza</value>
</field>

And I want them looking like this:

<field-list>
<field>
<tag>customer</tag>
<value>New Customer</value>
</field>
<field>
<tag>companyname</tag>
<value>Pagliai's PIzza</value>
</field>

This is a case of single-node vs. multiple-node correct? And a XSLT Transform is a "simple" way to do the conversion. Except that it seems everyone using XSLT is using it with PHP or the like and I can't find squat for examples that would work with Switch.

The reason I (think) I want it this way is so I can pull specific values out in Switch as variables. Without the <tag> Switch sees the whole file as one blob.
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: XSLT Need Help

Post by gabrielp »

XLST is hard for me, so I don't really use it. You don't have to transform that to get the values. Here is how to do it with XPath: [Metadata.Text:Path="/fields/field[@name='customer']/value",Dataset="RgpepperXml",Model="XML"]

Here is a flow that writes those values to private data: https://drive.google.com/file/d/0B9ciRz ... sp=sharing
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Thanks!
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Are these 2 different methods? one for XPath and one using PrivateData?
gabrielp wrote:XLST is hard for me, so I don't really use it. You don't have to transform that to get the values. Here is how to do it with XPath: [Metadata.Text:Path="/fields/field[@name='customer']/value",Dataset="RgpepperXml",Model="XML"]

Here is a flow that writes those values to private data: https://drive.google.com/file/d/0B9ciRz ... sp=sharing
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: XSLT Need Help

Post by gabrielp »

They do different things. Using PD is just a convenient way to access your variables and is considered a best practice. You can read more about it here: https://github.com/open-automation/swit ... guidelines

It boils down to this: what is easier to work with and read?

[Metadata.Text:Path="/fields/field[@name='customer']/value",Dataset="RgpepperXml",Model="XML"]

or

[Job.PrivateData:Key="RGP Customer"]

Perhaps in your flow logic, you wanted to overwrite the customer. Maybe a user would choose a customer within a checkpoint. If you're using PD, you can simply overwrite the "RGP Customer" PD tag and re-route the job. If you're stuck with using the XPath everywhere, you need to re-populate the dataset in the exact same way so your XPath still works.

But the most important reason is, perhaps your dataset changes one day. Maybe you make an update and now the attribute is 'customer_name' instead of 'customer'. Well, if you're using PD, all of your PD values are mapped within a single flow element. So you just update that one element and you're good to go. If you're using the XPath of your external dataset, well you need to update that path in every element, connector, email notification, etc... huge pain.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Thanks for that. But I'm still not tracking if you're saying this _by itself_:

[Metadata.Text:Path="/fields/field[@name='customer']/value",Dataset="RgpepperXml",Model="XML"]

Would accomplish what I'm after, at least in theory.
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: XSLT Need Help

Post by gabrielp »

rgpepper wrote:Thanks for that. But I'm still not tracking if you're saying this _by itself_:

[Metadata.Text:Path="/fields/field[@name='customer']/value",Dataset="RgpepperXml",Model="XML"]

Would accomplish what I'm after, at least in theory.
What are you after? You want the value of the node with the attribute of "name"? If so, that string will get you that. But only if you conduct an XML Pickup beforehand and name your dataset "RgpepperXml". Please run the flow I made for you if you're unclear.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Thanks again Gabe for the clarification, never hurts to have yet another tool in my tool belt - sometimes I just need a one-off piece of data and that will get me there. I will continue to look into your preferred method.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Can you suggest some syntax reference document for this stuff, i.e. how did you know to use "@name="?
User avatar
gabrielp
Advanced member
Posts: 645
Joined: Fri Aug 08, 2014 4:31 pm
Location: Boston
Contact:

Re: XSLT Need Help

Post by gabrielp »

rgpepper wrote:Can you suggest some syntax reference document for this stuff, i.e. how did you know to use "@name="?
I access variables from Metadata > Text > Build location path. I usually just use the GUI which allows you to point and click at which values you want to select. Switch does so in two ways, and the way I'm familiar with is called XPath (https://www.w3.org/TR/xpath/). Sometimes, you want to build a location path with an expression that the Switch GUI doesn't support. In these cases, you can write your own as long as it's XPath 1.0 compatible. You can read more on that here: http://www.enfocus.com/manuals/UserGuid ... th_10.html

This is part of the Metadata module, which you can read about here: http://www.enfocus.com/manuals/UserGuid ... adata.html

But honestly, to answer your question I googled something like "xpath select value from attribute" and found a stackoverflow answer which I adopted in the flow I linked above. I really don't know or like using XML or any of the related standards -- not my thing.
Free Switch scripts: open-automation @ GitHub
Free Switch apps: open-automation @ Enfocus appstore

Want to hire me? I'm looking for my next gig. Contact me on LinkedIn or via email.
rgpepper
Member
Posts: 80
Joined: Wed Oct 14, 2015 2:09 am

Re: XSLT Need Help

Post by rgpepper »

Stackoverflow, good stuff. And I agree on XML and its' ilk.
Post Reply