XSLT对每4个相邻元素进行分组

编程入门 行业动态 更新时间:2024-10-28 13:14:13
本文介绍了XSLT对每4个相邻元素进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个如下所示的xml.

I have a xml like below.

源XML:

<?xml version="1.0" encoding="Windows-1252"?> <XML> <Attributes> <Attribute> <Name>Collection</Name> <Value /> </Attribute> <Attribute> <Name>A</Name> <Value>Testing</Value> </Attribute> <Attribute> <Name>B</Name> <Value>Blank</Value> </Attribute> <Attribute> <Name>C</Name> <Value>11</Value> </Attribute> <Attribute> <Name>D</Name> <Value>NA</Value> </Attribute> <Attribute> <Name>A</Name> <Value>Testing1</Value> </Attribute> <Attribute> <Name>B</Name> <Value>Red</Value> </Attribute> <Attribute> <Name>C</Name> <Value>12</Value> </Attribute> <Attribute> <Name>D</Name> <Value>NAT</Value> </Attribute> </Attributes> </XML>

从上面的xml中,我该怎么做.我只想将它们分为4组.因此,在源xml中的前4个元素中,/attribute/Name ='A'-/attribute/Name ='D'将在第一组中.接下来的4个,其中/attribute/Name ='A'-/attribute/Name ='D'将在第二个组中....如下所示

From the above xml how I can I do this. I only want to group them in groups of 4. So first 4 (in the source xml) element where /attribute/Name='A' - /attribute/Name='D' will be in the first group. and next 4 where /attribute/Name='A' - /attribute/Name='D' will be in the second group .... like below

预先感谢

输出

<Collection name="Collection" > <ComplexAttr> <Attr name="A" value="Testing" /> <Attr name="B" value="Blank" /> <Attr name="C" value="11" /> <Attr name="D" value="NA" /> </ComplexAttr> <ComplexAttr > <Attr name="A" value="Testing1" /> <Attr name="B" value="Red" /> <Attr name="C" value="12" /> <Attr name="D" value="NA" /> </ComplexAttr> </Collection>

推荐答案

这是常见问题解答.请参阅我对您问题的评论中的链接.这是根据您的输入和所需输出量身定制的样式表:

This is a FAQ. See my link in the comments to your question. Here is a stylesheet tailored to your input and desired output:

<xsl:stylesheet version="1.0" xmlns:xsl="www.w3/1999/XSL/Transform"> <!-- the number of items to include in each group --> <xsl:variable name="group" select="4" /> <xsl:template match="/"> <Collection name="Collection"> <xsl:apply-templates select="XML/Attributes/Attribute[not(Name='Collection')] [position() mod $group = 1]" /> </Collection> </xsl:template> <xsl:template match="Attribute" mode="inner"> <Attr name="{Name}" value="{Value}" /> </xsl:template> <xsl:template match="Attribute"> <ComplexAttr> <xsl:apply-templates select=".|following-sibling::Attribute[position() &lt; $group]" mode="inner" /> </ComplexAttr> </xsl:template> </xsl:stylesheet>

产生:

<Collection name="Collection"> <ComplexAttr> <Attr name="A" value="Testing" /> <Attr name="B" value="Blank" /> <Attr name="C" value="11" /> <Attr name="D" value="NA" /> </ComplexAttr> <ComplexAttr> <Attr name="A" value="Testing1" /> <Attr name="B" value="Red" /> <Attr name="C" value="12" /> <Attr name="D" value="NAT" /> </ComplexAttr> </Collection>

更多推荐

XSLT对每4个相邻元素进行分组

本文发布于:2023-11-29 22:18:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647792.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   XSLT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!