gpt4 book ai didi

xslt - 如何在 XSLT 1.0 中匹配和包装相同和相邻的节点?

转载 作者:行者123 更新时间:2023-12-02 07:29:38 25 4
gpt4 key购买 nike

我有一个看起来像这样的 XML 文件,我试图用一个 div 包裹彼此相邻的“Para_bb”节点。

   <Para_a></Para_a>
<Para_a></Para_a>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_a></Para_a>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_a></Para_a>
<Para_a></Para_a>
<Para_bb></Para_bb>

如何让它看起来像这样?

   <p></p>
<p></p>
<div class="blackBox">
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
<Para_bb></Para_bb>
</div>
<p></p>
<div class="blackBox">
<Para_bb></Para_bb>
<Para_bb></Para_bb>
</div>
<p></p>
<p></p>
<div class="blackBox">
<Para_bb></Para_bb>
</div>

最佳答案

您可以按如下方式定义键:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:key name="group" match="Para_bb[preceding-sibling::*[1][self::Para_bb]]" use="generate-id(preceding-sibling::Para_bb[not(preceding-sibling::*[1][self::Para_bb])][1])"/>

<xsl:template match="Root">
<body>
<xsl:apply-templates/>
</body>
</xsl:template>

<xsl:template match="Para_a">
<p>
<xsl:apply-templates/>
</p>
</xsl:template>

<xsl:template match="Para_bb[preceding-sibling::*[1][self::Para_bb]]"/>

<xsl:template match="Para_bb[not(preceding-sibling::*[1][self::Para_bb])]">
<div class="blackBox">
<xsl:copy-of select=". | key('group', generate-id())"/>
</div>
</xsl:template>

</xsl:stylesheet>

转变

<Root>
<Para_a></Para_a>
<Para_a></Para_a>
<Para_bb>1</Para_bb>
<Para_bb>2</Para_bb>
<Para_bb>3</Para_bb>
<Para_bb>4</Para_bb>
<Para_a></Para_a>
<Para_bb>5</Para_bb>
<Para_bb>6</Para_bb>
<Para_a></Para_a>
<Para_a></Para_a>
<Para_bb>7</Para_bb>
</Root>

进入

<body>

<p/>

<p/>

<div class="blackBox">
<Para_bb>1</Para_bb>
<Para_bb>2</Para_bb>
<Para_bb>3</Para_bb>
<Para_bb>4</Para_bb>
</div>




<p/>

<div class="blackBox">
<Para_bb>5</Para_bb>
<Para_bb>6</Para_bb>
</div>


<p/>

<p/>

<div class="blackBox">
<Para_bb>7</Para_bb>
</div>

</body>

关于xslt - 如何在 XSLT 1.0 中匹配和包装相同和相邻的节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com