代码之家  ›  专栏  ›  技术社区  ›  jbrehr

xpath删除元素串联中的多余空格

  •  1
  • jbrehr  · 技术社区  · 6 年前

    在xpath中,我将处理如下所示的源XML,在这里我要连接每个源XML的子元素 editor 使用空格分隔符创建全名,然后依次用逗号连接生成的全名:

    <biblStruct type="book" xml:id="Biller_2011a">
         <monogr>
            <title>Inquisitors and Heretics in Thirteenth-Century Languedoc: Edition and Translation
               of Toulouse Inquisition Depositions, 1273-1282</title>
            <editor>
               <forename>Peter</forename><surname>Biller</surname>
            </editor>
            <editor>
               <forename>Caterina</forename><surname>Bruschi</surname>
            </editor>
            <editor>
               <forename>Shelagh</forename><surname>Sneddon</surname>
            </editor>
            <imprint>
               <pubPlace>
                  <settlement>Leiden</settlement>
                  <country>NL</country>
               </pubPlace>
               <publisher>Brill</publisher>
               <date type="pub_date">2011</date>
            </imprint>
         </monogr>
      </biblStruct>
    

    当前,使用xpath的xpath(在xquery中)代码如下所示 map 引入分隔符:

    let $bibref := $bib//tei:biblStruct[@xml:id="Biller_2011a"]
    return   <editors>{
                (for $auth in $bibref//tei:editor
                return normalize-space(string-join($auth//child::text()," ")))!(if (position() > 1) then ', ' else (), .) 
            }</editors>
    

    但这会在逗号前后输出额外的空间:

    <editors>Peter Biller ,  Caterina Bruschi ,  Shelagh Sneddon</editors>
    

    相反,我想输出:

    <editors>Peter Biller, Caterina Bruschi, Shelagh Sneddon</editors>  
    

    事先谢谢。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Martin Honnen    6 年前

    “我要在其中连接每个 editor “将转化为 $auth/* 而不是 $auth//child::text() .

    不知怎的整个混合 for return ! string-join 看起来很奇怪,好像你可以用 string-join($bibref//tei:editor/string-join(*, ' '), ', ') .