我想转换以下HTML代码
<div id="pg-1">
<div id="pgc-1">
<div class="test">Hello World!</div>
</div>
<div id="pgc-2">
<div class="test">Hello World!</div>
</div>
</div>
<div id="pg-2">
<div id="pgc-3">
<div class="test">Hello World!</div>
</div>
<div id="pgc-4">
<div class="test">Hello World!</div>
</div>
</div>进入到这个
<section id="pg-1">
<div class="container">
<div class="row">
<div id="pgc-1">
<div class="test">Hello World!</div>
</div>
<div id="pgc-2">
<div class="test">Hello World!</div>
</div>
</div>
</div>
</section>
<section id="pg-2">
<div class="container">
<div class="row">
<div id="pgc-3">
<div class="test">Hello World!</div>
</div>
<div id="pgc-4">
<div class="test">Hello World!</div>
</div>
</div>
</div>
</section>我能够让这个XSLT文档正常工作:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="//div[starts-with(@id, 'pgc-')]">
<div class="container">
<div class="row">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</div>
</div>
</xsl:template>
<xsl:template match="//div[starts-with(@id, 'pg-')]">
<section>
<xsl:apply-templates select="@*|node()" />
</section>
</xsl:template>
</xsl:stylesheet>不幸的是,它并没有产生我想要的结果:特别是,它确实在容器中用id pgc包装元素,但是它并没有将它们分组为一个父元素。这就是现在输出的内容(对于每个部分):
<section id="pg-1">
<div class="container">
<div class="row">
<div id="pgc-1">
<div class="test">Hello World!</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div id="pgc-2">
<div class="test">Hello World!</div>
</div>
</div>
</div>
</section>作为我想要做的工作的总结:
id属性将所有元素的标记名从pg-更改为section (我应该将所有属性复制到新元素)id属性的div在同一个父节点.container > .row下以pgc-开头的子元素(我应该将所有属性复制到新元素中)发布于 2017-05-15 08:41:34
我猜*你想做这样的事:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="div[starts-with(@id, 'pg-')]">
<section>
<xsl:copy-of select="@*"/>
<div class="container">
<div class="row">
<xsl:apply-templates />
</div>
</div>
</section>
</xsl:template>
</xsl:stylesheet>--
(*)只有一个(有缺陷的)例子,而且没有规定的规则,人们所能做的就是猜测。
https://stackoverflow.com/questions/43974594
复制相似问题