xslt 1.0分组中的动态列名称

时间:2016-11-29 10:10:25

标签: xslt xslt-1.0

我正在使用xslt 1.0,并希望在组内使用动态列名,请建议我选择。

<xsl:key name="district-by-state" match="District" use="concat(../../StateName, '+', TranslatedDistrictName)" /> 

赞 - 如果 TranslatedDistrictName 为NULL或为空,则使用 DistrictName ,否则使用TranslatedDistrictName。

代码

XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="State.xsl" ?>

<StateData>
<States>
    <State>
        <StateName>State1</StateName>
        <Districts>
            <District>
                <TranslatedDistrictName>AT</TranslatedDistrictName>
                <DistrictName>A</DistrictName>              
                <Population>10000</Population>              
            </District>         
            <District>
                <TranslatedDistrictName>AT</TranslatedDistrictName>
                <DistrictName>B</DistrictName>      
                <Population>5000</Population>                   
            </District>     
            <District>
                <TranslatedDistrictName>AC</TranslatedDistrictName>
                <DistrictName>A</DistrictName>
                <Population>2000</Population>               
            </District>                 
        </Districts>
    </State>
    <State>
        <StateName>State2</StateName>
        <Districts>
            <District>
                <TranslatedDistrictName>AC</TranslatedDistrictName>
                <DistrictName>C</DistrictName>
                <Population>8000</Population>               
            </District>         
            <District>
                <TranslatedDistrictName>AT</TranslatedDistrictName>
                <DistrictName>B</DistrictName>      
                <Population>5500</Population>                   
            </District>     
            <District>
                <TranslatedDistrictName>AP</TranslatedDistrictName>
                <DistrictName>A</DistrictName>
                <Population>1000</Population>               
            </District> 
        </Districts>
    </State>
</States>
</StateData>

XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">  
   <xsl:key name="district-by-state" match="District" use="concat(../../StateName, '+', DistrictName)" /> 
    <xsl:template match="StateData">
        <xsl:for-each select="States/State">                
                <xsl:variable name="stateName" select="StateName" />    
                <xsl:value-of select="$stateName" />                        
                <xsl:for-each select="Districts/District[count(. | key('district-by-state', concat($stateName, '+', DistrictName))[1]) = 1]">   
                    <hr/>
                    <xsl:value-of select="$stateName"/>
                    <xsl:value-of select="DistrictName"/>   
                    <xsl:value-of select="sum(key('district-by-state', concat($stateName, '+', DistrictName))/Population)" />
                </xsl:for-each>
            <hr/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

1 个答案:

答案 0 :(得分:0)

怎么样

<xsl:key name="district-by-state" match="District" 
   use="concat(../../StateName, '+', TranslatedDistrictName,
                       DistrictName[not(string(../TranslatedDistrictName))])" />