用于函数迭代的 xsl 引入了不需要的空格

发布于 2024-11-17 21:04:48 字数 1307 浏览 2 评论 0原文

我使用基本“超级”xml 文件和参考 xml 文件生成 xml 输出,以列出我需要的超级文件的部分。

问题是 for 函数。当我使用它迭代一个集合并有条件地输出一个值时,它会不断输出不匹配的空格!

这是我的代码

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

,其中 $ref_indexes/@type 可以包含

 "abc def ghi MNO"

,而 ./@type 将是单个元素

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

结果在属性中总是有空格,即:

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

  • 我已经尝试过使用相交得到这个:

“‘相交’的第一个操作数所需的项目类型是node();提供的值有 项目类型 xs:string"

  • 我尝试过 nomalize-space 并得到了这个:

“不允许将多个项目的序列作为第一个参数 归一化空间()“

  • 当我使用 distinct-values 时,它只给我一个空格,这特别令人沮丧,因为它太接近了!

  • 我也尝试过检查长度 > 1 的结果,但我仍然得到了空格。我们也尝试在翻译中包含空格。


。使用 Java 1.5.0_26 和 Saxon 9 HE 在 OSX (10.6.7) 上进行转换

我在这个阶段几乎没有想法:(

热烈感谢所有的帮助,

Gary。

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/@type 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世界如花海般美丽 2024-11-24 21:04:48

<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 ''), '') "/>

做你想做的事 ?如果不考虑发布最少但完整的示例,以便我们重现问题。

[编辑]我认为简单地做

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

就足够了。

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.

墨落成白 2024-11-24 21:04:48

我对变量中的内容有点困惑,但在我看来,这

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

实际上意味着:

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

现在,如果选择多个值,这些值将在输出中以空格分隔,这是事实。这就是 xsl:attribute 的工作方式。您是否尝试过在 xsl:attribute 指令上设置分隔符=“”?

恐怕您关于“相交”和“规范化空间”的消息并不能很好地反映您的总体方法。它们给人的印象是,你正在绞尽脑汁地选择任何可行的功能并尝试一下,而不是花时间阅读和研究。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文