XSLT select the second occurrence of an attribute

瘦欲@ 提交于 2021-02-11 14:34:18

问题


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

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>

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"/>

Desired Output:

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

Actual Output:

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

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.

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


回答1:


In achieving the output, try something like:

<xsl:variable name="Month03" select="//SummaryHeader/descendant::SummaryCell[3]/@Name"/>

do this also for the rest.




回答2:


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>

when applied to a (corrected) input of:

<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>

will return the following result:

<?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.



来源:https://stackoverflow.com/questions/25279953/xslt-select-the-second-occurrence-of-an-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!