 |
| 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
|
|
|
|

February 2nd, 2007, 04:34 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Template generalization
Hallo all,
my project manager wanted from me to build a generelized template for date check. What I have already done was something like that.
<xsl:template match="messagedate" name="messagedate">
<xsl:variable name="messageDate">
<xsl:value-of select="./messagedate"/>
</xsl:variable>
<xsl:variable name="cc">
<xsl:value-of select="substring($messageDate,1,2)"/>
</xsl:variable>
<xsl:variable name="yy">
<xsl:value-of select="substring($messageDate,3,2)"/>
</xsl:variable>
<xsl:variable name="mm">
<xsl:value-of select="substring($messageDate,5,2)"/>
</xsl:variable>
<xsl:variable name="dd">
<xsl:value-of select="substring($messageDate,7,2)"/>
</xsl:variable>
<xsl:variable name="HH">
<xsl:value-of select="substring($messageDate,9,2)"/>
</xsl:variable>
<xsl:variable name="MM">
<xsl:value-of select="substring($messageDate,11,2)"/>
</xsl:variable>
<xsl:variable name="SS">
<xsl:value-of select="substring($messageDate,13,2)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="number($cc) >20 or number($cc)<19 ">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">cc</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be either 19 or 20</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test ="number($yy) >99 or number($yy)< 0">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">yy</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 00 and 99</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test="number($mm)>12 or number($mm)<1">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">mm</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 01 and 12</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test="number($dd)>31 or number($dd)<1 ">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">dd</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 01 and 31</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test="number($HH)>23 or number($HH)<0">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">HH</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 00 and 23</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test="number($MM)>59 or number($MM)<0">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">MM</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 00 and 59</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:when test="number($MM)>59 or number($MM)<0">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">SS</xsl:attribute>
<xsl:attribute name="message">Illegal value. The value should be between 00 and 59</xsl:attribute>
</xsl:element>
</xsl:when>
</xsl:choose>
</xsl:template>
So this is oinly for messagedate element. I have to make this for all elements like messagedate and it should also give the attribute name as the element name in which I have to use it. I think I have to define a parameter but I am clueless. Can anyone help me? It is quite urgent.
Your attitude determines your altitude
__________________
Your attitude determines your altitude
|
|

February 2nd, 2007, 04:45 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
I haven't examined all the logic but if it works as required you just need to modify it to use a param as you said:
Code:
<xsl:template name="validateDate">
<xsl:param name="messageDate"/>
</xsl:template>
You can then use the code as before but remove the variable declaration. To call the template use:
Code:
<xsl:call-template name="validateDate">
<xsl:with-param name="messageDate" select="some xpath here"/>
</xsl:call-template>
and use the relevant XPath in the xsl:param select attribute.
I'm not sure what you mean by the last part about attribute name, can you show an example?
--
Joe ( Microsoft MVP - XML)
|
|

February 2nd, 2007, 04:51 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
for instance
<xsl:choose>
<xsl:when test="not(./messagedate)">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">messagedate</xsl:attribute>
<xsl:attribute name="message">Element does not exist</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name ="messagedate"/>
</xsl:otherwise>
</xsl:choose>>
here is the attribute name messagedate in my template it is also messagedate. So if I generalize it it should take the element name instead of messagedate. How can I do it?
Your attitude determines your altitude
|
|

February 2nd, 2007, 05:10 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well generally you can use the name() function to return a node's name but if it doesn't exist then I don't see how that can work.
For other errors you can use:
Code:
<xsl:attribute name="name"><xsl:value-of select="name($messageDate)"/></xsl:attribute>
The other option would be to pass a second parameter:
Code:
<xsl:call-template name="validateDate">
<xsl:with-param name="messageDate" select="some xpath here"/>
<xsl:with-param name="xpath" select="'some xpath here'"/>
</xsl:call-template>
Note the single quotes around the XPath. Then have:
Code:
<xsl:template name="validateDate">
<xsl:param name="messageDate"/>
<xsl:param name="xpath"/>
<xsl:choose>
<xsl:when test="not($messageDate)">
<error type="element" name="{$xpath}" message="Element does not exist" name="error" />
</xsl:when>
</xsl:choose>
</xsl:template>
But that's from inside the template, I don't think you can do what you want outside of it unless you use a proprietary extension.
--
Joe ( Microsoft MVP - XML)
|
|

February 2nd, 2007, 05:15 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Well actually I have to use XSLT 1.0 specifications. What do you mean by xpath expresions? Can you make it a bit more clear? Thanks for your answer
Your attitude determines your altitude
|
|

February 2nd, 2007, 05:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well in your first example you had ./messagedate as your XPath to slect the node to be tested but as you want to use this template generally that will change.
--
Joe ( Microsoft MVP - XML)
|
|

February 2nd, 2007, 06:20 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
joe can you tell me how can I get the element name as a variable in XSLT1.0? I mean for example
<xsl:choose>
<xsl:when test="not($messageDate)">
<error type="element" name="{$xpath}" message="Element does not exist" name="error" />
</xsl:when>
</xsl:choose>
here for example instead of $xpath I want to get the name of the messagedate tag. How can I do it in XSLT 1.0 with param?
Your attitude determines your altitude
|
|

February 2nd, 2007, 06:30 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You can't because what is the name of an element that does not exist?
--
Joe ( Microsoft MVP - XML)
|
|

February 2nd, 2007, 06:40 AM
|
|
Friend of Wrox
|
|
Join Date: May 2005
Posts: 140
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
What I am tryin to do is like this:
<xsl:template name="Date204">
<xsl:param name="Date204"/>
<xsl:param name="Element"/>
.......
</xsl:template>
<xsl:choose>
<xsl:when test="not(./messagedate)">
<xsl:element name="error">
<xsl:attribute name="type">element</xsl:attribute>
<xsl:attribute name="name">messagedate</xsl:attribute>
<xsl:attribute name="message">Element does not exist</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name ="Date204">
<xsl:with-param name="Date204" select="messagedate"/>
<xsl:with-param name ="Element" select ="<elementname>"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
That means I want to take this Element parameter als elementtagname.
Your attitude determines your altitude
|
|

February 2nd, 2007, 06:46 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Code:
<xsl:otherwise>
<xsl:call-template name ="Date204">
<xsl:with-param name="Date204" select="messagedate"/>
<xsl:with-param name ="Element" select="name(messagedate)"/>
</xsl:call-template>
</xsl:otherwise>
--
Joe ( Microsoft MVP - XML)
|
|
 |