XSLT对每4个相邻元素进行分组 [英] XSLT grouping every 4 adjacent elements

查看:83
本文介绍了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="http://www.w3.org/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个相邻元素进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆