我有一个反复出现的元素,需要删除该元素并重新排列它
输入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类,并将其重新排列到另一个普通文本的位置。
发布于 2020-08-06 10:10:46
我只是编写了与您相同的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>XML用于将<?xe.source ../Temporary Files/Test_XML_1.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>https://stackoverflow.com/questions/63280026
复制相似问题