Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
xslt2从节点中删除指定的部分_Xslt_Xslt 1.0_Xslt 2.0 - Fatal编程技术网

xslt2从节点中删除指定的部分

xslt2从节点中删除指定的部分,xslt,xslt-1.0,xslt-2.0,Xslt,Xslt 1.0,Xslt 2.0,我想从html中的一些列表项中删除不需要的内容。基本上,我想用class选项卡剥离给定范围之前的所有内容,但前提是该范围之前的内容符合某些条件 以下面的例子为例: <ol class="ast"> <li>*<span class="tab"><!--tab--></span>Some blabla <img href="#">with a link.</a></li> <li>*

我想从html中的一些列表项中删除不需要的内容。基本上,我想用class选项卡剥离给定范围之前的所有内容,但前提是该范围之前的内容符合某些条件

以下面的例子为例:

<ol class="ast">
  <li>*<span class="tab"><!--tab--></span>Some blabla <img href="#">with a link.</a></li>
  <li>**<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
</ol>
我想得到的是:

<ol class="ast">
  <li>Some blabla <img href="#">with a link.</a></li>
  <li>Some other blabla, this one without other elements</li>
</ol>
或者,用文字解释一下,如果我有一个列表项,以一个或多个星号开头,后跟一个选项卡范围,那么只保留范围后的内容

我一直在胡闹,但没能找到满足我需要的东西,所以欢迎再次提出任何建议

这是怎么回事:

<xsl:stylesheet 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
    exclude-result-prefixes="xs">

    <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
    </xsl:template>
    <xsl:template match="li/node()[1]
                                  [self::text() and 
                                   matches(., '^\*+$') and
                                   following-sibling::node()[1]
                                            [self::span and @class = 'tab']
                                   ]" />
    <xsl:template match="li/node()[2]
                                  [self::span and @class = 'tab']
                                  [matches(preceding-sibling::text(), '^\*+$')]" />

</xsl:stylesheet>
在该输入上运行时:

<ol class="ast">
  <li>*<span class="tab"><!--tab--></span>Some blabla <a href="#">with a link.</a></li>
  <li>Not asterisks!<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>**<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>***<span>hello</span>Some other blabla, this one without other elements</li>
  <li><a href="#">with a link.</a>*<span class="tab">Some blabla </span></li>
</ol>
结果是:

<ol class="ast">
  <li>Some blabla <a href="#">with a link.</a></li>
  <li>Not asterisks!<span class="tab"/>Some other blabla, this one without other elements</li>
  <li>Some other blabla, this one without other elements</li>
  <li>***<span>hello</span>Some other blabla, this one without other elements</li>
  <li><a href="#">with a link.</a>*<span class="tab">Some blabla </span></li>
</ol>
当前接受的解决方案不正确,通常会产生不正确的结果。例如,应用于此XML文件时:

以下是一个正确的解决方案:

应用于提供的XML文档时:

此转换生成所需的正确结果:

当应用于上面的第一个XML文档时:

再次产生正确的结果:


使用此输入,但从散文问题陈述中,我会使用以下同级::*[1][self::span],而不是使用以下同级::span,同样地,对于前面的同级。然而,我可能误解了这些要求——我经常这样做。我知道在某些情况下,解决方案可能不起作用,但在我的文档结构中,这是可以的,尽管还有改进的余地。只是说否决投票可能有点困难,因为在本质上,解决方案对于给定的情况是正确的problem@MichaelKay谢谢你的建议。我同意你关于以下兄弟姐妹的看法:*[1],并相应地调整了我的答案。对于前面的同级,我们假设正在检查的两个节点是li的第一个和第二个同级,因此这里不需要[1],但我确实需要在XPath的前面添加一个节点[2]。
<ol class="ast">
  <li><a href="#">with a link.</a>*<span class="tab">Some blabla </span></li>
  <li>Something else</li>
</ol>
<?xml version="1.0" encoding="UTF-8"?><ol class="ast">
  <li><a href="#">with a link.</a></li>
  <li>Something else</li>
</ol>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
  "li/node()[1]
     [self::text() and not(translate(.,'*',''))
     and following-sibling::node()[self::span[@class='tab']]
     ]"/>

 <xsl:template match=
  "li/node()[2]
      [self::span[@class='tab']
     and preceding-sibling::node()[1]
             [self::text() and not(translate(.,'*',''))]
      ]
 "/>
</xsl:stylesheet>
<ol class="ast">
  <li>*<span class="tab"><!--tab--></span>Some blabla <a href="#">with a link.</a></li>
  <li>Not asterisks!<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>**<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>***<span>hello</span>Some other blabla, this one without other elements</li>
</ol>
<ol class="ast">
  <li>*<span class="tab"><!--tab--></span>Some blabla <a href="#">with a link.</a></li>
  <li>Not asterisks!<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>**<span class="tab"><!--tab--></span>Some other blabla, this one without other elements</li>
  <li>***<span>hello</span>Some other blabla, this one without other elements</li>
</ol>
<ol class="ast">
    <li><a href="#">with a link.</a>*<span class="tab">Some blabla </span>
    </li>
    <li>Something else</li>
</ol>
<ol class="ast">
   <li>
      <a href="#">with a link.</a>*<span class="tab">Some blabla </span>
   </li>
   <li>Something else</li>
</ol>