Re: [xslt] Issues using parameters with namespaces
On Tue, Mar 1, 2022 at 3:37 AM Nick Wellnhofer wrote: > On 28/02/2022 22:07, George Joseph wrote: > > Actually "//*[name()='dm:device']" works as well so maybe that's what > I'll do. > > This seems like the best work-around. > Yeah makes sense. I'll need to do some performance testing regardless of which way I go since I'm parsing SIP messages in real time. > > Is there any reason why path parameters with namespaces haven't been > > supported? > > Would a pull request to add the namespaces > in xsltNewTransformContext be > > considered? > > How parameters are passed isn't covered by the standard, so it's up to the > implementation to come up with something. It seems natural to allow XPath > expressions, but it's not clear how to handle namespaces. Simply taking > the > namespace defitinions from the root element of the root stylesheet should > do > what most people expect, but it is somewhat fragile. > > True. > 1. Passing parameters from the command line > > Supporting namespaces in a clean way would probably require a separate > channel > to register namespaces just for parameter evaluation. Even Saxon doesn't > seem > to support custom namespaces in parameter expressions: > > "The static context for this XPath expression includes only the standard > namespaces conventionally bound to the prefixes xs, fn, xsi, and saxon." > [1] > > 2. Passing parameters using the API > > The most flexible approach is probably an API to set parameters directly, > similar to Saxon's XsltTransformer [2]. This would even allow to pass > nodes > from external documents as parameters. But if you want to combine that > with > XPath expressions, you'd have to evaluate them yourself. > > Nick > > Gotcha. Thanks for all your help Nick! > > [1] > https://www.saxonica.com/documentation11/index.html#!using-xsl/commandline > [2] > > https://www.saxonica.com/documentation11/index.html#!javadoc/net.sf.saxon.s9api/XsltTransformer@setParameter > ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On 28/02/2022 22:07, George Joseph wrote: Actually "//*[name()='dm:device']" works as well so maybe that's what I'll do. This seems like the best work-around. Is there any reason why path parameters with namespaces haven't been supported? Would a pull request to add the namespaces in xsltNewTransformContext be considered? How parameters are passed isn't covered by the standard, so it's up to the implementation to come up with something. It seems natural to allow XPath expressions, but it's not clear how to handle namespaces. Simply taking the namespace defitinions from the root element of the root stylesheet should do what most people expect, but it is somewhat fragile. 1. Passing parameters from the command line Supporting namespaces in a clean way would probably require a separate channel to register namespaces just for parameter evaluation. Even Saxon doesn't seem to support custom namespaces in parameter expressions: "The static context for this XPath expression includes only the standard namespaces conventionally bound to the prefixes xs, fn, xsi, and saxon." [1] 2. Passing parameters using the API The most flexible approach is probably an API to set parameters directly, similar to Saxon's XsltTransformer [2]. This would even allow to pass nodes from external documents as parameters. But if you want to combine that with XPath expressions, you'd have to evaluate them yourself. Nick [1] https://www.saxonica.com/documentation11/index.html#!using-xsl/commandline [2] https://www.saxonica.com/documentation11/index.html#!javadoc/net.sf.saxon.s9api/XsltTransformer@setParameter ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On Mon, Feb 28, 2022 at 2:01 PM George Joseph wrote: > > > On Mon, Feb 28, 2022 at 1:04 PM Nick Wellnhofer > wrote: > >> On 28/02/2022 20:54, George Joseph via xslt wrote: >> > I think it works because xsltProcessUserParamInternal only sets >> > xpctxt->namespaces = NULL but it doesn't clear xpctxt->nsHash which is >> where >> > xmlXPathRegisterNs puts the namespaces to be registered. >> >> Right, the XPath engine uses both ->namespaces and ->nsHash. >> >> > Can you give me a hint? I've had absolutely no luck is trying to pass >> a path >> > as a string and getting it to result in something other than "The >> 'select' >> > expression >> > did not evaluate to a node set" even in an absurdly simple example. >> I'm passing >> > the path double quoted "'/dm:device'" and have tried various >> combinations of >> > "select=" and "with-param".Even Google is failing me so any help >> would be >> > appreciated. >> >> I meant that you should only use string or integer literals in parameter >> values and no other XPath expressions at all. This means to rework your >> stylesheets, of course. >> > > I think I'll stick with my solution to add the namespaces to the xpath > context. :) > Actually "//*[name()='dm:device']" works as well so maybe that's what I'll do. > Is there any reason why path parameters with namespaces haven't been > supported? > Would a pull request to add the namespaces in xsltNewTransformContext be > considered? > > > > >> >> Nick >> >> ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On Mon, Feb 28, 2022 at 1:04 PM Nick Wellnhofer wrote: > On 28/02/2022 20:54, George Joseph via xslt wrote: > > I think it works because xsltProcessUserParamInternal only sets > > xpctxt->namespaces = NULL but it doesn't clear xpctxt->nsHash which is > where > > xmlXPathRegisterNs puts the namespaces to be registered. > > Right, the XPath engine uses both ->namespaces and ->nsHash. > > > Can you give me a hint? I've had absolutely no luck is trying to pass > a path > > as a string and getting it to result in something other than "The > 'select' > > expression > > did not evaluate to a node set" even in an absurdly simple example. I'm > passing > > the path double quoted "'/dm:device'" and have tried various > combinations of > > "select=" and "with-param".Even Google is failing me so any help > would be > > appreciated. > > I meant that you should only use string or integer literals in parameter > values and no other XPath expressions at all. This means to rework your > stylesheets, of course. > I think I'll stick with my solution to add the namespaces to the xpath context. :) Is there any reason why path parameters with namespaces haven't been supported? Would a pull request to add the namespaces in xsltNewTransformContext be considered? > > Nick > > ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On 28/02/2022 20:54, George Joseph via xslt wrote: I think it works because xsltProcessUserParamInternal only sets xpctxt->namespaces = NULL but it doesn't clear xpctxt->nsHash which is where xmlXPathRegisterNs puts the namespaces to be registered. Right, the XPath engine uses both ->namespaces and ->nsHash. Can you give me a hint? I've had absolutely no luck is trying to pass a path as a string and getting it to result in something other than "The 'select' expression did not evaluate to a node set" even in an absurdly simple example. I'm passing the path double quoted "'/dm:device'" and have tried various combinations of "select=" and "with-param". Even Google is failing me so any help would be appreciated. I meant that you should only use string or integer literals in parameter values and no other XPath expressions at all. This means to rework your stylesheets, of course. Nick ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On Mon, Feb 28, 2022 at 11:42 AM Nick Wellnhofer wrote: > On 28/02/2022 18:50, George Joseph via xslt wrote: > > I've been having a heck of a time trying to pass a parameter > > to xsltApplyStylesheet > > that has namespaces in the xpath expression. > > This use case is simply not supported. > Okay. > > > Finally I did find a workaround by calling xsltNewTransformContext() > then > > iterating > > over the nsDefs in the stylesheet document and > calling xmlXPathRegisterNs on > > ctxt->xpathCtxt for each nsDef. > > I'm a bit puzzled that this worked at all. See the code here: > > > > https://gitlab.gnome.org/GNOME/libxslt/-/blob/master/libxslt/variables.c#L1581 I think it works because xsltProcessUserParamInternal only sets xpctxt->namespaces = NULL but it doesn't clear xpctxt->nsHash which is where xmlXPathRegisterNs puts the namespaces to be registered. > > > But the expression is compiled before resetting the namespaces, so I guess > that's why. > > > I would have thought this would have been done automatically though. > Did I > > miss something or is there a better way to do this? > > No, I'd recommend that you only use string and integer params. > Can you give me a hint? I've had absolutely no luck is trying to pass a path as a string and getting it to result in something other than "The 'select' expression did not evaluate to a node set" even in an absurdly simple example. I'm passing the path double quoted "'/dm:device'" and have tried various combinations of "select=" and "with-param".Even Google is failing me so any help would be appreciated. Thanks! > > Nick > ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt
Re: [xslt] Issues using parameters with namespaces
On 28/02/2022 18:50, George Joseph via xslt wrote: I've been having a heck of a time trying to pass a parameter to xsltApplyStylesheet that has namespaces in the xpath expression. This use case is simply not supported. Finally I did find a workaround by calling xsltNewTransformContext() then iterating over the nsDefs in the stylesheet document and calling xmlXPathRegisterNs on ctxt->xpathCtxt for each nsDef. I'm a bit puzzled that this worked at all. See the code here: https://gitlab.gnome.org/GNOME/libxslt/-/blob/master/libxslt/variables.c#L1581 But the expression is compiled before resetting the namespaces, so I guess that's why. I would have thought this would have been done automatically though. Did I miss something or is there a better way to do this? No, I'd recommend that you only use string and integer params. Nick ___ xslt mailing list, project page http://xmlsoft.org/XSLT/ xslt@gnome.org https://mail.gnome.org/mailman/listinfo/xslt