选择以前的堂兄弟姐妹(包括兄弟姐妹)

问题描述 投票:0回答:1
xslt-2.0 xslt-3.0 xpath-2.0 xpath-3.0 xpath-3.1
1个回答
0
投票

我想不出一个简单的表达方式,我想知道下面使用

path
xsl:evaluate
的代码是否实现了要求:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="3.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="#all"
  xmlns:mf="http://example.com/mf"
  expand-text="yes">
  
  <xsl:function name="mf:cousins" as="node()*">
    <xsl:param name="node" as="node()"/>
    <xsl:variable name="path" select="path($node)"/>
    <xsl:variable name="path-without-predicates" select="replace($path, '\[[0-9]+\]', '')"/>
    <xsl:variable name="cousins" as="node()*">
      <xsl:evaluate context-item="$node" xpath="$path-without-predicates"/>
    </xsl:variable>
    <xsl:sequence select="$cousins except $node"/>
  </xsl:function>
  
  <xsl:function name="mf:preceding-cousins" as="node()*">
    <xsl:param name="node" as="node()"/>
    <xsl:sequence select="mf:cousins($node)[. &lt;&lt; $node]"/>
  </xsl:function>
  
  <xsl:template match="level2[@id = 6] | level2[@id = 5]">
    <xsl:comment>preceding cousin: {mf:preceding-cousins(.)[last()]=>serialize(map{ 'method' : 'xml' })}</xsl:comment>
    <xsl:next-match/>
  </xsl:template>

  <xsl:output method="xml" indent="no"/>

  <xsl:mode on-no-match="shallow-copy"/>

</xsl:stylesheet>

使用

path
函数,然后进行字符串替换和
xsl:evaluate
看起来有点像“黑客”,但我没有看到简单、直接的 XPath。

© www.soinside.com 2019 - 2024. All rights reserved.