0

我需要帮助才能获得准确的输出。我在 Sean B. Durkin 为以下输入 xml 提供的代码的帮助下处理了一个 xml。我更改了代码以获得所需的输出,但在获得准确输出时遇到问题。帮助我在哪里出错并在代码中。当我调试时,我可以在“game_no”123 和“group”1 中找到“indoor”的值无法识别

输入 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<t>
<Games>
    <Game_no>123</Game_no>
    <Group>1</Group>        
    <Name>game.outdoor</Name>   
    <Value>Golf</Value>     
</Games>            
<Games>     
    <Game_no>123</Game_no>  
    <Group>1</Group>    
    <Name>game.indoor</Name>    
    <Value>Chess</Value>        
</Games>        
<Games>         
    <Game_no>223</Game_no>      
    <Group>1</Group>            
    <Name>games.outdoor</Name>  
    <Value>Soccer</Value>   
</Games>        
<Games>     
    <Game_no>223</Game_no>  
    <Group>2</Group>            
    <Name>games.indoor</Name>       
    <Value>Golf</Value> 
</Games>        
<Games>     
    <Game_no>223</Game_no>      
    <Group>1</Group>        
    <Name>games.outdoor</Name>  
    <Value>Batminton</Value>    
</Games>            
<Games>     
    <Game_no>123</Game_no>      
    <Group>1</Group>        
    <Name>ga.outdoor</Name> 
    <Value>tennis</Value>   
</Games>
<Games>         
    <Game_no>123</Game_no>  
    <Group>1</Group>        
    <Name>today</Name>  
    <Value>value returning with no'.'</Value>   
</Games>        
<Games> 
    <Game_no>123</Game_no>      
    <Group>1</Group>
    <Name>indoor</Name>     
    <Value>value returning with same Group 123 and game_no 1</Value>    
</Games>    
<Games> 
    <Game_no>123</Game_no>          
    <Group>2</Group>            
    <Name>outdoor</Name>        
    <Value>value returning with different Group 2</Value>       
</Games> 
<Games> 
    <Game_no>323</Game_no>          
    <Group>2</Group>            
    <Name>outdoor</Name>        
    <Value>value returning with different Game_no 323</Value>       
</Games> 
</t>    

上面的输入 XML 有一个元素“Games”,如果我有相同的值“Game_no”和“Group”用于后续节点,那么我可以在“.”时为元素“Name”生成所需的输出。在那儿。但是,如果我在“Game_no”和“Group”的每个节点都有相同的值,我无法在没有“.”的情况下实现输出。在元素“名称”处。我将放置我想要的输出和我得到的输出。连同代码。 需要帮助,为什么我不能做到这一点。

我根据预期的输出修改了 XSL 文件代码:

<xsl:stylesheet version="1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:key name="gamekey" match="Games" use="Game_no"/>
<xsl:key name="game-group" match="Games" use="concat(substring-before(Name,'.'),'|',Game_no,'|',Group)"/>
<xsl:template match="/">
    <Holiday>
            <xsl:for-each select="//t/Games[generate-id(.) = generate-id(key('gamekey', Game_no)[1])] ">
                <Weekend>
                    <xsl:variable name="var1" select="Game_no"/>
                    <xsl:apply-templates select="//t/Games[generate-id(.) = generate-id( key('game-group',        concat( substring-before(Name ,'.'),'|',$var1,'|',Group))[1])] [substring-before(Name ,'.')or not(Name/*)] "/>
                    <xsl:apply-templates select="key('game-group','||')/self::Games"/>
                </Weekend>
            </xsl:for-each>
    </Holiday>
</xsl:template>
<xsl:template match="Games [ substring-before(Name,'.') != '']">
    <xsl:element name="{substring-before(Name,'.')}">
        <xsl:for-each select="key('game-group', concat( substring-before(Name,'.'),'|',Game_no,'|',Group))">
            <xsl:element name="{substring-after(Name,'.')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>
<xsl:template match="Games [ substring-before(Name,'.') = '']">
    <xsl:element name="{Name}">
        <xsl:value-of select="Value"/>
    </xsl:element>
</xsl:template>

我得到的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Holiday>
<Weekend>
    <game>
        <outdoor>Golf</outdoor>
        <indoor>Chess</indoor>
    </game>
    <ga>
        <outdoor>tennis</outdoor>
    </ga>
    <today>value returning with no'.'</today>
    <outdoor>value returning with different Group 2</outdoor>
</Weekend>
<Weekend>
    <games>
        <outdoor>Soccer</outdoor>
        <outdoor>Batminton</outdoor>
    </games>
    <games>
        <indoor>Golf</indoor>
    </games>
</Weekend>
<Weekend>
    <outdoor>value returning with different Game_no 323</outdoor>
</Weekend>
</Holiday>

所需输出:

<?xml version="1.0" encoding="UTF-8"?>
<Holiday>
<Weekend>
    <game>
        <outdoor>Golf</outdoor>
        <indoor>Chess</indoor>
    </game>
    <ga>
        <outdoor>tennis</outdoor>
    </ga>
    <today>value returning with no'.'</today>
    <indoor>value returning with same Group 123 and game_no 1</indoor>
    <outdoor>value returning with different Group 2</outdoor>
</Weekend>
<Weekend>
    <games>
        <outdoor>Soccer</outdoor>
        <outdoor>Batminton</outdoor>
    </games>
    <games>
        <indoor>Golf</indoor>
    </games>
</Weekend>
<Weekend>
    <outdoor>value returning with different Game_no 323</outdoor>
</Weekend>
</Holiday>

注意:问题看起来很相似,但我是一个尝试各种方法来修改内部循环的学习者。帮助表示赞赏。如果我能知道为什么其中一个节点正在淘汰,我很高兴。

4

1 回答 1

0

这是我提出的问题的更新。

现在我可以获得所需的输出。当循环到没有“。” 在“Game_no”和“Group”中具有相同的值。这是代码。

<xsl:stylesheet version="1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
<xsl:key name="gamekey" match="Games" use="Game_no"/>
<xsl:key name="game-group" match="Games" use="concat(substring-before(Name,'.'),'|',Game_no,'|',Group)"/>
<xsl:key name="gamekey2" use="concat(Game_no,'|',Group)" match="Games"/>
<xsl:template match="/">
    <Holiday>
            <xsl:for-each select="//t/Games[generate-id(.) = generate-id(key('gamekey', Game_no)[1])] ">
                <Weekend>
                    <xsl:variable name="var1" select="Game_no"/>
                    <xsl:apply-templates select="//t/Games[generate-id(.) = generate-id( key('game-group',        concat( substring-before(Name ,'.'),'|',$var1,'|',Group))[1])] [substring-before(Name ,'.')or not(Name/*)] "/>
                    <xsl:apply-templates select="key('game-group','||')/self::Games"/>
                </Weekend>
            </xsl:for-each>
    </Holiday>
</xsl:template>
<xsl:template match="Games [ substring-before(Name,'.') != '']">
    <xsl:element name="{substring-before(Name,'.')}">
        <xsl:for-each select="key('game-group', concat( substring-before(Name,'.'),'|',Game_no,'|',Group))">
            <xsl:element name="{substring-after(Name,'.')}">
                <xsl:value-of select="Value"/>
            </xsl:element>
        </xsl:for-each>
    </xsl:element>
</xsl:template>
<xsl:template match="Games [ substring-before(Name,'.') = '']">
<xsl:for-each select="key('gamekey2', concat( Game_no,'|',Group))">
<xsl:if test="not(contains(Name,'.'))">
    <xsl:element name="{Name}">
        <xsl:value-of select="Value"/>
    </xsl:element>
    </xsl:if>
    </xsl:for-each>
</xsl:template>

应用时获得所需的输出:

<?xml version="1.0" encoding="UTF-8"?>
<Holiday>
<Weekend>
    <game>
        <outdoor>Golf</outdoor>
        <indoor>Chess</indoor>
    </game>
    <ga>
        <outdoor>tennis</outdoor>
    </ga>
    <today>value returning with no'.'</today>
    <indoor>value returning with same Group 123 and game_no 1</indoor>
    <outdoor>value returning with different Group 2</outdoor>
</Weekend>
<Weekend>
    <games>
        <outdoor>Soccer</outdoor>
        <outdoor>Batminton</outdoor>
    </games>
    <games>
        <indoor>Golf</indoor>
    </games>
</Weekend>
<Weekend>
    <outdoor>value returning with different Game_no 323</outdoor>
</Weekend>
</Holiday>
于 2012-07-13T14:38:02.937 回答