[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Iterate through variable in XSLT?
At 2007-08-18 08:07 +0000, yun Wu wrote:
basically i like to generic produce the following code rather then write it by myself You can use either XML or simple text for this, putting the information into a separate file. But that XML file is as detailed as the file you are producing above, so I'll assume you are producing something more detailed than above. i was thinking to define a global variable $classes and iterate it at runtime but i don't know how to achieve that. If you did that you would be touching your source stylesheets ... it would probably be best not to have to change your stylesheets to get runtime behaviour changes. If you are using XSLT 1 then you are obliged to use XML for your external definition, but if you are using XSLT 2 then you have the option of using simple text. There are two examples below: yunwu1.xsl for XSLT 1.0 and yunwu2.xsl if you want to use text for the external class definition. The yunwu1.xsl file also works with XSLT 2.0. The document() function loads an external XML file ready for use as a set of nodes, while the unparsed-text() function reads an external text file as a string. This way you can keep runtime configuration files separate from your source files but still access them while you are accessing your source file. Your use of the <xsl:attribute> instruction can be replaced with an attribute value template. I hope this helps. . . . . . . . . . Ken
<xsl:output indent="yes"/> <!--template rule for the source--> <xsl:template match="/"> <classes> <!--access external class definitions--> <xsl:for-each select="document('yunwu-classes.xml')/classes/class"> <class name="{.}"/> </xsl:for-each> </classes> </xsl:template> </xsl:stylesheet> T:\ftemp>call xslt yunwu.xml yunwu.xsl yunwu1.out T:\ftemp>echo on T:\ftemp>type yunwu1.out <?xml version="1.0" encoding="utf-8"?> <classes> <class name="ClassA"/> <class name="ClassB"/> <class name="ClassC"/> <class name="ClassD"/> </classes> T:\ftemp>type yunwu-classes.txt ClassA ClassB ClassC ClassD ClassE T:\ftemp>type yunwu2.xsl <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsd="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xsd" version="2.0"> <xsl:output indent="yes"/> <!--template rule for the source--> <xsl:template match="/"> <classes> <!--access external class definitions in text--> <xsl:for-each select="tokenize( unparsed-text('yunwu-classes.txt'),'\r?\n') [normalize-space(.)]"> <class name="{.}"/> </xsl:for-each> </classes> </xsl:template> </xsl:stylesheet> T:\ftemp>call xslt2 yunwu.xml yunwu2.xsl yunwu2.out T:\ftemp>type yunwu2.out <?xml version="1.0" encoding="UTF-8"?> <classes> <class name="ClassA"/> <class name="ClassB"/> <class name="ClassC"/> <class name="ClassD"/> <class name="ClassE"/> </classes> T:\ftemp>rem Done! -- Upcoming public training: XSLT/XSL-FO Sep 10, UBL/code lists Oct 1 World-wide corporate, govt. & user group XML, XSL and UBL training RSS feeds: publicly-available developer resources and training G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995) Male Cancer Awareness Jul'07 http://www.CraneSoftwrights.com/s/bc Legal business disclaimers: http://www.CraneSoftwrights.com/legal
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|