XSLT help

Post Reply
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

XSLT help

Post by LasseThid »

I need to split an incoming xml-file into one xml-file per product in the order.
I have created an XSLT that will pick up all information except for the job-name.
Any ideas what might be wrong with my XSLT?

Incoming xml file

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<order version="1.0" type="order" order-id="26779" status="1" external-order-id="Weborder 103591/105658/105660" external-cost-center="" created-date="11/08/2019 16:15:35" order-date="11/08/2019 16:15:35" user-id="a.b@c.com" customer-id="2" customer-name="Customer Name" external-customer-id="2036" organization-id="081646" vat-id="" sales-person="" project-manager="csr" currency="SEK" portal-id="2" portal-name="Customer Name">
	<information />
	<jobs>
		<job id="5008" type="print" status="1" name="Businessscard 4+4" job-name="Businesscard 1">
			<information />
			<fields>
				<field name="climatecompensation">Climatecompensated</field>
			</fields>
			<producttype>indigo_stationary_businesscard</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Profimatt</media>
				<nup>24</nup>
			</part>
		</job>
		<job id="5009" type="print" status="1" name="Businessscard 4+4" job-name="Businesscard 2">
			<information />
			<fields>
				<field name="climatecompensation">Climatecompensated</field>
			</fields>
			<producttype>indigo_stationary_businesscard</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Profimatt</media>
				<nup>24</nup>
			</part>
		</job>
		<job id="5010" type="print" status="1" name="Businessscard 4+4" job-name="Businesscard 3">
			<information />
			<fields>
				<field name="climatecompensation">Climatecompensated</field>
			</fields>
			<producttype>indigo_stationary_businesscard</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Profimatt</media>
				<nup>24</nup>
			</part>
		</job>
	</jobs>
</order>
XSLT:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <job>
            <jobname>
                <xsl:value-of select="//order/jobs/job/job-name"/>
            </jobname>
            <quantity>
                <xsl:value-of select="//order/jobs/job/quantity"/>
            </quantity>
            <sides>
                <xsl:value-of select="//order/jobs/job/part/sides"/>
            </sides>
            <finaldimension>
                <xsl:value-of select="//order/jobs/job/part/finaldimension"/>
            </finaldimension>
            <printdimension>
                <xsl:value-of select="//order/jobs/job/part/printdimension"/>
            </printdimension>
        </job>
    </xsl:template>
</xsl:stylesheet>
Resulting xml-file:

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<job>
   <jobname/>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
What I'm looking for is for the node jobname to read <jobname>Businesscard 1</jobname>

Thanks in advance!
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
Arthur
Member
Posts: 113
Joined: Sat Sep 09, 2017 11:58 pm
Location: Yateley, UK

Re: XSLT help

Post by Arthur »

Have you had a chance to find out if the path to the nod you are referring to, resolved with a correct value in Switch ??
Navigate in Switch to "//order/jobs/job/job-name" of that XML and see if it is correct. If other paths to certain nodes are getting you the right result and they all are defined in the XSLT in the same fashion, I would suspect sth with how the Path to that particular problematic value is defined (as it is with a dash or so) .....

Sorry if this does not make sense, had a long and difficult night with a baby :lol: ....
jan_suhr
Advanced member
Posts: 586
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: XSLT help

Post by jan_suhr »

Try this:

Code: Select all

 <xsl:value-of select="//order/jobs/job/@job-name"/>
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Thanks Jan.

That did the trick.
Can you explain why I need to use @job-name for this node when everything else works without the @-character?
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Thanks Jan.

That did the trick.
Can you explain why I need to use @job-name for this node when everything else works without the @-character?
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: XSLT help

Post by billy.olvestad »

job-name in your case is an attribute to the node "job", not an element with a text node directly under "job". The other values are elements with text nodes directly under "job".
That's why you need the @-character. It means "attribute" in xslt.
Last edited by billy.olvestad on Mon Nov 11, 2019 2:09 pm, edited 1 time in total.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Arthur wrote: Mon Nov 11, 2019 12:50 pm Have you had a chance to find out if the path to the nod you are referring to, resolved with a correct value in Switch ??
Navigate in Switch to "//order/jobs/job/job-name" of that XML and see if it is correct. If other paths to certain nodes are getting you the right result and they all are defined in the XSLT in the same fashion, I would suspect sth with how the Path to that particular problematic value is defined (as it is with a dash or so) .....

Sorry if this does not make sense, had a long and difficult night with a baby :lol: ....
Hi Arthur.
I haven't tried it in Switch yet as I'm just trying to figure out how to set up the XSLT. XSLT is something I have no knowledge about at all, so I'm learning by trial and error... :lol:
I guess I could have created a test flow and add the xml file as metadata to itself, but I was testing it in XMLSpear when I noticed it didn't work, so I figured I'd better ask the experts...
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

billy.olvestad wrote: Mon Nov 11, 2019 2:08 pm job-name in your case is an attribute to the node "job", not a text node directly under. The other values are text nodes directly under "job".
That's why you need the @-character. It means "attribute" in xslt.
Thanks for the information.
It's kinda hard to understand why some things work while others doesn't when you don't know what you're doing... :lol:
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
User avatar
billy.olvestad
Member
Posts: 51
Joined: Mon Nov 19, 2018 10:08 pm
Location: Sweden
Contact:

Re: XSLT help

Post by billy.olvestad »

LasseThid wrote: Mon Nov 11, 2019 2:11 pm Thanks for the information.
It's kinda hard to understand why some things work while others doesn't when you don't know what you're doing... :lol:
I know. I have pulled my hair a lot when learning XSLT :) .
In this case it was a simple error, but XSLT in general is not the most intuitive thing, so sometimes it takes a lot of trial and error.
But it is seriously powerful when you get to know it.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Yeah.
I have both the XML for Dummies and the XSLT for Dummies here at work. Perhaps I should bring them and do some reading on the train to Stockholm tonight. :roll:
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Ok, so the XSLT works fine for one product, but if the xml file contains more than one product I get the same result repeated for every product.
I added an <xsl:for-each> loop, but it seems to pick up the first product every time.
How can I get one <job> for each product?

Incoming xml:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<order version="1.0" type="order" order-id="26779" status="1" external-order-id="Weborder 103591/105658/105660" external-cost-center="" created-date="11/08/2019 16:15:35" order-date="11/08/2019 16:15:35" user-id="a.b@c.se" customer-id="2" customer-name="kundnamn" external-customer-id="2036" organization-id="081646" vat-id="" sales-person="" project-manager="CSR" currency="SEK" portal-id="2" portal-name="Portalnamn">
	<information />
	<fields>
		<field name="Reference">Kund</field>
		<field name="extend">True</field>
	</fields>
	<customer>
		<fields>
			<field name="API-nyckel">xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</field>
			<field name="Systemkod">haikom</field>
		</fields>
	</customer>
	<buyer>
		<company>Företag</company>
		<name>Namn</name>
		<email />
		<phone />
		<mobilephone />
	</buyer>
	<payment type="Invoice">
		<billingaddress e-invoice-code="" email="">
			<name>Namn</name>
			<address1>Adress</address1>
			<address2 />
			<address3 />
			<city>Stad</city>
			<zipcode>Postnr</zipcode>
			<country>Sweden</country>
			<countrycode>SE</countrycode>
			<state />
		</billingaddress>
	</payment>
	<items>
		<item type="Template" item-number="201" name="Visitkort 4+4 (Motiv 1)" account-code="" external-cost-center="" quantity="250" unit-price="x,xxx" unit="pcs" total-price="xxx,xxx" vat-code="1" vat="25,00" weight="0,337" template-id="159ac08b349a4b8390693fb952e0e694">
			<information />
			<deliveries>
				<delivery delivery-id="2b102f364c214f96b78f712de7417275" quantity="250" />
			</deliveries>
			<jobs>
				<job id="5008" />
			</jobs>
		</item>
		<item type="Template" item-number="201" name="Visitkort 4+4 (Motiv 2)" account-code="" external-cost-center="" quantity="250" unit-price="x,xxxx" unit="pcs" total-price="xxx,xxx" vat-code="1" vat="25,00" weight="0,337" template-id="cd6955be74b044cb82ae61a6f8df5e29">
			<information />
			<deliveries>
				<delivery delivery-id="2b102f364c214f96b78f712de7417275" quantity="250" />
			</deliveries>
			<jobs>
				<job id="5009" />
			</jobs>
		</item>
		<item type="Template" item-number="201" name="Visitkort 4+4 (Motiv 3)" account-code="" external-cost-center="" quantity="250" unit-price="x,xxxx" unit="pcs" total-price="xxx,xxx" vat-code="1" vat="25,00" weight="0,337" template-id="e048f34e3a5a458aae2fe1dc0f805eb6">
			<information />
			<deliveries>
				<delivery delivery-id="2b102f364c214f96b78f712de7417275" quantity="250" />
			</deliveries>
			<jobs>
				<job id="5010" />
			</jobs>
		</item>
		<item type="Freight" item-number="Frakt" name="Frakt" account-code="bring_parcel" external-cost-center="" quantity="1" unit-price="xxx" unit="pcs" total-price="xxx" vat-code="1" vat="25,00" weight="0" template-id="">
			<information />
			<deliveries>
				<delivery delivery-id="2b102f364c214f96b78f712de7417275" quantity="1" />
			</deliveries>
			<jobs />
		</item>
	</items>
	<jobs>
		<job id="5008" type="print" status="1" name="Visitkort 4+4 (Motiv 1)" job-name="Motiv 1">
			<information />
			<fields>
				<field name="climatecompensation">Klimatkompenserad</field>
			</fields>
			<producttype>indigo_kontorstrycksaker_visitkort_korrespondenskort</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Papperskvalitet</media>
				<nup>24</nup>
			</part>
		</job>
		<job id="5009" type="print" status="1" name="Visitkort 4+4 (Motiv 2)" job-name="Motiv 2">
			<information />
			<fields>
				<field name="climatecompensation">Klimatkompenserad</field>
			</fields>
			<producttype>indigo_kontorstrycksaker_visitkort_korrespondenskort</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Papperskvalitet</media>
				<nup>24</nup>
			</part>
		</job>
		<job id="5010" type="print" status="1" name="Visitkort 4+4 (Motiv 3)" job-name="Motiv 3">
			<information />
			<fields>
				<field name="climatecompensation">Klimatkompenserad</field>
			</fields>
			<producttype>indigo_kontorstrycksaker_visitkort_korrespondenskort</producttype>
			<productionunit>a3_print</productionunit>
			<quantity>250</quantity>
			<instructions />
			<part type="inlay">
				<pages>2</pages>
				<sides>TwoSidedHeadToHead</sides>
				<color>CMYK</color>
				<finaldimension>90x50</finaldimension>
				<printdimension>460x320</printdimension>
				<media item-number="Indigo-0241" weight="300" dimension="460x320" grain-direction="ShortEdge">Papperskvalitet</media>
				<nup>24</nup>
			</part>
		</job>
	</jobs>
	<deliveries>
		<delivery id="2b102f364c214f96b78f712de7417275" shipping-method="Frakt">
			<name>Kundnamn</name>
			<address1>Adress</address1>
			<address2 />
			<address3 />
			<city>Stad</city>
			<zipcode>Postnr</zipcode>
			<country>Sweden</country>
			<countrycode>SE</countrycode>
			<state />
			<requesteddeliverydate>11/13/2019 00:00:00</requesteddeliverydate>
			<contactname>Mottagare</contactname>
			<contactemail>a.b@c.se</contactemail>
			<contactphone />
			<contactmobilephone />
			<deliveryinformation />
			<transportinstructions />
			<isexpress>False</isexpress>
			<fields />
			<consignments />
		</delivery>
	</deliveries>
</order>
XSLT

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
       <xsl:for-each select="//order/jobs/job">
         <job>
           <jobid>
                <xsl:value-of select="//order/jobs/job/@id"/>              
              </jobid>
            <jobname>
                <xsl:value-of select="//order/jobs/job/@job-name"/>
            </jobname>
            <quantity>
                <xsl:value-of select="//order/jobs/job/quantity"/>
            </quantity>
            <sides>
                <xsl:value-of select="//order/jobs/job/part/sides"/>
            </sides>
            <finaldimension>
                <xsl:value-of select="//order/jobs/job/part/finaldimension"/>
            </finaldimension>
            <printdimension>
                <xsl:value-of select="//order/jobs/job/part/printdimension"/>
            </printdimension>
        </job>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
Resulting xml

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<job>
   <jobid>5008</jobid>
   <jobname>Motiv 1</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
<job>
   <jobid>5008</jobid>
   <jobname>Motiv 1</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
<job>
   <jobid>5008</jobid>
   <jobname>Motiv 1</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
Desired result

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<job>
   <jobid>5008</jobid>
   <jobname>Motiv 1</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
<job>
   <jobid>5009</jobid>
   <jobname>Motiv 2</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
<job>
   <jobid>5010</jobid>
   <jobname>Motiv 3</jobname>
   <quantity>250</quantity>
   <sides>TwoSidedHeadToHead</sides>
   <finaldimension>90x50</finaldimension>
   <printdimension>460x320</printdimension>
</job>
Even better if I can get one xml per <job>... :D

Thanks in advance
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

A slightly modified XSLT

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="1.0">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
       <xsl:for-each select="//order/jobs/job">
         <job>
           <jobid>
                <xsl:value-of select="@id"/>              
              </jobid>
            <jobname>
                <xsl:value-of select="@job-name"/>
            </jobname>
            <quantity>
                <xsl:value-of select="/quantity"/>
            </quantity>
            <sides>
                <xsl:value-of select="/part/sides"/>
            </sides>
            <finaldimension>
                <xsl:value-of select="/part/finaldimension"/>
            </finaldimension>
            <printdimension>
                <xsl:value-of select="/part/printdimension"/>
            </printdimension>
        </job>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>
gives me

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<job>
   <jobid>5008</jobid>
   <jobname>Motiv 1</jobname>
   <quantity/>
   <sides/>
   <finaldimension/>
   <printdimension/>
</job>
<job>
   <jobid>5009</jobid>
   <jobname>Motiv 2</jobname>
   <quantity/>
   <sides/>
   <finaldimension/>
   <printdimension/>
</job>
<job>
   <jobid>5010</jobid>
   <jobname>Motiv 3</jobname>
   <quantity/>
   <sides/>
   <finaldimension/>
   <printdimension/>
</job>
But why can't I pick up the other values?
If I set to path for <quantity> to //order/jobs/job/quantity all <quantity> nodes get the same result even if I change them to different values.
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
LasseThid
Advanced member
Posts: 353
Joined: Tue Mar 03, 2015 2:30 pm
Location: Molndal, Sweden

Re: XSLT help

Post by LasseThid »

Thanks for your help Jan.

From looking at your XSLT file I think I'm starting to understand what I did wrong in my XSLT file.
Enfocus Switch, Enfocus PitStop Server, Enfocus PDF Review, HP SmartStream& Kodak Prinergy with RBA
Offset 72x102, Offset Large Format, Digital Large Format and Digital print.
Post Reply