This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

accessing elements created on the fly


Hi All,

I have some xml that arrives with elements containing
comma-delimited text like this:
<months>19991225,20000130,20010615<months>

However, I've written my XSLT transforms that generate
html based on input xml like this:
<month>19991225</month><month>20000130</month><month>20010615</month>

So, I've created the attached XSLT to convert the
comma-delimited <months></months> elements into
<month></month> elements.  

Being quite new to XSLT, I'm currently thinking I'll
have to perform the following two steps separately:

1. transform the original xml containing
<months></months> into xml using <month></month>
elements
2. transform the xml from step 2 into html using the
XSLT designed to work with <month></month> elements

Is there any way to do both in one XSLT?  That is, can
the XSLT reference the elements created on the fly?
Ideally, I'd like to create an XSLT that I could
import into my XSLT that does the html transform so
that it the messy code that converts the
comma-delimited <months></months> elements does not
clutter up the xslt that does the html transform.

Thanks

 

---- Source XML file ----
<?xml version="1.0" encoding="UTF-8"?>
<root>
	<client number="0000012345">
<months>199901,199902,200101,200105</months>
	</client>
</root>


---- XSLT file used to convert <months></months>
elements ----
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">
	<xsl:import href="helper.xslt"/>
	<xsl:output method="xml" version="1.0"
encoding="UTF-8" indent="yes"/>
	
	<!-- Identity transformation template -->
  	<xsl:template match="@* | * | comment() |
processing-instruction() | text()">
    		<xsl:copy>
     			<xsl:apply-templates/>
    		</xsl:copy>
 	 </xsl:template>
	
	<!-- Convert <months>YYYYMM,YYYYMM,YYYYMM</months>"
elements to:
	                   
<month>YYYYMM</month><month>YYYYMM</month>...elements
-->
	<xsl:template match="months">
		<xsl:call-template
name="convert-delimited-text-to-elements">
			<xsl:with-param name="text" select="text()"/>
			<xsl:with-param name="delimiter" select="','"/>
			<xsl:with-param name="element-name"
select="'month'"/>
    		</xsl:call-template>
	</xsl:template>

</xsl:stylesheet>

 ---- Source for helper.xslt ---- 
<?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:template
name="convert-delimited-text-to-elements">
		<xsl:param name="text"/>
		<xsl:param name="delimiter"/>
		<xsl:param name="element-name"/>
		<xsl:choose>
			<!-- if the passed in value contains the specified
delimiter... -->
			<xsl:when test="contains($text,$delimiter)">
				<!-- create an element using the specified element
name and the value preceding the delimiter -->
					<xsl:element name="{$element-name}">
						<xsl:value-of
select="normalize-space(substring-before($text,$delimiter))"/>
					</xsl:element>
				<!-- now recursively call this template again to
convert the remainder of the string -->
				<xsl:call-template
name="convert-delimited-text-to-elements">
					<xsl:with-param name="text"
select="substring-after($text,$delimiter)"/>
					<xsl:with-param name="delimiter"
select="$delimiter"/>
					<xsl:with-param name="element-name"
select="$element-name"/>
				</xsl:call-template>
			</xsl:when>
			<!-- handle the last value -->
			<xsl:when test="$text != ' '">
				<xsl:element name="{$element-name}">
					<xsl:value-of select="normalize-space($text)"/>
				</xsl:element>
			</xsl:when>
		</xsl:choose>
	</xsl:template>
</xsl:stylesheet>


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]