Page 1 of 1

Element reordering help....

Posted: Wed Mar 18, 2009 10:56 pm
by undrmoons
I am having an issue with trying to reorder 3 elements for my output. One of these elements has 2 child nodes which have attributes I need to maintain (Product_Image & Logo_image_1), which is why I chose to use "copy-of".

Here is a sample of the XSL I am currently using:

<xsl:template match="Story">
<Story>
<xsl:apply-templates/>
</Story>
</xsl:template>

<xsl:template match="Product_Name">
<Item_Name>
<xsl:copy-of select="Product_Image"/>
<xsl:apply-templates/>
<xsl:copy-of select="Logo_Image_1"/>
</Item_Name>
</xsl:template>

<xsl:template match="Headline">
<Price_32pt>
<xsl:apply-templates/>
</Price_32pt>
</xsl:template>
<xsl:template match="Why_Buy">
<Save_15pt>
<xsl:apply-templates/>
</Save_15pt>
</xsl:template>......

As of right now the output I get is:

<Story><Item_Name><Product_Image href="file://265313.eps"/>TextTextText</Item_Name>
<Price_32pt>Save $1</Price_32pt>
<Save_15pt>Was $9</Save_15pt>...

However, my desired output is:

<Story>
<Price_32pt>Save $1</Price_32pt>
<Save_15pt>Was $9 </Save_15pt>
<Item_Name><Product_Image href="file://265313.eps"/>TextTextText</Item_Name>...

I've seen several ways to do similar things, but only in pieces.
Thanks in advance...
undrmoons

Re: Element reordering help....

Posted: Thu Mar 19, 2009 1:26 pm
by JohnBampton
<xsl:template match="Story">
<Story>
<xsl:apply-templates select="Headline"/>
<xsl:apply-templates select="Why_Buy"/>
etc...
</Story>
</xsl:template>

Cheers, John Bampton.

Re: Element reordering help....

Posted: Thu Mar 19, 2009 5:14 pm
by undrmoons
Hi John,

I think if I use a template match for "story", I cannot use another template match for "ProductName", keeping all of it's child nodes and attributes, correct?

I probably failed to really stress that the important element I am looking to reorder is the "Product_Name" — <Item_Name> element. It has to move from position 1 within <Story> to position 3 (after Why_Buy).

Thanks!
undr

Re: Element reordering help....

Posted: Fri Mar 20, 2009 11:58 am
by JohnBampton
I have given you the answer

post your input xml, your example output xml and your xsl and I will help you.

Re: Element reordering help....

Posted: Mon Mar 23, 2009 11:35 pm
by undrmoons
Hi John,

I've pasted details below:

Initial XML File: 

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Story><!-- data[username:"menl",blockid:17469,pageid:4240,docid:799,docstatus:"Ready to be Built",chart_row_count:4,chart_col_count:5] --><Product_Name>Artisan 800 <Product_Image href="file:///Volumes/Product_Images/S026xxxx/S0265xxx/S02653xx/S0265313.eps" LetterKey="" ComfortKey="" Caption="" /></Product_Name>
<Headline>Save $50
</Headline>
<Why_Buy>4999 was $9999
</Why_Buy>
<Subhead_First_Bullet>4-in-1: print, copy, scan, fax
</Subhead_First_Bullet><Body_Copy>Prints up to 38 blk/38 clr ppm
</Body_Copy><Body_Copy>Network and wireless ready
</Body_Copy><Body_Copy>Wireless printing
</Body_Copy><Body_Copy>Reg. price $299.98
</Body_Copy><Table ><Cell ><Chart_Header>Reg.</Chart_Header></Cell><Cell ><Chart_Header>Reg.</Chart_Header></Cell><Cell ><Chart_Header>Price</Chart_Header></Cell><Cell ><Chart_Header_Red>Item #</Chart_Header_Red></Cell><Cell ><Chart_Header>Description</Chart_Header></Cell><Cell ><Chart_Subhead>USB flash drive</Chart_Subhead></Cell><Cell ></Cell><Cell ></Cell><Cell ></Cell><Cell ></Cell><Cell ><Reg_Price>$00.00</Reg_Price></Cell><Cell ><Save_Price>$00.00</Save_Price></Cell><Cell ><List_Price>$00.00</List_Price></Cell><Cell ><ProdID>XXX207</ProdID></Cell><Cell ><Chart_Copy></Chart_Copy></Cell><Cell ><Chart_Subhead>USB flash drive</Chart_Subhead></Cell><Cell ></Cell><Cell ></Cell><Cell ></Cell><Cell ></Cell><Cell ><Reg_Price>$00.00</Reg_Price></Cell><Cell ><Save_Price>$00.00</Save_Price></Cell><Cell ><List_Price>$00.00</List_Price></Cell><Cell ><ProdID>XXX207</ProdID></Cell><Cell ><Chart_Copy>While supplies last</Chart_Copy></Cell></Table></Story>


Current XSL:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [<!ENTITY nbsp "&#x09;">]>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

<xsl:template match="Story|comment()">
<xsl:copy>
<xsl:apply-templates select="Story" />
<xsl:apply-templates select="node( )" />
</xsl:copy>
</xsl:template>

<xsl:template match="Product_Name">
<Item_Name>
<xsl:copy-of select="Product_Image"/>
<xsl:apply-templates/>
<xsl:copy-of select="Logo_Image_1"/>
<xsl:text>
</xsl:text>
</Item_Name>
</xsl:template>

<xsl:template match="Headline">
<Price_32pt>
<xsl:apply-templates/>
</Price_32pt>
</xsl:template>
<xsl:template match="Why_Buy">
<Save_15pt>
<xsl:apply-templates/>
</Save_15pt>
</xsl:template>

<xsl:template match="Subhead_First_Bullet">
<Item_Name>
<xsl:apply-templates/>
</Item_Name>
</xsl:template>
<xsl:template match="Body_Copy">
<Item_Copy>
<xsl:apply-templates/>
</Item_Copy>
</xsl:template>

<xsl:template match="ProdID">
<xsl:choose>
<xsl:when test="(following::Chart_Copy[2])">
<SKU>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</SKU>
</xsl:when>
<xsl:otherwise>
<SKU>
<xsl:text> </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</SKU>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="Chart_Header_Red"/>
<xsl:template match="Chart_Header"/>

<xsl:template match="Reg_Price">
<Item_Copy_Price>
<xsl:text>Reg. price </xsl:text>
<xsl:value-of select="."/>
</Item_Copy_Price>
</xsl:template>
<xsl:template match="Save_Price">
<Item_Copy_Price_Red>
<xsl:text> Save </xsl:text>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</Item_Copy_Price_Red>
</xsl:template>
<xsl:template match="List_Price">
<Item_Copy_Price>
<xsl:value-of select="."/>
<xsl:text> </xsl:text>
</Item_Copy_Price>
</xsl:template>

<xsl:template match="Chart_Copy">
<SKU>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</SKU>
<xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="Chart_Subhead">
<Item_Name>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</Item_Name>
</xsl:template>

</xsl:stylesheet>

Desired Result:

<?xml version="1.0" encoding="UTF-8"?><Story><!-- data[username:"menl",blockid:17469,pageid:4240,docid:799,docstatus:"Ready to be Built",chart_row_count:4,chart_col_count:5] -->
<Price_32pt>Save $50
</Price_32pt>
<Save_15pt>4999 was $9999
</Save_15pt>
<Item_Name><Product_Image href="file:///Volumes/Product_Images/S026xxxx/S0265xxx/S02653xx/S0265313.eps" LetterKey="" ComfortKey="" Caption=""/>Artisan 800
</Item_Name>
<Item_Name>4-in-1: print, copy, scan, fax
</Item_Name><Item_Copy>Prints up to 38 blk/38 clr ppm
</Item_Copy><Item_Copy>Network and wireless ready
</Item_Copy><Item_Copy>Wireless printing
</Item_Copy><Item_Copy>Reg. price $299.98
</Item_Copy><Item_Name>USB flash drive
</Item_Name><Item_Copy_Price>Reg. price $00.00</Item_Copy_Price><Item_Copy_Price_Red> Save $00.00 </Item_Copy_Price_Red><Item_Copy_Price>$00.00 </Item_Copy_Price><SKU> XXX207
</SKU><SKU>
</SKU>
<Item_Name>USB flash drive
</Item_Name><Item_Copy_Price>Reg. price $00.00</Item_Copy_Price><Item_Copy_Price_Red> Save $00.00 </Item_Copy_Price_Red><Item_Copy_Price>$00.00 </Item_Copy_Price><SKU> XXX207 </SKU><SKU>While supplies last
</SKU>
</Story>

Thanks in advance!
undr

Re: Element reordering help....

Posted: Tue Mar 24, 2009 3:41 am
by JohnBampton
This gives ordered output, an extension of the first example that I gave you. You just need to add in the other templates/apply templates etc

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="2.0">
<xsl:output method="xml" indent="yes"></xsl:output>
<xsl:template match="Story">
<xsl:copy>
<xsl:apply-templates select="comment()"></xsl:apply-templates>
<xsl:apply-templates select="Headline"></xsl:apply-templates>
<xsl:apply-templates select="Why_Buy"></xsl:apply-templates>
<xsl:apply-templates select="Product_Name"></xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="comment()">
<xsl:copy></xsl:copy>
</xsl:template>
<xsl:template match="Headline">
<Price_32pt>
<xsl:apply-templates/>
</Price_32pt>
</xsl:template>
<xsl:template match="Why_Buy">
<Save_15pt>
<xsl:apply-templates/>
</Save_15pt>
</xsl:template>
<xsl:template match="Product_Name">
<Item_Name>
<xsl:copy-of select="Product_Image"/>
<xsl:apply-templates/>
<xsl:copy-of select="Logo_Image_1"/>
<xsl:text>
</xsl:text>
</Item_Name>
</xsl:template>
</xsl:stylesheet>
Cheers, John Bampton.

Re: Element reordering help....

Posted: Thu Mar 26, 2009 10:09 pm
by undrmoons
John this is awesome! Thank you so much for the assistance!