1

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

以下面的例子为例:

<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>

或者,用文字来解释,如果我有一个列表项,以一个或多个星号开头,后跟一个制表符跨度,然后只保留跨度之后的内容。

我一直在胡闹,但找不到满足我需求的东西,所以再次欢迎任何建议!

4

2 回答 2

0

这个怎么样:

<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>
于 2013-03-15T16:08:20.787 回答
0

当前接受的解决方案是不正确的,通常会产生不正确的结果。例如,当应用于此 XML 文件时

<ol class="ast">
  <li><a href="#">with a link.</a>*<span class="tab">Some blabla </span></li>
  <li>Something else</li>
</ol>

产生了这个不正确的结果(span并且文本被错误地删除):

<?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>

应用于提供的 XML 文档时

<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>

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

<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>
于 2013-03-16T05:38:27.487 回答