You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by Mansour <ma...@yahoo.com> on 2007/12/31 16:31:05 UTC

How to get XSLT element in the extension class

Hello:
I 've been looking for a way to get the XSLT element its self from the 
ExpressionContext object. I am trying to add some methods for some 
extensions and I have learned that these methods receive an instance of 
ExpressionContext when called, however, I couldn't find a way or an 
example showing how can I get the XSLT element that's being processed.


Thank you everyone, and happy new year.


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: How to get XSLT element in the extension class

Posted by Mansour <ma...@yahoo.com>.
Henry,
My apologies, I think I was over complicating things. I think you are 
right about this. I had some sort of confusion about the specs, and this 
discussion resolved it.
I will try again, and report the results.
Thank you.

Henry Zongaro wrote:
>
> Hi, Mansour.
>
> Mansour <ma...@yahoo.com> wrote on 2008-01-24 11:27:30 PM:
> > I really appreciate your help.
> > tokenize method is not the same, as it reads the elements from the 
> input
> > xml document.
> > I have looked at it and was able to implement the replace method in the
> > same class.
>
> [Snipped sample "replace" method.  HZ]
>
> > However, in the case of match the elements inside the element calling
> > match has to be executed.
> > In other words,  The example given with the for-each, in case the
> > regular expression given for match function was found , then the
> > for-each will kick in.
>
> I must be misunderstanding some subtle point about the definition of 
> regexp:match, because I still don't see why the definition of this 
> extension function needs to have access to the element in which it is 
> being invoked.  The definition of the function at the EXSLT web 
> site[1] says the following:
>
> <quote>
> The regexp:match function returns a node set of match elements, each 
> of whose string value is equal to a portion of the first argument 
> string that was captured by the regular expression. If the match is 
> not global, the first match element has a value equal to the portion 
> of the string matched by the entire regular expression.
> </quote>
>
> The definition of str:tokenize [2] says:
>
> <quote>
> The str:tokenize function splits up a string and returns a node set of 
> token elements, each containing one token from the string.
>
> The first argument is the string to be tokenized. The second argument 
> is a string consisting of a number of characters. Each character in 
> this string is taken as a delimiting character. The string given by 
> the first argument is split at any occurrence of any of these characters.
> </quote>
>
> In both cases, the function needs to return a node set of elements 
> whose string values are equal to substrings of the first argument 
> string.  I think that all you need to do is define two Java functions 
> for regexp:match, one of which has two String and the other of which 
> has three String arguments, to handle the optional third argument to 
> regexp:match.  The result type of those functions should be 
> org.w3c.dom.NodeList.  You should then be able to use regexp:match in 
> any context in which you could use a function that returns a node set 
> in an XPath expression, without having the implementation of the 
> function worry about the context in which it is used.
>
> If you think there's still a situation in which the implementation of 
> regexp:match needs to have access to the body of the XSLT element in 
> which it's being invoked, please let me know.  An example that 
> illustrates the difference in how you think str:tokenize and 
> regexp:match should behave would help.
>
> Thanks,
>
> Henry
> [1] http://www.exslt.org/regexp/functions/match/index.html
> [2] http://www.exslt.org/str/functions/tokenize/index.html
> ------------------------------------------------------------------
> Henry Zongaro      XSLT Processors Development
> IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: How to get XSLT element in the extension class

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Mansour.

Mansour <ma...@yahoo.com> wrote on 2008-01-24 11:27:30 PM:
> I really appreciate your help.
> tokenize method is not the same, as it reads the elements from the input 

> xml document.
> I have looked at it and was able to implement the replace method in the 
> same class.

[Snipped sample "replace" method.  HZ] 

> However, in the case of match the elements inside the element calling 
> match has to be executed.
> In other words,  The example given with the for-each, in case the 
> regular expression given for match function was found , then the 
> for-each will kick in.

I must be misunderstanding some subtle point about the definition of 
regexp:match, because I still don't see why the definition of this 
extension function needs to have access to the element in which it is 
being invoked.  The definition of the function at the EXSLT web site[1] 
says the following:

<quote>
The regexp:match function returns a node set of match elements, each of 
whose string value is equal to a portion of the first argument string that 
was captured by the regular expression. If the match is not global, the 
first match element has a value equal to the portion of the string matched 
by the entire regular expression. 
</quote>

The definition of str:tokenize [2] says:

<quote>
The str:tokenize function splits up a string and returns a node set of 
token elements, each containing one token from the string. 

The first argument is the string to be tokenized. The second argument is a 
string consisting of a number of characters. Each character in this string 
is taken as a delimiting character. The string given by the first argument 
is split at any occurrence of any of these characters. 
</quote>

In both cases, the function needs to return a node set of elements whose 
string values are equal to substrings of the first argument string.  I 
think that all you need to do is define two Java functions for 
regexp:match, one of which has two String and the other of which has three 
String arguments, to handle the optional third argument to regexp:match. 
The result type of those functions should be org.w3c.dom.NodeList.  You 
should then be able to use regexp:match in any context in which you could 
use a function that returns a node set in an XPath expression, without 
having the implementation of the function worry about the context in which 
it is used.

If you think there's still a situation in which the implementation of 
regexp:match needs to have access to the body of the XSLT element in which 
it's being invoked, please let me know.  An example that illustrates the 
difference in how you think str:tokenize and regexp:match should behave 
would help.

Thanks,

Henry
[1] http://www.exslt.org/regexp/functions/match/index.html
[2] http://www.exslt.org/str/functions/tokenize/index.html
------------------------------------------------------------------
Henry Zongaro      XSLT Processors Development
IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com


Re: How to get XSLT element in the extension class

Posted by Mansour <ma...@yahoo.com>.
Henry,
I really appreciate your help.
tokenize method is not the same, as it reads the elements from the input 
xml document.
I have looked at it and was able to implement the replace method in the 
same class.

public static NodeList replace(String str, String regex, String replacement)
    {

        NodeSet resultSet = new NodeSet();
        Document doc = DocumentHolder.m_doc;
       
        Element element = doc.createElement("replaced");
        element.appendChild(doc.createTextNode(str.replaceAll(regex, 
replacement)));
        resultSet.addNode(element);
       
        return resultSet;
    }


However, in the case of match the elements inside the element calling 
match has to be executed.
In other words,  The example given with the for-each, in case the 
regular expression given for match function was found , then the 
for-each will kick in.

Hope this makes things clear.



Henry Zongaro wrote:
>
> Hi, Mansour.
>
> Mansour <ma...@yahoo.com> wrote on 2008-01-23 04:44:07 PM:
> > Henry Zongaro wrote:
> > > Mansour <ma...@yahoo.com> wrote on 2008-01-21 10:40:13 PM:
> > > > Ok, I need to implement the ESLT function for match. Somehting like
> > > this:
> > > >
> > > >                 <xsl:for-each select="regexp:match('word')">
> > > >                         <WORD>
> > > >                                 <xsl:value-of select="." />
> > > >                         </WORD>
> > > >                 </xsl:for-each>
> > > >
> > > > I have added the method match and am able to call it 
> successfully. But
> > > > how do I retrieve the part inside the for-each?
> > > >
> > > > Is there a way to access the sheet elements from the
> > > ExpressionContext ?
> > > Your example uses an extension function.  Would it be possible for 
> you
> > > to use an extension element instead?  It would seem more natural to
> > > want to get the element and its children in that context than in the
> > > context of an extension function.
> > >
> > You are absolutely right. But in my case, where I am trying to follow
> > the specs of exslt.org, I need it to be in a function. The syntax for
> > "match" function can be found at
> > http://www.exslt.org/regexp/functions/match/index.html. If I am 
> going to
> > implement this natively then I need the xslt code to be portable,
> > therefore it has to follow the specs.
>
> I missed the fact that you're implementing the EXSLT regexp:match 
> function specifically.  It's not clear to me why that function would 
> need to have access to the content of the xsl:for-each element.  Isn't 
> it purely a function of its arguments?
>
> I suggest you take a look at the ExsltStrings.tokenize method, which 
> implements the str:tokenize function, for ideas - though I know that 
> implementation has a memory leak.  See [1] for details.
>
> I hope that helps.
>
> Thanks,
>
> Henry
> [1] http://issues.apache.org/jira/browse/XALANJ-2215
> ------------------------------------------------------------------
> Henry Zongaro      XSLT Processors Development
> IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: How to get XSLT element in the extension class

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Mansour.

Mansour <ma...@yahoo.com> wrote on 2008-01-23 04:44:07 PM:
> Henry Zongaro wrote:
> > Mansour <ma...@yahoo.com> wrote on 2008-01-21 10:40:13 PM:
> > > Ok, I need to implement the ESLT function for match. Somehting like 
> > this:
> > >
> > >                 <xsl:for-each select="regexp:match('word')">
> > >                         <WORD>
> > >                                 <xsl:value-of select="." />
> > >                         </WORD>
> > >                 </xsl:for-each>
> > >
> > > I have added the method match and am able to call it successfully. 
But
> > > how do I retrieve the part inside the for-each?
> > >
> > > Is there a way to access the sheet elements from the 
> > ExpressionContext ?
> > Your example uses an extension function.  Would it be possible for you 

> > to use an extension element instead?  It would seem more natural to 
> > want to get the element and its children in that context than in the 
> > context of an extension function.
> >
> You are absolutely right. But in my case, where I am trying to follow 
> the specs of exslt.org, I need it to be in a function. The syntax for 
> "match" function can be found at 
> http://www.exslt.org/regexp/functions/match/index.html. If I am going to 

> implement this natively then I need the xslt code to be portable, 
> therefore it has to follow the specs.

I missed the fact that you're implementing the EXSLT regexp:match function 
specifically.  It's not clear to me why that function would need to have 
access to the content of the xsl:for-each element.  Isn't it purely a 
function of its arguments?

I suggest you take a look at the ExsltStrings.tokenize method, which 
implements the str:tokenize function, for ideas - though I know that 
implementation has a memory leak.  See [1] for details.

I hope that helps.

Thanks,

Henry
[1] http://issues.apache.org/jira/browse/XALANJ-2215
------------------------------------------------------------------
Henry Zongaro      XSLT Processors Development
IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com


Re: How to get XSLT element in the extension class

Posted by Mansour <ma...@yahoo.com>.
Henry Zongaro wrote:
>
> Hi, Mansour.
>
> Mansour <ma...@yahoo.com> wrote on 2008-01-21 10:40:13 PM:
> > Mansour wrote:
> > > Hello:
> > > I 've been looking for a way to get the XSLT element its self from 
> the
> > > ExpressionContext object. I am trying to add some methods for some
> > > extensions and I have learned that these methods receive an instance
> > > of ExpressionContext when called, however, I couldn't find a way 
> or an
> > > example showing how can I get the XSLT element that's being processed.
> > Either the question is not very clear or I did not wait enough :P
> > Ok, I need to implement the ESLT function for match. Somehting like 
> this:
> >
> >                 <xsl:for-each select="regexp:match('word')">
> >                         <WORD>
> >                                 <xsl:value-of select="." />
> >                         </WORD>
> >                 </xsl:for-each>
> >
> > I have added the method match and am able to call it successfully. But
> > how do I retrieve the part inside the for-each?
> >
> > Is there a way to access the sheet elements from the 
> ExpressionContext ?
>
> My apologies.  I saw your question, but then responding slipped my mind.
>
> Your example uses an extension function.  Would it be possible for you 
> to use an extension element instead?  It would seem more natural to 
> want to get the element and its children in that context than in the 
> context of an extension function.
>
You are absolutely right. But in my case, where I am trying to follow 
the specs of exslt.org, I need it to be in a function. The syntax for 
"match" function can be found at 
http://www.exslt.org/regexp/functions/match/index.html. If I am going to 
implement this natively then I need the xslt code to be portable, 
therefore it has to follow the specs.

I might be wrong. Please give me an advice.

> John Gentilin has posted some utility functions [1,2] that help with 
> accessing and evaluating the children and attributes of an extension 
> element.
>
> I hope that helps.
>
> Thanks,
>
> Henry
> [1] 
> http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200709.mbox/%3c46E1C735.2080504@eyecatching.com%3e 
>
> [2] 
> http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200709.mbox/%3c46E1C57F.5020403@eyecatching.com%3e
> ------------------------------------------------------------------
> Henry Zongaro      XSLT Processors Development
> IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
> mailto:zongaro@ca.ibm.com


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org


Re: How to get XSLT element in the extension class

Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Mansour.

Mansour <ma...@yahoo.com> wrote on 2008-01-21 10:40:13 PM:
> Mansour wrote:
> > Hello:
> > I 've been looking for a way to get the XSLT element its self from the 

> > ExpressionContext object. I am trying to add some methods for some 
> > extensions and I have learned that these methods receive an instance 
> > of ExpressionContext when called, however, I couldn't find a way or an 

> > example showing how can I get the XSLT element that's being processed.
> Either the question is not very clear or I did not wait enough :P
> Ok, I need to implement the ESLT function for match. Somehting like 
this:
> 
>                 <xsl:for-each select="regexp:match('word')">
>                         <WORD>
>                                 <xsl:value-of select="." />
>                         </WORD>
>                 </xsl:for-each>
> 
> I have added the method match and am able to call it successfully. But 
> how do I retrieve the part inside the for-each?
> 
> Is there a way to access the sheet elements from the ExpressionContext ?

My apologies.  I saw your question, but then responding slipped my mind.

Your example uses an extension function.  Would it be possible for you to 
use an extension element instead?  It would seem more natural to want to 
get the element and its children in that context than in the context of an 
extension function.

John Gentilin has posted some utility functions [1,2] that help with 
accessing and evaluating the children and attributes of an extension 
element.

I hope that helps.

Thanks,

Henry
[1] 
http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200709.mbox/%3c46E1C735.2080504@eyecatching.com%3e
[2] 
http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200709.mbox/%3c46E1C57F.5020403@eyecatching.com%3e
------------------------------------------------------------------
Henry Zongaro      XSLT Processors Development
IBM SWS Toronto Lab   T/L 313-6044;  Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com

Re: How to get XSLT element in the extension class

Posted by Mansour <ma...@yahoo.com>.
Either the question is not very clear or I did not wait enough :P
Ok, I need to implement the ESLT function for match. Somehting like this:

                <xsl:for-each select="regexp:match('word')">
                        <WORD>
                                <xsl:value-of select="." />
                        </WORD>
                </xsl:for-each>

I have added the method match and am able to call it successfully. But 
how do I retrieve the part inside the for-each?

Is there a way to access the sheet elements from the ExpressionContext ?
Thank you in advance

Mansour wrote:
> Hello:
> I 've been looking for a way to get the XSLT element its self from the 
> ExpressionContext object. I am trying to add some methods for some 
> extensions and I have learned that these methods receive an instance 
> of ExpressionContext when called, however, I couldn't find a way or an 
> example showing how can I get the XSLT element that's being processed.
>
>
> Thank you everyone, and happy new year.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xalan-dev-help@xml.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-dev-help@xml.apache.org