Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 17th, 2009, 04:15 PM
Registered User
 
Join Date: Jul 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Dynamically determining the name of an attribute-set

I have a need to dynamically determine the name of an attribute-set. I'm restricted to using XSLT 1.0 and we're currently using Xalan 2.7.1 for Java in both a Windows XP environment as well as an AIX environment.

Some history... I'm creating a stylesheet template that can be included in other developer's stylesheets that will generate "Link" tags for them. The system that I'm constrained to use is a custom portal application that has a content XML language that it uses to render HTML. It's a long story. Basically, a Link tag renders an HTML Anchor tag in the end. The DTD for the Link tag is as follows:
Code:
<!ELEMENT Link (#PCDATA | Image | Bold | Italic | Underline | LineBreak | Sup | MouseOverText)*>
<!ATTLIST Link
	copy CDATA #IMPLIED
	url CDATA #IMPLIED
	target CDATA #IMPLIED
	proxy (false | true) #IMPLIED
	class CDATA #IMPLIED
	onmouseover CDATA #IMPLIED
	onmouseout CDATA #IMPLIED
	onclick CDATA #IMPLIED
	serviceid CDATA #IMPLIED
	serviceparams CDATA #IMPLIED
	componentid CDATA #IMPLIED
	params CDATA #IMPLIED
	contentID CDATA #IMPLIED
	views CDATA #IMPLIED
	groups CDATA #IMPLIED
	rule CDATA #IMPLIED
	ruledata CDATA #IMPLIED
	ruleaction (include | exclude) #IMPLIED
	title CDATA #IMPLIED
	type (doc | secure-doc | anchor | external | internal) "internal"
>
I have to have a template to generate the Link tags because the template must evaluate the value of an XML node at runtime to determine the value of one of the attributes on the Link tag. Here is what I have so far.

Included Stylesheet
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
	
	<xsl:variable name="simulatedUser" select="/Message/MessageControlArea/transactionOriginLoggedInUserId"/>
	
	<xsl:template name="SimulateLink">
		<xsl:param name="realServiceid"/>
		<xsl:param name="content"/>
		<xsl:param name="attrSetName"/>
		
		<xsl:element name="Link" use-attribute-sets="linkAttrSet">
			<xsl:choose>
				<xsl:when test="$simulatedUser = 'N/A' ">
					<!--  ********** Not simulating -->
					<xsl:attribute name="serviceid"><xsl:value-of select="$realServiceid"/></xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<!--  ********** Simulating -->
					<xsl:attribute name="serviceid">LFGSimulateUserService</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:copy-of select="$content"/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
EXAMPLE:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

	<xsl:attribute-set name="linkAttrSet">
		<xsl:attribute name="copy"><xsl:value-of select="current()//ProducerID"/></xsl:attribute>
		<xsl:attribute name="serviceparams">ProducerID=<xsl:value-of select="current()//ProducerID"/>&amp;CompanyCode=<xsl:value-of select="current()//CompanyCode"/></xsl:attribute>
		<xsl:attribute name="ServiceID">LFGPOD</xsl:attribute>
		<xsl:attribute name="ServiceInterface">GetPODProducerDetail</xsl:attribute>
	</xsl:attribute-set>
	
	<xsl:variable name='hubServiceId'>MyRealServiceId</xsl:variable>
	<xsl:variable name="simulatedUser" select="/Message/MessageControlArea/transactionOriginLoggedInUserId"/>

	<!-- Output Main Entry Point -->
	<xsl:template match="/">
		<WebPage>
			<PrinterFriendly visible="true"/>
			<Body>
				<xsl:apply-templates select="/Message/Output"/>
			</Body>
		</WebPage>
	</xsl:template>
	
	<xsl:template match="Output">
		<Form name="podEntryForm" method="post" action="">
			<xsl:call-template name="SimulateForm">
				<xsl:with-param name="realServiceid" select="$hubServiceId"/>
			</xsl:call-template>
			
			<table cellspacing="0" cellpadding="3" width="100%">

				<xsl:for-each select="PODEntry[not(ProducerMessage)]">
					<xsl:call-template name="PODEntry"/>
				</xsl:for-each>
			</table>

		</Form>
	</xsl:template>

	<xsl:template name="PODEntry">
		
		<tr>
			<td align="left">
						<xsl:call-template name="SimulateLink">
							<xsl:with-param name="realServiceid" select="$hubServiceId"/>
							<xsl:with-param name="content"/>
							<xsl:with-param name="attrSetName">linkAttrSet</xsl:with-param>
						</xsl:call-template>

						<!--<Link>
							<xsl:attribute name="copy"><xsl:value-of select="ProducerID"/></xsl:attribute>
							<xsl:attribute name="serviceid"><xsl:value-of select="$hubServiceId"/></xsl:attribute>
							<xsl:attribute name="serviceparams">ServiceID=LFGPOD&amp;ServiceInterface=GetPODProducerDetail&amp;ProducerID=<xsl:value-of select="ProducerID"/>&amp;CompanyCode=<xsl:value-of select="CompanyCode"/></xsl:attribute>
						</Link>-->
			</td>
		</tr>
	</xsl:template>

	<xsl:template name="SimulateForm">
		<xsl:param name="realServiceid"/>
		
		<xsl:choose>
			<xsl:when test="$simulatedUser = 'N/A' ">
				<!--  ********** Not simulating -->
				<xsl:attribute name="serviceid"><xsl:value-of select="$realServiceid"/></xsl:attribute>
			</xsl:when>
			<xsl:otherwise>
				<!--  ********** Simulating -->
				<xsl:attribute name="serviceid">LFGSimulateUserService</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="SimulateLink">
		<xsl:param name="realServiceid"/>
		<xsl:param name="content"/>
		<xsl:param name="attrSetName"/>
		
		<!--<xsl:element name="Link" use-attribute-sets="{$attrSetName}">-->
		<xsl:element name="Link" use-attribute-sets="linkAttrSet">
			<xsl:choose>
				<xsl:when test="$simulatedUser = 'N/A' ">
					<!--  ********** Not simulating -->
					<xsl:attribute name="serviceid"><xsl:value-of select="$realServiceid"/></xsl:attribute>
				</xsl:when>
				<xsl:otherwise>
					<!--  ********** Simulating -->
					<xsl:attribute name="serviceid">LFGSimulateUserService</xsl:attribute>
				</xsl:otherwise>
			</xsl:choose>
			<xsl:copy-of select="$content"/>
		</xsl:element>
	</xsl:template>
</xsl:stylesheet>
SAMPLE XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<Message>
	<Input>
		<LFGApplicationID>server1</LFGApplicationID>
		<LFGServiceID>LocalServiceId</LFGServiceID>
		<LFGUserSessionID>AF88AEDE5B06A7F9B7701C847B7D7A32</LFGUserSessionID>
		<LFGView>aView</LFGView>
		<ServiceID>POD</ServiceID>
		<ServiceInterface>GetPODEntryList</ServiceInterface>
	</Input>
	<MessageControlArea>
		<interface>GetPODEntryList</interface>
		<messageDateTime/>
		<messageId>1b08a109:1227ee2e104:-7fdc</messageId>
		<serviceName>LFGPOD</serviceName>
		<transactionOriginSourceCode>Hub</transactionOriginSourceCode>
		<transactionOriginUserId>testPOD</transactionOriginUserId>
		<!--
		<transactionOriginLoggedInUserId>N/A</transactionOriginLoggedInUserId>
		-->
		<transactionOriginLoggedInUserId>Mark</transactionOriginLoggedInUserId>
		<transactionOriginSourcePassword>
			<encryptionTypeCode>None</encryptionTypeCode>
			<password>None</password>
		</transactionOriginSourcePassword>
		<transactionOriginUserPassword>
			<encryptionTypeCode>hub</encryptionTypeCode>
			<password>7c6a180b36896a0a8c02787eeafb0e4c</password>
		</transactionOriginUserPassword>
		<transactionOriginLoggedInUserPassword>
			<encryptionTypeCode>N/A</encryptionTypeCode>
			<password>N/A</password>
		</transactionOriginLoggedInUserPassword>
		<transactionId>1b08a109:1227ee2e104:-7fdb</transactionId>
	</MessageControlArea>
	<Output>
		<PODEntry>
			<ProducerID>9990352</ProducerID>
			<ProducerMessage>Payment on Demand only allows one transaction per day.</ProducerMessage>
			<CompanyCode>MAB</CompanyCode>
		</PODEntry>
		<PODEntry>
			<ProducerID>9995859</ProducerID>
			<ProducerMessage>Payment on Demand only allows one transaction per day.</ProducerMessage>
			<CompanyCode>MAB</CompanyCode>
		</PODEntry>
		<PODEntry>
			<ProducerID>9990002531</ProducerID>
			<ProducerMessage>Payment on Demand only allows one transaction per day.</ProducerMessage>
			<CompanyCode>MAB</CompanyCode>
		</PODEntry>
		<PODEntry>
			<ProducerID>9990014716</ProducerID>
			<MinAvailable>500</MinAvailable>
			<RequestAmt>0</RequestAmt>
			<CompanyCode>MAB</CompanyCode>
			<AvailableAmt>250000</AvailableAmt>
			<MaxAvailable>250000</MaxAvailable>
		</PODEntry>
		<PODEntry>
			<ProducerID>0649720000</ProducerID>
			<MinAvailable>0</MinAvailable>
			<ProducerMessage>Available Amount Below Minimum</ProducerMessage>
			<CompanyCode>MAB</CompanyCode>
			<AvailableAmt>0</AvailableAmt>
			<MaxAvailable>0</MaxAvailable>
		</PODEntry>
	</Output>
</Message>
What I'm trying to accomplish is to allow developers to create their own attribute-sets and pass the attribute-set name into the SimulateLink template and have the template generate Link tags using the attribute set the developer identified. The template works fine if I hardcode the name of the attribute-set in the use-attribute-sets attribute of the xsl:element tag. If I attempt to use an attribute value template, it doesn't expand the name.

Is there any way that I can provide a dynamic set of attributes to a template?

Thanks,
Mark
 
Old July 17th, 2009, 04:23 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>What I'm trying to accomplish is to allow developers to create their own attribute-sets and pass the attribute-set name into the SimulateLink template and have the template generate Link tags using the attribute set the developer identified.

Can't be done. Like template names, modes, variable names, and many other language constructs, attribute set names must be statically known. You'll have to find another approach. For example, you could use dynamic generation/modification of the stylesheet source code - though this could be expensive.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert attribute to node-set using xalan Coolcampers XSLT 1 May 3rd, 2008 06:06 AM
how to give attribute value dynamically gantait XSLT 6 February 17th, 2007 05:00 AM
set image size from attribute bonekrusher XSLT 12 August 10th, 2006 05:49 AM
Error - Attribute cannot be set now ajish_jose Access 6 February 1st, 2005 04:38 AM
Dynamically set selected attribute fs22 Javascript How-To 5 December 24th, 2004 04:10 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.