xslt2从节点中删除特定部分

时间:2013-03-15 15:45:30

标签: xslt xslt-1.0 xslt-2.0

我想从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>

或者,用文字解释,如果我有一个列表项,从一个或多个星号开始,后跟一个标签范围,那么只保留跨度后的内容。

我一直在愚弄但是找不到满足我需求的东西,所以欢迎任何建议!

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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>