需要删除多余的普通元素

时间:2020-08-06 08:51:13

标签: xml xslt xslt-3.0

我的元素反复出现,需要删除该元素并重新排列

输入XML:

<section>
<p class="p heading">Heading</p>
<p class="normal">Text</p>
<ul>
<li><p class="p"><span class="bold">Check</span> - Remaining</p></li>
</ul>
</section>

我拥有的XSL,因为我正在将列表参数更改为普通参数:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"
    version="3.0">
    
  <xsl:param name="class-map">
   <name>
      <old>heading</old>
      <new>Headings</new>
   </name>
   <name>
      <old>normal</old>
      <new>Actual</new>
   </name>
  </xsl:param>
  
  <xsl:key name="class-map" match="name/new" use="../old"/>
  
  <xsl:template match="p/@class[key('class-map', tokenize(.), $class-map)]">
      <xsl:attribute name="style">
      <xsl:attribute name="{name()}" select="key('class-map', tokenize(.) , $class-map)"/>
      </xsl:attribute>
  </xsl:template>
  
  <xsl:template match="p">
  <xsl:copy>
    <xsl:apply-templates select="@*"/>
    <normal>
      <xsl:apply-templates/>
    </normal>
  </xsl:copy>
</xsl:template>

<xsl:template match="span[@class='bold']">
    <normal style="CD Bold">
      <xsl:apply-templates/>
    </normal>
  </xsl:template>
  
  <xsl:template match="ul">
      <xsl:apply-templates/>
  </xsl:template>
  
  <xsl:template match="li">
      <xsl:apply-templates/>
  </xsl:template>

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

</xsl:stylesheet>

预期输出:

<section>
   <p style="Headings"><normal>Heading</normal></p>
   <p style="Actual"><normal>Text</normal></p>
   <p class="p"><normal style="CD Bold">Check</normal><normal style="normal"> - Remaining</normal></p>
</section>

需要删除多余的normal元素,并使其在Bold类中变为粗体,然后将其重新排列在普通文本的另一个位置。

1 个答案:

答案 0 :(得分:1)

我做到了,只是写了与您相同的XML传入文件,因此它具有相同的输入和输出。

这是XML / HTML文件:

<?xml version="1.0" encoding="UTF-8"?>
<section>
    <p class="p heading">Heading</p>
    <p class="normal">Text</p>
    <ul>
        <li>
            <p class="p">
                <span class="bold">Check</span> - Remaining
            </p>
        </li>
    </ul>
</section>

注意: 对于HTML标签,我们不需要使用:

    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:math="http://www.w3.org/2005/xpath-functions/math"
    xmlns:map="http://www.w3.org/2005/xpath-functions/map"
    xmlns:array="http://www.w3.org/2005/xpath-functions/array"
    exclude-result-prefixes="#all"

导入某些库时应小心。如果添加过多,则代码将花费更多时间。这是因为您的代码必须首先创建对链接和.co的所有引用。就像在Java中一样,这也可能导致一些错误。

借助[@...='...'],我们可以调用诸如<normal style="CD Bold">之类的元素的特定修改,例如: normal[@style='CD Bold'

我的后援:

<?xml version="1.0" encoding="UTF-8"?><?xe.source ../TemporaryFiles/Test_XML_1.xml?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0">
    <xsl:output media-type="text/xml" method="xml"></xsl:output>
    <xsl:template match="/">
        <section>
            <p style="Headings">
                <normal>
                    <xsl:value-of select="/section/p[@class='p heading']"></xsl:value-of>
                </normal>
            </p>
            <p style="Actual">
                <normal>
                    <xsl:value-of select="/section/p[@class='normal']"></xsl:value-of>
                </normal>
            </p>
            <p class="p">
                <normal style="CD Bold">
                    <xsl:value-of select="/section/ul/li/p[@class='p']/span[@class='bold']"></xsl:value-of>
                </normal>
                <normal style="normal">
                    -<xsl:value-of select="substring-after(/section/ul/li/p[@class='p'],'-')"></xsl:value-of>
                 </normal>
            </p>
        </section>
    </xsl:template>
</xsl:stylesheet>

<?xe.source ../Temporary Files/Test_XML_1.xml?>行用于将XML文件直接添加到XSLT文件。路径应该在您的另一条路径上

我对这个简单的XSLT文件实现了期望。

这是重击:

<?xml version="1.0" encoding="UTF-8"?>
<section>
    <p style="Headings">
        <normal>Heading</normal>
    </p>
    <p style="Actual">
        <normal>Text</normal>
    </p>
    <p class="p">
        <normal style="CD Bold">Check</normal>
        <normal style="normal"> - Remaining </normal>
    </p>
</section>

如果您想在<p class="p">中用粗体显示,可以在此处使用以下代码行:

                 <normal style="CD Bold">
                    <xsl:value-of select="/section/ul/li/p[@class='p']/span[@class='bold']"></xsl:value-of> - <xsl:value-of select="substring-after(/section/ul/li/p[@class='p'],' - ')"></xsl:value-of>
                 </normal>
相关问题