Need to remove the extra normal element

我们两清 提交于 2020-08-10 17:41:46

问题


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

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

Expected output:

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

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


回答1:


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

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>

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"

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.

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

My resault:

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

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

I achived with that simple XSLT file your expection.

Here is the resault:

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

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>


来源:https://stackoverflow.com/questions/63280026/need-to-remove-the-extra-normal-element

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!