XSLT 选择第二次出现的属性 [英] XSLT select the second occurrence of an attribute

查看:27
本文介绍了XSLT 选择第二次出现的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种动态的方法来获取属性的第二次出现?

Is there a dynamic way to get the 2nd occurrence of an attribute ?

对于这个 XML 示例,我想将 SummaryCell/@Name 值的前 12 个值放入变量 Month01、Month02 等中;

For this XML example, I'd like to get the first 12 values of the SummaryCell/@Name value into a variable Month01, Month02, and so on;

<SummaryHeader>
      <SummaryColumnGroup Name="2014">
        <SummaryCell Name="Feb14" Type="Text" Value="Feb"/>
        <SummaryCell Name="Jan14" Type="Text" Value="Jan"/>
      </SummaryColumnGroup>
      <SummaryColumnGroup Name="2013">
        <SummaryCell Name="Dec13" Type="Text" Value="Dec"/>
        <SummaryCell Name="Nov13" Type="Text" Value="Nov"/>
        <SummaryCell Name="Oct13" Type="Text" Value="Oct"/>
        <SummaryCell Name="Sep13" Type="Text" Value="Sep"/>
        <SummaryCell Name="Aug13" Type="Text" Value="Aug"/>
        <SummaryCell Name="Jul13" Type="Text" Value="Jul"/>
        <SummaryCell Name="Jun13" Type="Text" Value="Jun"/>
        <SummaryCell Name="May13" Type="Text" Value="May"/>
        <SummaryCell Name="Apr13" Type="Text" Value="Apr"/>
        <SummaryCell Name="Mar13" Type="Text" Value="Mar"/>
        <SummaryCell Name="Feb13" Type="Text" Value="Feb"/>
        <SummaryCell Name="Jan13" Type="Text" Value="Jan"/>
      </SummaryColumnGroup>

以下是我用过的XSL;

The following is the XSL I have used;

<xsl:variable name="Month01" select="//SummaryHeader/SummaryColumnGroup/SummaryCell[1]/@Name"/>
    <xsl:variable name="Month02" select="//SummaryHeader/SummaryColumnGroup/SummaryCell[2]/@Name"/>
    <xsl:variable name="Month03" select="//SummaryHeader/SummaryColumnGroup/SummaryCell[3]/@Name"/>

期望的输出:

<Month01>Feb14</Month01>
<Month02>Jan14</Month02>
<Month03>Dec13</Month03>

实际输出:

<Month01>Feb14</Month01>
<Month02>Jan14</Month02>
<Month03>Oct13</Month03>

发生的情况是,SummaryCell[3] 实际上选取了以下 SummaryColumnGroup 的第 3 个 SummaryCell,而不是迭代到第 3 个出现的 SummaryCell.

What happens is, SummaryCell[3] actually picks up the the 3rd SummaryCell of the following SummaryColumnGroup, and not iterates through to the 3rd occurrences of SummaryCell.

注意:SummaryColumnGroup 内的SummaryCell 数量是动态的,具体与上面的例子不同.

Note: The number of SummaryCell within a SummaryColumnGroup is dynamic, not specifically the same as the above example.

推荐答案

我想获取 SummaryCell/@Name 值的前 12 个值

I'd like to get the first 12 values of the SummaryCell/@Name value

有一个更好的方法来做到这一点.以下样式表:

There is a much better way to do that. The following stylesheet:

XSLT 1.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:template match="/">
    <root>
        <xsl:for-each select="SummaryHeader/SummaryColumnGroup/SummaryCell[count(preceding::SummaryCell) &lt; 12 ]">
            <Month num="{position()}"><xsl:value-of select="@Name"/></Month>
        </xsl:for-each>
    </root>
</xsl:template>

</xsl:stylesheet>

当应用于(更正的)输入:

<SummaryHeader>
    <SummaryColumnGroup Name="2014">
        <SummaryCell Name="Feb14" Type="Text" Value="Feb"/>
        <SummaryCell Name="Jan14" Type="Text" Value="Jan"/>
    </SummaryColumnGroup>
    <SummaryColumnGroup Name="2013">
        <SummaryCell Name="Dec13" Type="Text" Value="Dec"/>
        <SummaryCell Name="Nov13" Type="Text" Value="Nov"/>
        <SummaryCell Name="Oct13" Type="Text" Value="Oct"/>
        <SummaryCell Name="Sep13" Type="Text" Value="Sep"/>
        <SummaryCell Name="Aug13" Type="Text" Value="Aug"/>
        <SummaryCell Name="Jul13" Type="Text" Value="Jul"/>
        <SummaryCell Name="Jun13" Type="Text" Value="Jun"/>
        <SummaryCell Name="May13" Type="Text" Value="May"/>
        <SummaryCell Name="Apr13" Type="Text" Value="Apr"/>
        <SummaryCell Name="Mar13" Type="Text" Value="Mar"/>
        <SummaryCell Name="Feb13" Type="Text" Value="Feb"/>
        <SummaryCell Name="Jan13" Type="Text" Value="Jan"/>
    </SummaryColumnGroup>
</SummaryHeader>

将返回以下结果:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <Month num="1">Feb14</Month>
   <Month num="2">Jan14</Month>
   <Month num="3">Dec13</Month>
   <Month num="4">Nov13</Month>
   <Month num="5">Oct13</Month>
   <Month num="6">Sep13</Month>
   <Month num="7">Aug13</Month>
   <Month num="8">Jul13</Month>
   <Month num="9">Jun13</Month>
   <Month num="10">May13</Month>
   <Month num="11">Apr13</Month>
   <Month num="12">Mar13</Month>
</root>

我认为对 元素进行编号不是一个好方法,但是您当然可以通过使用 从位置计算名称.

I don't think having numbered <MonthXX> elements is a good way to go, but of course you could do that by using <xsl:element> to calculate the name from position.

这篇关于XSLT 选择第二次出现的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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