Subject: Re: [xsl] how to create variable by comparing two variables using [not] From: "Wendell Piez wapiez@xxxxxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> Date: Mon, 15 Oct 2018 13:31:36 -0000 |
Eliot writes: > I also depend heavily on using messages to test my assumptions. > For example, I might do something like: > <xsl:message>+ [DEBUG] jpeg_few={$jpeg_few => string-join(', ')}</xsl:message> > <xsl:message>+ [DEBUG] jpeg_many={$jpeg_many => string-join(', ')}</xsl:message> This is a key technique when developing XSLT. The language is designed to "fail gracefully" most of the time -- which puts the burden on the programmer to ensure things don't fail catastrophically. :-) Cheers, Wendell On Sun, Oct 14, 2018 at 7:10 PM Eliot Kimber ekimber@xxxxxxxxxxxx <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > > Looking at the XPath 3 Functions and Operators specification and searching on "intersect" (hoping to also find "disjoint") I find this discussion: > > D.4.2.3 eg:value-except > eg:value-except( $arg1 as xs:anyAtomicType*, > $arg2 as xs:anyAtomicType*) as xs:anyAtomicType* > This function returns a sequence containing all the distinct items that appear in $arg1 but not in $arg2, in an arbitrary order. > > XSLT implementation > > <xsl:function name="eg:value-except" as="xs:anyAtomicType*"> > <xsl:param name="arg1" as="xs:anyAtomicType*"/> > <xsl:param name="arg2" as="xs:anyAtomicType*"/> > <xsl:sequence > select="fn:distinct-values($arg1[not(.=$arg2)])"/> > </xsl:function>Which is in https://www.w3.org/TR/xpath-functions-31/#other-functions (Appendix D). > > So basically > > distinct-values($jpeg_few[not(. = $jpeg_many)] > > Should give you the answer you seek. > > I agree with Mike that being obsessive about putting data types on all variables and function return values (and templates when the templates should return atomic types or specific element types) will help a lot. > > If your code is working without types but failing with them it means your code is "working" but probably not for the reasons you think. > > Working carefully through the stages of the expressions by setting each intermediate result into variable will help a lot. > > I also depend heavily on using messages to test my assumptions. > > For example, I might do something like: > > <xsl:message>+ [DEBUG] jpeg_few={$jpeg_few => string-join(', ')}</xsl:message> > <xsl:message>+ [DEBUG] jpeg_many={$jpeg_many => string-join(', ')}</xsl:message> > > Or if those lists are very long, use count() or get the first n items or whatever to make it clear that you're working with the values you think you are. > > Also, remember that <xsl:value-of> ({} in string result contexts) is different from <xsl:sequence>, which returns the actual value, not a string representation. > > For example, given a variable that is an attribute node, value-of will return string value of the attribute but xsl:sequence will return the attribute node and Saxon will serialize it as <attribute name="foo" value="bar"> (or something similar to that. > > It's easy to accidently create a sequence of attributes when what you wanted was a sequence of strings (or visa versa) and using xsl:value-of can obscure that mistake. > > I've also started using the XQuery-required explicating casting of values even though XSLT usually lets you get away with implicit casting, because it makes it clearer to me what my intent was (and makes it easier to copy XPath expressions into XQuery, if that's something you need to do). > > Cheers, > > Eliot > -- > Eliot Kimber > http://contrext.com > > > o;?On 10/14/18, 3:53 PM, "Dave Lang emaildavelang@xxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx> wrote: > > > That error can only come from an expression that calls tokenize(). It's therefore clearly not your declaration of jpgs_in_xml_not_directories that's at fault. > > Fair enough - but when I run the transformation without that declaration > everything works fine. Is there something I can do to the variables that > are included in it to make the declaration work? > > > > -- Wendell Piez | http://www.wendellpiez.com XML | XSLT | electronic publishing Eat Your Vegetables _____oo_________o_o___ooooo____ooooooo_^
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] About casting, Leo Studer leo.stude | Thread | Re: [xsl] how to create variable by, Dave Pawson dave.paw |
Re: [xsl] another beginner question, Terry Badger terry_b | Date | Re: [xsl] how to create variable by, Dave Pawson dave.paw |
Month |