开发者

xsl for function iteration introducing unwanted spaces

I'm generating an xml output using a base 'super' xml file and a reference xml file to list the parts of the super-file that I need.

The problem is the for function. When I use it to iterate over a set and conditionally output a value, it keeps outputting spaces for no match!

Here's my code

    <xsl:attribute
        name="type"
        select="
        for $index_type in $ref_indexes/@type
        return (if
        (translate($index_type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
        'abcdefghijklmnopqrstuvwxyz') = ./@type)
        then $index_type
        else '')) "/>

Where $ref_indexes/@ty开发者_如何学JAVApe could contain

 "abc def ghi MNO"

and ./@type would be a single element of

 {abc,def,ghi,jkl,mno,pqr}

The result always has spaces in the attribute, ie:

type="abc..."
type=".def.."
type="..ghi."
type="...MNO"

  • I have tried using intersect and got this:

"Required item type of first operand of 'intersect' is node(); supplied value has item type xs:string"

  • I've tried nomalize-space and got this:

"A sequence of more than one item is not allowed as the first argument of normalize-space()"

  • When I use distinct-values it gives me just one space, which is especially frustrating because it's so close!

  • I've tried checking the result for length > 1 also, still I get the spaces. I've also tried including a space in the translation.


FWIW I'm doing the transformation on OSX (10.6.7) with Java 1.5.0_26 and Saxon 9 HE.

I'm pretty much out of ideas at this stage :(

All and any help warmly appreciated,

Gary


Does

<xsl:attribute
name="type"
select="
string-join(for $index_type in $ref_indexes/@type
return (if
(translate($index_type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
'abcdefghijklmnopqrstuvwxyz') = ./@type)
then $index_type
else ''), '') "/>

do what you want? If not consider to post minimal but complete samples allowing us to reproduce the problem.

[edit] I think simply doing

<xsl:attribute
name="type"
select="$ref_indexes/@type[translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
'abcdefghijklmnopqrstuvwxyz') = current()/@type]"/>

might suffice.


I'm a little confused as to what's in your variables, but it seems to me that this

select="
        for $index_type in $ref_indexes/@type
        return (if
        (translate($index_type, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ',
        'abcdefghijklmnopqrstuvwxyz') = ./@type)
        then $index_type
        else '')) "

really just means this:

select="$ref_indexes/@type[lower-case(.)=lower-case(current()/@type)]"

Now it's true that if this selects more than one value, the values are going to be space-separated in the output. That's the way xsl:attribute works. Have you tried setting separator="" on the xsl:attribute instruction?

I'm afraid your messages about "intersect" and "normalize-space" don't reflect very well on your general approach. They give the impression that you're thrashing about picking any function that moves and giving it a try, rather than taking time to read and study.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜