【问题标题】:XPath, flat hierarchy and stop conditionXPath、平面层次结构和停止条件
【发布时间】:2011-07-28 21:06:15
【问题描述】:

我需要从非常糟糕的 XML 构造 Start 对象。我为一个案例制作了 SAX 解析器,但它很乱,我想试试 XPath。

我有以下 XML:

<doc>
    <start/>
    <a/>
    <b/>
    <item/>
    <item/>
    <item/>

    <start/>
    <item/>
    <item/>
    <item/>

    <start/>
    <b/>
    <item/>
    <item/>
    <item/>

</doc>

但是我更喜欢这份文件(我没有):

<doc>
    <start>
        <a/>
        <b/>
        <item/>
        <item/>
        <item/>
    <start/>

    <start>
        <item/>
        <item/>
        <item/>
    <start/>

    <start>
        <b/>
        <item/>
        <item/>
       <item/>
    <start/>

</doc>

请假设我有第二个“开始”节点对象(来自第一个 XML 示例)。现在我想在这个节点之后直接获取“a”和“b”元素。但是,如果我从该节点(具有以下兄弟)对“b”节点进行相对查询,我将在第 3 个起始节点下获得节点。是否可以说“在该节点之后找到节点X,但在节点Y上停止(返回null)”?

我知道我可以使用“|”或多个查询,但这不是我想要的(尽管它也可能解决我的问题)。

谢谢。

【问题讨论】:

  • 另外,“直接跟随”不是指在当前节点之后,而是在下一个“开始”节点之前的某个位置。
  • 不清楚你是想要那个输出还是只是一个 XPath 选择。在前一种情况下,您应该使用 XSLT 来转换您的输入。

标签: xml xslt xpath flat


【解决方案1】:

如果您使用 XSLT 1.0,您还可以使用键 xsl:key 对相邻的兄弟姐妹进行分组,从而简化 XPath 表达式:

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>

    <xsl:key name="k_adjChild"
        match="/*/*[not(self::start)]"
        use="generate-id(preceding-sibling::start[1])"
        />

    <xsl:template match="doc">
        <doc>
            <xsl:apply-templates select="start"/>
        </doc>
    </xsl:template>

    <xsl:template match="start">
        <xsl:copy>
            <xsl:copy-of select="key('k_adjChild', generate-id())" />
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    假设上下文是一个特定的&lt;start&gt; 元素,这个XPath 将选择当前&lt;start&gt; 和后面的&lt;start&gt; 之间的所有节点。

    following-sibling::node()[not(self::start)]
                             [generate-id(preceding-sibling::start[1]) 
                               = generate-id(current())]
    

    此 XSLT 应用该 XPath 以便按 &lt;start&gt; 元素对内容进行分组。

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes" />
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="doc">
            <xsl:copy>
                <xsl:apply-templates select="@*|start" />
            </xsl:copy>
        </xsl:template>
    
        <!--for each start element, copy it,
            apply templates for it's attributes(in case any exist)
            and for nodes() that are following-siblings
            who's first preceeding-sibling is this start element-->
        <xsl:template match="start">
            <xsl:copy>
                <xsl:apply-templates select="@*
                    | following-sibling::node()[not(self::start)]
                        [generate-id(preceding-sibling::start[1]) 
                          = generate-id(current())]" />
            </xsl:copy>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • +1 请注意 generate-id() 不是 XPath 函数。您只能在转换中使用这种 XPath 表达式。
    【解决方案3】:

    假设输入的 XML 在文件 in.xml 中,这个 XQuery 脚本会执行您想要的操作:

    (:
      This library function can be found here:
      http://www.xqueryfunctions.com/xq/functx_index-of-node.html
    :)
    declare namespace functx = "http://www.functx.com"; 
    declare function functx:index-of-node($nodes as node()* ,
    $nodeToFind as node() )  as xs:integer* 
    {     
      for $seq in (1 to count($nodes))
      return $seq[$nodes[$seq] is $nodeToFind]
    };
    
    (:
      Recursively calculate the start elements with the other elements between
      as childs.
      Take the first two indices of $positions and create a start element
      with the elements of $elements with positions between these two indices.
      Then remove the first index of $position and do the recursive call.
      Input:
        $positions: Sequence with start element indices (belongs to $elements)
        $elements: Element sequence
      Output:
        Sequence of start elements with child elements
    :)
    declare function local:partition($positions as xs:integer*, 
        $elements as element()*) as element()* 
    {
      let $len := count($positions)
      return
        if($len gt 1)
        then (
          let $first := $positions[1]
          let $second := $positions[2]
          let $rest := subsequence($positions, 2)
          return
            ( element start
              {
                subsequence($elements, $first + 1, $second - $first - 1)
              },
              local:partition($rest, $elements)
            )
        ) 
        else if($len eq 1)
        then (
              element start
              {
                subsequence($elements, $positions[1] + 1)
              }
        )
        else () 
    };
    
    (: Input document :)
    let $input-doc := doc('in.xml')
    
    (: Sequence of all child elements of root element doc :)
    let $childs := $input-doc/doc/node()[. instance of element()]
    
    (: Sequence with the indices of the start elements in $childs :)
    let $positions := for $s in $input-doc/doc/start 
                      return functx:index-of-node($childs, $s)
    
    return 
      <doc>
      {
        local:partition($positions, $childs)
      }
      </doc>
    

    输出是:

    <doc>
      <start>
        <a/>
        <b/>
        <item/>
        <item/>
        <item/>
      </start>
      <start>
        <item/>
        <item/>
        <item/>
      </start>
      <start>
        <b/>
        <item/>
        <item/>
        <item/>
      </start>
    </doc>
    

    使用XQilla 进行测试,但每个其他 XQuery 处理器都应该产生相同的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2014-07-14
      • 1970-01-01
      相关资源
      最近更新 更多