Subject: [xsl] How to use an attribute value passed as a param to a named template for pattern matching From: "Neal Mulvenna" <mulvenna@xxxxxxxxxx> Date: Sun, 11 Mar 2001 22:08:52 -0500 |
Hi, this is my first post to this newly discovered usergroup. I am experimenting with using XSLT in an integration environment for XML-XML transforms. More about that in future posts. right now I'm trying to find out how to use named templates with pattern matching info passed to them as params. The example included shows three different named templates: - "oneparam" template (param = element to be matched) works fine, although I am not sure why. I found the solution in the FAQ, but would like to understand why it works and why the 'intuitively obvious' way doesn't. Suggested reading please. - "twoparam" template doesn't work. This is just one of many that I've tried, all fail. What is the trick for handling the attribute value properly? - "simple" template works fine, the pattern matching was first done in the main template. But the only way I know of to properly position prior to the named template call is to do a for-each block, even if I know there is just a single occurence. Is this inefficient? And, while I'm at it, what is wrong with my single total line sum( a * b) statement in the example (commented out because of the error it generates)? I tried a couple things from the Math examples in the FAQ, no luck. I am using Lotus xsl 2.0.0 (xalan) from the IBM alphaworks site Thanks in advance for any insight you may provide. On to the example: First the input XML, NealExample.xml: <!-- NealExample.xml --> <Top> <Repeater index="1"> <Try1> <Value>1</Value> </Try1> <Try2 type="a"> <Value>2</Value> </Try2> <Try2 type="b"> <Value>3</Value> </Try2> </Repeater> <Repeater index="2"> <Try1> <Value>11</Value> </Try1> <Try2 type="a"> <Value>12</Value> </Try2> <Try2 type="b"> <Value>3.1</Value> </Try2> </Repeater> </Top> Now the xsl, NealExample.xsl <xsl:stylesheet version="1.0" xmlns:xsl ="http://www.w3.org/1999/XSL/Transform"> <!-- ======================================================================== NealExample.xsl ======================================================================== --> <xsl:template match="/"> <Output> <xsl:for-each select="//Repeater"> <Group> <Note><xsl:text>hello</xsl:text></Note> <Catch1> <xsl:call-template name="oneparam"> <xsl:with-param name="p_node">Try1</xsl:with-param> </xsl:call-template> </Catch1> <Catch2> <xsl:call-template name="twoparam"> <xsl:with-param name="p_node">Try2</xsl:with-param> <!-- <xsl:with-param name="p_attr">a</xsl:with-param> --> <xsl:with-param name="p_attr" select="a"/> </xsl:call-template> </Catch2> <xsl:for-each select="Try2[@type='b']"> <Catch3> <xsl:call-template name="simple"/> </Catch3> </xsl:for-each> </Group> </xsl:for-each> <Totals> <Total_1> <xsl:value-of select="sum(//Try2[@type='b']/Value)"/> </Total_1> <Total_2x3> <!-- Following commented out because it gives this error: XSLT Error (javax.xml.transform.TransformerException): Can not convert #NUMBER to a NodeList! <xsl:value-of select="sum(//Try2[@type='a']/Value * //Try2[@type='b']/Value)"/> end of comment-out --> </Total_2x3> </Totals> </Output> </xsl:template> <!-- ========== named templates follow ========== --> <xsl:template name="oneparam"> <xsl:param name="p_node">.</xsl:param> <xsl:value-of select="*[name()=$p_node]/Value"/> </xsl:template> <xsl:template name="twoparam"> <xsl:param name="p_node">.</xsl:param> <xsl:param name="p_attr"></xsl:param> <xsl:value-of select="*[name()=$p_node][(@type=$p_attr)]/Value"/> </xsl:template> <xsl:template name="simple"> <xsl:value-of select="./Value"/> </xsl:template> </xsl:stylesheet> And this is the generated output XML: <?xml version="1.0" encoding="UTF-8"?> <Output> <Group> <Note>hello</Note> <Catch1>1</Catch1> <Catch2/> <Catch3>3</Catch3> </Group> <Group> <Note>hello</Note> <Catch1>11</Catch1> <Catch2/> <Catch3>3.1</Catch3> </Group> <Totals> <Total_1>6.1</Total_1> <Total_2x3/> </Totals> </Output> Neal Mulvenna, IBM Application Integration and Middleware Services and Solutions Internet: mulvenna@xxxxxxxxxx XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] How to disable escaping o, Michael Kay | Thread | RE: [xsl] How to use an attribute v, Hahn, Kimberly |
[xsl] Need example of PARAM in XALA, Roshan Sharma | Date | Re: [xsl] How to disable escaping o, Mike Brown |
Month |