transformation quits after third apply-templates

Here should go questions about transforming XML with XSLT and FOP.
gjledger2k
Posts: 16
Joined: Tue Aug 01, 2006 2:56 am
Location: Chicago

transformation quits after third apply-templates

Post by gjledger2k »

Hi,
Having a problem with transforming code.

I need to select elements based on their (one) parent's attribute, and insert a unique attribute in each element.

Here is the xsl:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<--here I need to recreate the cam element while inserting a namespace declaration-->
<xsl:template match="cam">
<cam xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:apply-templates/>
</cam>
</xsl:template>
<--listing is a child of cam, these represent records from a database; the only thing that changes is the member type. I want to add attributes to listing's children based on each unique member_type attribute.-->
<xsl:template match="listing[@MEMBER_TYPE='CPB']/COMPANY">
<company>
<xsl:attribute name="aid:pstyle">Head_Bronze</xsl:attribute>
<xsl:value-of select="."/>
</company>
<xsl:apply-templates/>
</xsl:template>
<--that worked fine, but instead of moving on to the next template, the transformation simply stops. The code below is not executed.-->

<xsl:template match="listing[@MEMBER_TYPE='CPG']/COMPANY">
<company>
<xsl:attribute name="aid:pstyle">Head_Gold</xsl:attribute>
<xsl:value-of select="."/>
</company>
<xsl:apply-templates/>
</xsl:template>
Here is some partial xml:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<cam>
<listing MEMBER_TYPE="CPB">
<FULL_NAME>Ms. Rebecca Sunny</FULL_NAME>
<COMPANY>ABC Company</COMPANY>
</listing>

<listing MEMBER_TYPE="CPG">
<FULL_NAME>Brook Farms</FULL_NAME>
<COMPANY>XYZ</COMPANY>
</listing>
I know the answer is probably simple, so thanks for taking time to help.
george
Site Admin
Posts: 2095
Joined: Thu Jan 09, 2003 2:58 pm

Re: transformation quits after third apply-templates

Post by george »

I cannot reproduce the problem you mention, that is that the processing stops before matching the second COMPANY element. See below the complete files I tested with

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<!--here I need to recreate the cam element while inserting a namespace declaration-->
<xsl:template match="cam">
<cam xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:apply-templates/>
</cam>
</xsl:template>
<!--listing is a child of cam, these represent records from a database; the only thing that changes is the member type. I want to add attributes to listing's children based on each unique member_type attribute.-->
<xsl:template match="listing[@MEMBER_TYPE='CPB']/COMPANY">
<company>
<xsl:attribute name="aid:pstyle">Head_Bronze</xsl:attribute>
<xsl:value-of select="."/>
</company>
<xsl:apply-templates/>
</xsl:template>
<!--that worked fine, but instead of moving on to the next template, the transformation simply stops. The code below is not executed.-->

<xsl:template match="listing[@MEMBER_TYPE='CPG']/COMPANY">
<company>
<xsl:attribute name="aid:pstyle">Head_Gold</xsl:attribute>
<xsl:value-of select="."/>
</company>
<xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<cam>
<listing MEMBER_TYPE="CPB">
<FULL_NAME>Ms. Rebecca Sunny</FULL_NAME>
<COMPANY>ABC Company</COMPANY>
</listing>

<listing MEMBER_TYPE="CPG">
<FULL_NAME>Brook Farms</FULL_NAME>
<COMPANY>XYZ</COMPANY>
</listing>
</cam>
and here it is the result with Saxon 6.5

Code: Select all


<?xml version="1.0" encoding="utf-8"?>
<cam xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">

Ms. Rebecca Sunny
<company aid:pstyle="Head_Bronze">ABC Company</company>ABC Company



Brook Farms
<company aid:pstyle="Head_Gold">XYZ</company>XYZ

</cam>
Now, from your description you seem to want to copy the input and just add an aid:pstyle attribute on COMPANY... Check the stylesheet below:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:output method="xml"/>

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

<!-- This is optional, just to have the aid namespace declaration on the root -->
<xsl:template match="cam">
<cam xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<xsl:apply-templates select="node() | @*"/>
</cam>
</xsl:template>

<xsl:template match="listing[@MEMBER_TYPE='CPB']/COMPANY">
<xsl:copy>
<xsl:attribute name="aid:pstyle">Head_Bronze</xsl:attribute>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="listing[@MEMBER_TYPE='CPG']/COMPANY">
<xsl:copy>
<xsl:attribute name="aid:pstyle">Head_Gold</xsl:attribute>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The result is

Code: Select all


<?xml version="1.0" encoding="utf-8"?><cam xmlns:aid="http://ns.adobe.com/AdobeInDesign/4.0/">
<listing MEMBER_TYPE="CPB">
<FULL_NAME>Ms. Rebecca Sunny</FULL_NAME>
<COMPANY aid:pstyle="Head_Bronze">ABC Company</COMPANY>
</listing>

<listing MEMBER_TYPE="CPG">
<FULL_NAME>Brook Farms</FULL_NAME>
<COMPANY aid:pstyle="Head_Gold">XYZ</COMPANY>
</listing>
</cam>
Best Regards,
George
George Cristian Bina
gjledger2k
Posts: 16
Joined: Tue Aug 01, 2006 2:56 am
Location: Chicago

Re: transformation quits after third apply-templates

Post by gjledger2k »

George,
Thanks so much. I did use your alternate code, and it worked great. I just had a few more bugs to work out before I could respond to your reply.

Thanks again.
Post Reply