2016-11-29 6 views
0

Я использую xslt 1.0 и хотел бы использовать динамическое имя столбца внутри группы. Пожалуйста, предложите мне варианты.Динамическое имя столбца в xslt 1.0 grouping

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

ответ

0

Как насчет

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

Благодаря @LarsH он работает идеально. – Manish