Click to See Complete Forum and Search --> : variiable select


fiddes_c
December 15th, 2003, 11:33 AM
hi,
I want to display an element (either 'A' or 'C'), dependant on a value coming across in the xml file.

I'm using
<xsl:template name="TITLE">
<xsl:variable name="TitleDigit">
<xsl:value-of select="string(FormTop/template)"/>
</xsl:variable>
<xsl:choose>

<!-- When Template = 1 then display 'A' or when =2 display 'C' -->
<xsl:when test="$TitleDigit=1">
<xsl:element name="FORM">
<xsl:text>A</xsl:text>
</xsl:element>
</xsl:when>

<xsl:when test="$TitleDigit=2">
<xsl:element name="FORM">
<xsl:text>C</xsl:text>
</xsl:element>
</xsl:when>

</xsl:choose>
</xsl:template>

However, the xml looks something like this:
<FormTop template="1">
<Lower>
<Data1>Misc</Data1>
<Data2>Misc</Data2>
</Lower>
</FormTop>

As you can see the value for the variable "TitleDigit" come from the top level node "FormTop" and its attribute template.
How do I select the value of template?

Cheers
Cormac

khp
December 15th, 2003, 08:34 PM
To get the value of the attribute template, you should write
number(FormTop/@template)

fiddes_c
December 16th, 2003, 04:20 AM
khp,
thanks for your reply,

still no joy with that, the xsl is as follows:
<xsl:template name="TITLE">
<xsl:variable name="TitleDigit">
<xsl:value-of select="number(FormTop/@template)"/>
</xsl:variable>
<xsl:choose>

<!-- When Template = 1 then display 'A' or when =2 display 'C' -->
<xsl:when test="$TitleDigit=1">
<xsl:element name="FORM">
<xsl:text>A</xsl:text>
</xsl:element>
</xsl:when>

<xsl:when test="$TitleDigit=2">
<xsl:element name="FORM">
<xsl:text>C</xsl:text>
</xsl:element>
</xsl:when>

</xsl:choose>
</xsl:template>

However, the xml looks something like this:
<FormTop template="1">
<Lower>
<Data1>Misc</Data1>
<Data2>Misc</Data2>
</Lower>
</FormTop>

However, I still get nothing for the element FORM.
Am I missing something here?
cheers
Cormac

khp
December 16th, 2003, 05:04 AM
I'am having a hard time figuring out, what you are trying to do. Could you please post an example of your source xml file, your desired output, and your actual output.

fiddes_c
December 16th, 2003, 06:25 AM
khp,
no problem, suppose without the files it's difficult to get the full picture.

xml example:
<?xml version="1.0"?>
<JOB name="g_type" xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
<!-- Job Level Meta data. Should be generated in all cases, even for one document -->
<META>
<BatchID>315</BatchID>
<BatchFlag>Y</BatchFlag>
<UserID>FIDDES_C</UserID>
</META>
<DOCUMENT template="portrait_e01">
<!-- Document Level Meta Data. Generated for each document in the batch -->
<META>
<DocumentName>Customer Account</DocumentName>
<TargetType>P</TargetType>
<TargetAddress>Printer Address</TargetAddress>
<NumberOfCopies>1</NumberOfCopies>
</META>

<HEADER>
<!-- Header Information. -->
<PaymentContactTitle>Mrs</PaymentContactTitle>
<PaymentContactFirstName>June</PaymentContactFirstName>
<PaymentContactSurname>Summers</PaymentContactSurname>
<PaymentContactAddress1>On top of the Hill</PaymentContactAddress1>
<PaymentContactAddress2>A long Walk</PaymentContactAddress2>
<PaymentContactAddress3>From the bottom</PaymentContactAddress3>
<PaymentContactAddress4>Bring an apple</PaymentContactAddress4>
<PaymentContactAddress5>And some water</PaymentContactAddress5>
<Name>ABC LTd.</Name>
<Date>01-Jan-2003</Date>
<From>01-Jan-2003</From>
<To>31-Dec-2003</To>
<Frequency>Monthly</Frequency>
<Payment>Direct Debit</Payment>
<Title>Mrs</Title>
<ContactName>June</ContactName>
<ContactLastName>Summers</ContactLastName>
<InvoiceNumber>112233</InvoiceNumber>
<Currency>Euro</Currency>
<Balance>1000.00</Balance>
<OfWhichUnreconciled>1000.00</OfWhichUnreconciled>
<Period>600.00</Period>
<AmountDue>1600.00</AmountDue>
<PaymentDue>01-Feb-2003</PaymentDue>
</HEADER>

<!-- Placeholder for Fixed TextBlock. Always after HEADER. Currently does not contain any data elements -->
<TEXTBLOCK/>

<!-- Signature Block. Maybe placed in any location of the data stream -->
<SIGNATURE>
<SignatureUserID></SignatureUserID>
</SIGNATURE>

<!-- Placeholder for Fixed Detail Header. Should be before any detail lines. May contain data elements -->
<MEMBER_DETAILS_HEADER />

<!-- Repeating Item Lines below -->
<MEMBER_DETAILS>
<PNumber>1234567</PNumber>
<Name>Sean Murphy</Name>
<SNumber>A1234</SNumber>
<NetPeriod>100.00</NetPeriod>
<CompanyPeriod>50.00</CompanyPeriod>
<OtherAmountPeriod>50.00</OtherAmountPeriod>
<DueThisPeriod>100.00</DueThisPeriod>
</MEMBER_DETAILS>

</DOCUMENT>

</JOB>

XSL example:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0"
version="1.0">
<xsl:template match="DOCUMENT">
<DOCUMENT>
<xsl:call-template name="FORMTITLE"/>
<xsl:apply-templates select="descendant::META"/>
<xsl:apply-templates select="descendant::HEADER"/>
<xsl:apply-templates select="descendant::TEXTBLOCK"/>
<xsl:apply-templates select="descendant::MEMBER_DETAILS_HEADER"/>
<xsl:apply-templates select="descendant::MEMBER_DETAILS"/>
</DOCUMENT>
</xsl:template>

<!--
All Elements within the block META will need to be defined as Globals.
Use * to indicate that all elements of META will be added to .dat file
-->

<xsl:template match="META">
<META>
<xsl:apply-templates select="descendant::*"/>
</META>
</xsl:template>

<xsl:template match="META//*">
<xsl:call-template name="global"/>
</xsl:template>

<!--
All Elements within the block HEADER will need to be defined as Globals.
Use * to indicate that all elements of HEADER will be added to .dat file
-->
<xsl:template match="HEADER">
<HEADER>
<xsl:apply-templates select="descendant::*"/>
</HEADER>
</xsl:template>

<xsl:template match="HEADER//*">
<xsl:call-template name="global"/>
</xsl:template>

<!--
Define the group MEMBER_DETAILS_HEADER
-->
<xsl:template match="MEMBER_DETAILS_HEADER">
<MEMBER_DETAILS_HEADER>
<xsl:call-template name="G_MEMBER_DETAILS_HEADER"/>
</MEMBER_DETAILS_HEADER>
</xsl:template>

<!--
Define the elements that are required within the block MEMBER_DETAILS
-->
<xsl:template match="MEMBER_DETAILS">
<MEMBER_DETAILS>
<xsl:apply-templates select="descendant::PNumber"/>
<xsl:apply-templates select="descendant::Name"/>
<xsl:apply-templates select="descendant::SNumber"/>
<xsl:apply-templates select="descendant::NetPeriod"/>
<xsl:apply-templates select="descendant::CompanyPeriod"/>
<xsl:apply-templates select="descendant::OtherAmountPeriod"/>
<xsl:apply-templates select="descendant::DueThisPeriod"/>
<xsl:call-template name="G_MEMBER_DETAILS"/>
</MEMBER_DETAILS>
</xsl:template>

<!--
This default template will output the element only if the
field contains something non-blank.
-->
<xsl:template match="*">
<xsl:if test="normalize-space()!=''">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:if>
</xsl:template>

<!--
Force a specified field to be a global field
-->
<xsl:template name="global">
<xsl:element name="{name() }">
<xsl:attribute name="xfa:match">many</xsl:attribute>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<!-- Subform for G_MEMBER_DETAILS_HEADER
-->
<xsl:template name="G_MEMBER_DETAILS_HEADER">
<xsl:element name="MEMBER_DETAILS_HEADER">
<xsl:attribute name="xfa:dataNode">dataGroup</xsl:attribute>
<xsl:text>begin</xsl:text>
</xsl:element>
</xsl:template>

<!-- Subform for G_MEMBER_DETAILS
-->
<xsl:template name="G_MEMBER_DETAILS">
<xsl:element name="MEMBER_DETAILS">
<xsl:attribute name="xfa:dataNode">dataGroup</xsl:attribute>
<xsl:text>begin</xsl:text>
</xsl:element>
</xsl:template>

<!--
Form Command thrown at start of each form.
-->
<xsl:template name="FORMTITLE">
<xsl:variable name="FormDigit">
<xsl:value-of select="string(DOCUMENT/@template"/>
</xsl:variable>

<xsl:choose>
<xsl:when test="$FormDigit=g_landscape">
<xsl:element name="FORMTITLE">
<xsl:text>^form g_landscape.mdf</xsl:text>
</xsl:element>
</xsl:when>

<xsl:when test="$FormDigit=g_portrait">
<xsl:element name="FORMTITLE">
<xsl:text>^form g_portrait.mdf</xsl:text>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>

To explain a little the process, the xml file is merged with the xsl via a merge product and it's controlled by perl scripts.
The end product of the merge is a .dat file.
Within the dat file a element called "FORMTITLE" is laid down with the correct compiled name of a form, which is a .mdf.
I need now to lay down the name of the .rdf dependant on the value of the document attribute called template,
e.g. if <DOCUMENT template="portrait_e01"> i.e. template is "portrait_e01" then lay formtitle down as "g_portrait.mdf".

That perhaps sounds a bit muddled, but the process is working perfectly, until of course, the need for variable formtitle names came in.
Any idea's greatly appreciated.
Originally the attribute template of document was going to be a number, but this has now changed to text.
cheers
Cormac

khp
December 16th, 2003, 08:35 AM
You got two problems.

1. when you call the FORMTITLE template, the DOCUMENT element is the current element. Which means that the xpath expression DOCUMENT/@template in the FORMTITLE template isn't valid. It should be just @template. Or more explicitly self::DOCUMENT/@template.

2. The test expression test="$FormDigit=g_landscape", doesn't do string comparison as you might expect, it compares the set of child elements called g_landscape, with the variable $FormDigit. To do string comparison it should be test="$FormDigit='g_landscape'"

fiddes_c
December 16th, 2003, 09:35 AM
khp,
as usual, you are spot on!

I am already level, so I can go straight to the template attribute.

I also changed for the string match,
as follows:

I had this working when I added a another tag into the meta level data, but it wasn't the requirements. It has to be the document level attribute. ( I didn't have to use the self::document/@template, just @template)

Thanks a million!
Cormac

<xsl:template name="FORMTITLE">
<xsl:variable name="Formname">
<xsl:value-of select="@template"/>
</xsl:variable>
<xsl:choose>

<xsl:when test="$Formname='g_portrait'">
<xsl:element name="FORMTITLE">
<xsl:text>^form g_portrait.mdf</xsl:text>
</xsl:element>
</xsl:when>

<xsl:when test="$Formname='g_landscape'">
<xsl:element name="FORMTITLE">
<xsl:text>^form g_landscape.mdf</xsl:text>
</xsl:element>
</xsl:when>

<!-- default is g_normal, both portrait and landscape -->
<xsl:otherwise>
<xsl:element name="FORMTITLE">
<xsl:text>^form g_normal.mdf</xsl:text>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

fiddes_c
December 18th, 2003, 07:31 AM
hi,
actually after reading up on xpath, I can also use attribute::template instead of @template, same result.
cheers
Cormac

was:
<xsl:template name="FORMTITLE">
<xsl:variable name="Formname">
<xsl:value-of select="@template"/>
</xsl:variable>
<xsl:choose>

can use instead:
<xsl:template name="FORMTITLE">
<xsl:variable name="Formname">
<xsl:value-of select="attribute::template"/>
</xsl:variable>
<xsl:choose>