XQUERY:如何更新HTML文本,将行括在<span>标记中

时间:2019-10-21 14:50:42

标签: xpath xquery

我正在尝试使用有关每一行的新信息来更新HTML文本片段。这是HTML的示例:

tableView:didSelectRowAtIndexPath:

这是新信息:

<div>
  <span class="column1">
    <a id="l1"></a>aaaaa<span type="foo">aaa</span>aa
    <br id="l2"/>aaaaaaa
  </span>
  <span class="column2">
    <br id="l3"/>aaabbbb
    <br id="l4"/>bb<span>123</span>bbbbb
    <br id="l5"/>bbbbbbb
    <br id="l6"/>ccccccc
  </span>
</div>

目标是能够根据此新信息(在此例如划分为多个部分)对线进行不同样式设置。因此,最终输出应如下所示:

<sections>
    <section n="1" type="intro" from="1" to="3"/><!-- @from and @to are line numbers -->
    <section n="2" type="main" from="3" to="5"/>
    <section n="3" type="conclusion" from="6" to="6"/>
</sections>

这是到目前为止我拥有的xquery:

<div>
  <span class="column1">
    <a id="l1"/></a><span class="intro">aaaaa<span type="foo">aaa</span>aa</span>
    <br id="l2"/><span class="intro">aaaaaaa</span>
  </span>
  <span class="column2">
    <br id="l3"/><span class="intro main">aaabbbb</span>
    <br id="l4"/><span class="main">bb<span>123</span>bbbbb</span>
    <br id="l5"/><span class="main">bbbbbbb</span>
    <br id="l6"/><span class="conclusion">ccccccc</span>
  </span>
</div>

显然这不起作用!

  1. 我遇到一条线属于两个部分的问题。例如我都 for $section in $sections/section for $line in $s/@from to $s/@to let $name := $section/@type let $br := $text//*[contains(@id, concat('l', $line))] let $newline := <span class="{$name}">{$text//*[contains(@id, concat('l', $line))]/following-sibling::node()[following-sibling::*[contains(@id, concat('l', $line+1))]]}</span> return ($br, $newline) <br id="l3"/><span class="intro">...</span>
  2. 如果将行分为<br id="l3"/><span class="main">...</span>列元素(或其他级别的分组),则会丢失。

我不知道如何获得所需的输出。任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

这是我尝试将注释中链接的XSLT 3转换为XQuery 3.1:

declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

declare option output:method 'html';
declare option output:html-version '5';

declare variable $sections as document-node(element(sections)) external := document {
<sections>
    <section n="1" type="intro" from="1" to="3"/><!-- @from and @to are line numbers -->
    <section n="2" type="main" from="3" to="5"/>
    <section n="3" type="conclusion" from="6" to="6"/>
</sections>    
};

declare variable $classes-per-line as map(xs:integer, xs:string*) := map:merge(for $section in $sections/sections/section, $line in $section/@from to $section/@to return map { $line : $section/@type/string() }, map { 'duplicates' : 'combine' });

declare function local:apply-templates($nodes as node()*, $line as xs:integer) as node()* {
    $nodes ! (typeswitch(.)
      case document-node()
        return document { local:apply-templates(node(), $line) }
      case element()
        return 
            if (self::*[*/@id = 'l' || $line])
            then 
                element { node-name() } {
                    local:apply-templates(@*, $line),
                    for tumbling window $w in node()
                    start $s when $s/@id
                    return
                        if ($s/@id = 'l' || $line)
                        then ($s, <span class="{$classes-per-line($line)}">{ local:apply-templates(tail($w), $line) }</span>)
                        else local:apply-templates($w, $line)               
                }
            else
                element { node-name() } { local:apply-templates((@*, node()), $line) }
      default
        return .
    )
};

document {
<html>
    <head>
      <title>fragement transformation</title>
    </head>
    <body>
    {serialize($classes-per-line, map { 'method' : 'adaptive' })}
    {
        fold-left(sort(map:keys($classes-per-line)), ., local:apply-templates#2)
    }
    </body>
  </html>
}

https://xqueryfiddle.liberty-development.net/bFukv8v/3,我已用一个简单的local:fold-left替换了高阶fn:fold-left,该递归调用了local:apply-templates,以使代码可用于Saxon 9.8或更高版本不支持高阶函数/函数引用。