Remember specific position in XML

Post Reply
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Remember specific position in XML

Post by FlorianDSK »

Hello,

Is there any way in Switch to write out a position in an XML when a specific value is found?

As an example:
XML structure
Jobs
Job
item: Indoor sticker
Format: A6
Job
Article: Sticker Outdoor
Format: A7
Job
Article: Special sticker
Format: free format


And in this case it should search for free format and recognise that it is 3 in the job and then remember this value 3.

Many thanks in advance

Florian
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Remember specific position in XML

Post by freddyp »

You can use an XSL transformation to capture the position of 'free format' in another XML that you can then attach as a dataset. The XSL would look something like this (I cannot be 100% sure because you did not share the real XML code):

Code: Select all

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <freeformat>
            <xsl:for-each select="//Job/Format">
                <xsl:if test="string() = 'free format'">
                    <xsl:element name="position">
                        <xsl:value-of select="position()"/>
                    </xsl:element>
                </xsl:if>
            </xsl:for-each>
        </freeformat>
    </xsl:template>
</xsl:stylesheet>
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Re: Remember specific position in XML

Post by FlorianDSK »

Hello Freddy,

Please find attached the XML.

However, we do not have a script extension for Switch.
Is there another possibility?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<jobs>
	<job>
		<order>
			<item>Indoor Sticker</item>
			<format>210x210</format>
			<freeFormat>round 21</freeFormat>
		</order>
	</job>
	<job>
		<order>
			<item>Outdoor Sticker</item>
			<format>95x95</format>
			<freeFormat>round 9,5</freeFormat>
		</order>
	</job>
	<job>
		<order>
			<item>Special Sticker</item>
			<format>190x290</format>
			<freeFormat>free format</freeFormat>
		</order>
	</job>
	<job>
		<order>
			<item>Indoor Sticker</item>
			<format>105x148</format>
			<freeFormat>a6</freeFormat>
		</order>
	</job>
	<job>
		<order>
			<item>Outdoor Sticker</item>
			<format>105x148</format>
			<freeFormat>a6</freeFormat>
		</order>
	</job>
	<job>
		<order>
			<item>Indoor Sticker</item>
			<format>52x74</format>
			<freeFormat>a8</freeFormat>
		</order>
	</job>
</jobs>
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Remember specific position in XML

Post by freddyp »

You do not need the scripting module, there is a default element "XSLT transform" with which you can apply the XSL. Save the XSL code I posted in a file and use that file on the "XSLT stylesheet" property.

Based on the true XML you will have to change the line:

Code: Select all

<xsl:for-each select="//Job/Format">
to:

Code: Select all

<xsl:for-each select="//freeFormat">
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Re: Remember specific position in XML

Post by FlorianDSK »

This works perfectly. Many thanks for that.

The background is that we need the individual position numbers for a switch loop. These are written out thanks to your XSLT.
We then give these to the asset as Dataset=Position, in addition to the normal XML.
Now Switch should search for the term freeFormat in the normal XML, but only in the XML position, which we then want to pass to it through the other dataset=position.

Is there a way to retrieve metadata in metadata?

[Metadata.Text:Dataset="Xml",Model="XML",Path="/jobs/job[Metadata.Text:Dataset="Position",Model="XML",Path="/freeformat/position[1]"]/order/productDescription",Search="freeFormat"]
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: Remember specific position in XML

Post by freddyp »

There is an app for that: https://www.enfocus.com/en/appstore/pro ... able-xpath

But you are overthinking it. It is easier to simply copy the tag. Instead of

Code: Select all

<xsl:element name="position">
    <xsl:value-of select="position()"/>
</xsl:element>
you use

Code: Select all

<xsl:copy-of select="."/>
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Re: Remember specific position in XML

Post by FlorianDSK »

Hello Freddy,

The requirements have changed again and we had to structure the workflow completely differently.

But your app suggestion was great, I appreciate that and I will keep the app in mind for future worklfows.

kindly

Florian
FlorianDSK
Newbie
Posts: 18
Joined: Fri May 26, 2023 2:35 pm

Re: Remember specific position in XML

Post by FlorianDSK »

Hello frailwhen,

Is there for instance a new way to deposit metadata into existing metadata as described here:

[Metadata.Text:Dataset="Xml",Model="XML",Path="/jobs/job[Metadata.Text:Dataset="Position",Model="XML",Path="/freeformat/position[1]"]/order/productDescription",Search="freeFormat"]

Feel free to share with us so that others may find an easier solution for their project :D
Post Reply