XSLT 如何用 div 包装每 3 个元素? [英] XSLT How can I wrap each 3 elements by div?

查看:29
本文介绍了XSLT 如何用 div 包装每 3 个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些带有 元素的 XML 文档,我想将它们中的每 3 个包装在

如果少于3个元素,也包裹起来.

<项目></项目><项目></项目><项目></项目><项目></项目><项目></项目><项目></项目>.....<项目></项目></商店>

所以结果一定是这样的

<项目></项目><项目></项目><项目></项目>

<div class="line"><项目></项目><项目></项目><项目></项目>

....<div class="line"><项目></项目><项目></项目>

解决方案

更简单的解决方案:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="item[position() mod 3 = 1]"><div><xsl:copy-of select=".|following-sibling::*[not(position() > 2)]"/>

</xsl:模板></xsl:stylesheet>

应用于提供的 XML 文档时:

<项目></项目><项目></项目><项目></项目><项目></项目><项目></项目><项目></项目><项目></项目></商店>

产生想要的、正确的结果:

<项目/><项目/><项目/>

<div><项目/><项目/><项目/>

<div><项目/>

更新:

OP 已阐明他需要一个转换来处理此页面上的 XML 文档.除了必须按三个分组的任何 元素组和每组包装成一个

元素之外,所有内容都应按原样复制.

代码如下:

<xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:strip-space elements="*"/><xsl:template match="node()|@*"><xsl:copy><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="node()[self::item and position() mod 3 = 1]"><div><xsl:copy-of select=".|following-sibling::*[not(position()>2)]"/>

</xsl:模板><xsl:模板匹配="node()[self::item 而不是(position() mod 3 = 1)]"/></xsl:stylesheet>

I have some XML document with <item> elements, and i'd want to wrap each 3 of them in <div> If there is less than 3 elements, wrap them too.

<shop>
  <item></item>
  <item></item>
  <item></item>
  <item></item>
  <item></item>
  <item></item>
  .....
  <item></item>
</shop>

So result must be something like this

<div class="line">
  <item></item>
  <item></item>
  <item></item>
</div>
<div class="line">
  <item></item>
  <item></item>
  <item></item>
</div>
....
<div class="line">
  <item></item>
  <item></item>
</div>

解决方案

A much simpler solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="item[position() mod 3 = 1]">
  <div>
   <xsl:copy-of select=
    ".|following-sibling::*[not(position() > 2)]"/>
  </div>
 </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<shop>
    <item></item>
    <item></item>
    <item></item>
    <item></item>
    <item></item>
    <item></item>      
    <item></item>
</shop>

produces the wanted, correct result:

<div>
   <item/>
   <item/>
   <item/>
</div>
<div>
   <item/>
   <item/>
   <item/>
</div>
<div>
   <item/>
</div>

Update:

The OP has clarified that he needs a transformation that would process the XML document on this page. Everything should be copied as-is with the exception of any group of <item> elements that must be grouped by three and each group wrapped into a <div>.

Here is the code for this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="node()[self::item and position() mod 3 = 1]">
   <div>
    <xsl:copy-of select=
    ".|following-sibling::*[not(position()>2)]"/>
   </div>
 </xsl:template>

 <xsl:template match=
  "node()[self::item and not(position() mod 3 = 1)]"/>

</xsl:stylesheet>

这篇关于XSLT 如何用 div 包装每 3 个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆