使用XSLT 1.0进行复杂分组

时间:2015-06-03 01:44:53

标签: xml xslt-1.0

我修改了数据文件并添加了分组列。否则,我无法找出分组的逻辑。

数据包含邮票收集的信息。

以下是XML示例:

    my $header = $self->parse_headers();
    open F,qq{>>~/MyWebServer.log};
    my $dump = Data::Dumper->Dump([$header], [qw($header)]);
    print F $dump;
    close F;

这是我放在一起的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<stamps>        
<stamp>     
<Group>25</Group>       
<Scott>3133</Scott>     
<Title>32¢ Thornton Wilder</Title>      
<Series>Literary Arts</Series>      
</stamp>        
<stamp>     
<Group>26</Group>       
<Scott>3134</Scott>     
<Title>32¢ Charlie Chaplin</Title>      
</stamp>
<stamp>     
<Group>26</Group>       
<Scott>3135</Scott>     
<Title>32¢ Raoul Wallenberg</Title>     
</stamp>
<stamp>     
<Group>27</Group>       
<Scott>3136</Scott>     
<Title>Sheet of 15</Title>      
<Issue>The World of Dinosaurs</Issue>       
</stamp>        
<stamp>     
<Group>27</Group>       
<Scott>3136</Scott>     
<Minor>a</Minor>        
<Title>32¢ Ceratosaurus</Title>     
<Issue>The World of Dinosaurs</Issue>       
</stamp>        
<stamp>     
<Group>27</Group>       
<Scott>3136</Scott>     
<Minor>b</Minor>        
<Title>32¢ Camptosaurus</Title>     
<Issue>The World of Dinosaurs</Issue>       
</stamp>        
<stamp>     
<Group>27</Group>       
<Scott>3136</Scott>     
<Minor>c</Minor>        
<Title>32¢ Camarasaurus</Title>     
<Issue>The World of Dinosaurs</Issue>       
</stamp></stamps>

以下是生成的XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>

<xsl:key name="StampGroup" match="stamp" use="Group"/>
<xsl:key name="ScottGroup" match="stamp" use="concat(Group, '|', Scott)"/>

<xsl:template match="/*">
 <xsl:copy>
  <xsl:apply-templates select="stamp[generate-id() = generate-id(key('StampGroup', Group)[1])]" mode="StampGroup" />
 </xsl:copy>
 </xsl:template>

<xsl:template match="stamp" mode="StampGroup">
<StampGroup id="{Group}">        

<xsl:apply-templates select="key('StampGroup', Group)[generate-id() = generate-id(key('ScottGroup', concat(Group, '|', Scott))[1])]" mode="ScottGroup" />
</StampGroup>
</xsl:template>

<xsl:template match="stamp" mode="ScottGroup">
 <stamp>
  <Scott><xsl:value-of select="Scott"/></Scott>
  <Title><xsl:value-of select="Title"/></Title>
  <Minor><xsl:value-of select="Minor"/></Minor> 
 </stamp>
</xsl:template>

</xsl:stylesheet>

它主要工作。它拉动了这些团体。它提取了唯一的<?xml version="1.0" encoding="utf-8"?> <stamps> <StampGroup id="25"> <stamp> <Scott>3133</Scott> <Title>32¢ Thornton Wilder</Title> <Minor/> </stamp> </StampGroup> <StampGroup id="26"> <stamp> <Scott>3134</Scott> <Title>32¢ Charlie Chaplin</Title> <Minor/> </stamp> <stamp> <Scott>3135</Scott> <Title>32¢ Raoul Wallenberg</Title> <Minor/> </stamp> <stamp> <Scott>3136</Scott> <Title>Sheet of 15</Title> <Minor/> </stamp> </StampGroup> </stamps> 项,但它没有提取<Scott>子项。

我之前从未使用过这种类型的XSLT结构。你怎么能像在for-each中那样重复项目?

第27组有4个项目。它们都具有相同的<Minor>个数字但不同的<Scott>字段。

我是否需要创建第三个密钥?

抱歉,忘了添加所需的结果:

<Minor>

2 个答案:

答案 0 :(得分:0)

你的问题很有趣。基本订单是ItemNo,其中包含更改分组标题,但是对于Series或Issue中的组,则应该有另一个分组标题而不更改ItemNo顺序。我想出了以下内容并使用了带有背景颜色的html表格来进行分组标题,以便于查看。我没有完全理解预期的输出,我不是邮票专家。该脚本应该让您了解如何解决这个问题。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="ItemNo" match="stamp" use="ItemNo" />
    <xsl:key name="Series" match="stamp" use="Series" />
    <xsl:key name="Issue" match="stamp" use="Issue" />
    <xsl:template match="/">
        <table border="1">
            <xsl:apply-templates select="stamps/stamp[generate-id(.)=generate-id(key('ItemNo',ItemNo)[1])]" mode="groupItem">
                <xsl:sort select="ItemNo"/>
            </xsl:apply-templates>
        </table>
    </xsl:template>
    <xsl:template match="stamp" mode="groupItem">
        <xsl:variable name="varItemNo" select="ItemNo"/>
        <xsl:apply-templates select="../stamp[ItemNo=$varItemNo][generate-id(.)=generate-id(key('Series',Series)[1])]" mode="groupSeries">
            <xsl:sort select="ItemNo"/>
        </xsl:apply-templates>
        <xsl:apply-templates select="../stamp[ItemNo=$varItemNo][generate-id(.)=generate-id(key('Issue',Issue)[1])]" mode="groupIssue">
            <xsl:sort select="Issue"/>
        </xsl:apply-templates>
        <tr style="background-color:yellow">
            <td><xsl:value-of select="ItemNo"/></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
        <xsl:apply-templates select="../stamp[ItemNo=$varItemNo]">
            <xsl:sort select="Series"/>
            <xsl:sort select="Issue"/>
            <xsl:sort select="Minor"/>
        </xsl:apply-templates>
    </xsl:template>
    <xsl:template match="stamp" mode="groupSeries">
        <tr style="background-color:gold">
            <td></td>
            <td></td>
            <td><xsl:value-of select="Series"/></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </xsl:template>
    <xsl:template match="stamp" mode="groupIssue">
        <tr style="background-color:tan">
            <td></td>
            <td></td>
            <td></td>
            <td></td>
            <td><xsl:value-of select="Issue"/></td>
            <td></td>
        </tr>
    </xsl:template>
    <xsl:template match="stamp">
        <tr>
            <td><xsl:value-of select="ItemNo"/></td>
            <td><xsl:value-of select="Date"/></td>
            <td><xsl:value-of select="Series"/></td>
            <td><xsl:value-of select="Name"/></td>
            <td><xsl:value-of select="Issue"/></td>
            <td><xsl:value-of select="Minor"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>

......是的,html还没有完成

答案 1 :(得分:0)

如果您希望每个Scott中只包含唯一的Group值,并且每个Minor子组中只有唯一的Scott值,那么您需要三个密钥

如果您希望子组的值对每个父组不是唯一的,则必须连接键才能将键缩小到仅匹配当前父组中的项目。

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:strip-space elements="*"/>

<xsl:key name="stamp-by-group" match="stamp" use="Group" />
<xsl:key name="stamp-by-scott" match="stamp" use="concat(Group, '|', Scott)" />
<xsl:key name="stamp-by-minor" match="stamp" use="concat(Group, '|', Scott, '|', Minor)" />

<xsl:template match="/stamps">
    <xsl:copy>
        <xsl:for-each select="stamp[count(. | key('stamp-by-group', Group)[1]) = 1]">
            <StampGroup id="{Group}">
                <xsl:for-each select="key('stamp-by-group', Group)[count(. | key('stamp-by-scott', concat(Group, '|', Scott))[1]) = 1]">
                    <xsl:apply-templates select="key('stamp-by-scott', concat(Group, '|', Scott))[count(. | key('stamp-by-minor', concat(Group, '|', Scott, '|', Minor))[1]) = 1]"/>
                </xsl:for-each>
            </StampGroup>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<xsl:template match="stamp">
    <xsl:copy>
        <xsl:copy-of select="Scott | Title"/>
        <Minor><xsl:value-of select="Minor" /></Minor>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

编辑:

如果您没有按Scott为该组创建任何内容,则可以跳过辅助分组及其相关密钥,直接转到第三级分组:

<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:strip-space elements="*"/>

<xsl:key name="stamp-by-group" match="stamp" use="Group" />
<xsl:key name="stamp-by-minor" match="stamp" use="concat(Group, '|', Scott, '|', Minor)" />

<xsl:template match="/stamps">
    <xsl:copy>
        <xsl:for-each select="stamp[count(. | key('stamp-by-group', Group)[1]) = 1]">
            <StampGroup id="{Group}">
                <xsl:apply-templates select="key('stamp-by-group', Group)[count(. | key('stamp-by-minor', concat(Group, '|', Scott, '|', Minor))[1]) = 1]"/>
            </StampGroup>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>

<xsl:template match="stamp">
    <xsl:copy>
        <xsl:copy-of select="Scott | Title"/>
        <Minor><xsl:value-of select="Minor" /></Minor>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>
相关问题