需要删除额外的正常元素 [英] Need to remove the extra normal element

查看:21
本文介绍了需要删除额外的正常元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm having the element which coming repeatedly, need to remove the element and rearrange it

输入 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 I'm having, In that I'm changing list para and into normal para:

<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类设置为粗体,并在正常文本的另一个位置重新排列.

Need to remove the extra normal element and make it bold for Bold class and rearrange it on another place of normal text.

推荐答案

我只是简单地编写了与您相同的 XML 传入文件,因此它的输入和输出是相同的.

I did it simply I wrote the same XML incoming file as you had so it's the same in- and output.

这是 XML/HTML 文件:

Here is the XML/HTML File:

<?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 标签,我们不需要使用:

Notes: For HTML tags we don't need to use:

    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 中一样.

You should be careful when importing some libraries. If you've added too many, your code will take more time. This is because your code must first create all references to the links and .co. That can be cause some errors too just like in Java.

使用 [@...='...'] 我们可以调用元素的特定修改,例如 例如:正常[@style='CD Bold'

With [@...='...'] we can call specific modifications of an element like <normal style="CD Bold"> for example: 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>

行用于将 XML 文件直接添加到 XSLT 文件中.路径应该是你的另一条路径

The <?xe.source ../Temporary Files/Test_XML_1.xml?> line is used for adding the XML file directly to the XSLT File. The path should be another on yours

我用那个简单的 XSLT 文件实现了您的期望.

I achived with that simple XSLT file your expection.

结果如下:

<?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"> 中使用粗体,您可以在此处使用以下代码行:

if you want to get bouth bold in <p class="p"> you can use thise lines of code here:

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

这篇关于需要删除额外的正常元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆