1

我有以下 XML 文件:

<layout>
 <layout-structure>
  <layout-root id="layout-root">
    <layout-chunk id="header-text">
         <layout-leaf xref="lay-1.01"/>
         <layout-leaf xref="lay-1.02"/>
    </layout-chunk>
    <layout-leaf xref="lay-1.03"/>
  </layout-root>
  <layout-root id="layout-root-two">
    <layout-chunk id="header-text-two">
     <layout-leaf xref="lay-1.04"/>
     <layout-leaf xref="lay-1.05"/>
     <layout-leaf xref="lay-1.06"/>
    </layout-chunk>
  <layout-leaf xref="lay-1.07"/>
 </layout-root>
</layout-structure>

<realization>
  <text xref="lay-1.01 lay-1.04"/>
  <text xref="lay-1.02 lay-1.05"/>
  <graphics xref="lay-1.03 lay-1.06" type="1"/>
  <graphics xref="lay-1.07" type="2"/>
</realization>
</layout>

我想提取图形元素的外部参照属性的值,以限制下面显示的函数的for子句中的输出。

declare function local:gfx($root, $graphics) {
let $graphic-xrefs := tokenize($graphics/@xref, " ")
for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs]
return concat('"', $layout-leafs/@xref, '" ', $dotgraphics, ';', $newline) 
};

但是,这会导致错误,因为图形元素下的某些外部参照属性包含单个值,例如.<graphics xref="lay-1.07"/>

是否可以使用标记化来获取图形/外部参照值,还是应该使用不同的方法?

4

2 回答 2

1

您可以尝试更改创建方式$graphic-xrefs...

declare function local:gfx($root, $graphics) {
    let $graphic-xrefs := 
        for $xref in $graphics/@xref
        return
            tokenize($xref,' ')
    for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs]
    return concat('"', $layout-leafs/@xref, '" ', $dotgraphics, ';', $newline) 
};
于 2012-09-06T02:32:18.917 回答
0

这应该不会导致问题,因为tokenize如果拆分字符串不在搜索字符串中,它将简单地返回整个字符串。

对于一般情况下的处理,正常的if (...) then ... else ...陈述可能是有用的。此外,您可能希望使用该try { ... } catch {...}构造来处理意外情况。

当我运行此代码时,您的代码实际上按预期工作:

declare variable $t :=
<layout>
 <layout-structure>
  <layout-root id="layout-root">
    <layout-chunk id="header-text">
         <layout-leaf xref="lay-1.01"/>
         <layout-leaf xref="lay-1.02"/>
    </layout-chunk>
    <layout-leaf xref="lay-1.03"/>
  </layout-root>
  <layout-root id="layout-root-two">
    <layout-chunk id="header-text-two">
     <layout-leaf xref="lay-1.04"/>
     <layout-leaf xref="lay-1.05"/>
     <layout-leaf xref="lay-1.06"/>
    </layout-chunk>
  <layout-leaf xref="lay-1.07"/>
 </layout-root>
</layout-structure>

<realization>
  <text xref="lay-1.01 lay-1.04"/>
  <text xref="lay-1.02 lay-1.05"/>
  <graphics xref="lay-1.03 lay-1.06" type="1"/>
  <graphics xref="lay-1.07" type="2"/>
</realization>
</layout>;

declare function local:gfx($root, $graphics) {
let $graphic-xrefs := tokenize($graphics/@xref, " ")
for $layout-leafs in $root//layout-leaf[@xref = $graphic-xrefs]
return $layout-leafs/@xref
};

local:gfx($t, $t/realization/graphics[2])

请注意,<layout>代码片段中的最后一个实际上应该是结束语句。

于 2012-09-04T14:24:48.660 回答