2013-07-25 7 views
0

Я хотел бы указать названия стран и стран в алфавитном порядке. Если есть какой-либо узел county1, ассоциированный с состоянием, мне нужно отобразить все состояния с именем страны. Однако, если нет состояния, которое начинается с определенного алфавита, типа «X», тогда он не должен показывать пустой. Я уверен, что это возможно с помощью xslt, но не знаю, как это сделать. Так что вы гуру там, pleeeeeeease помочь me.I я использую редактор visaul stuido2010 XML и xslt1.0 .. У меня нет никакого способа изменить version..I получил XSLT ударил здесь ..сортировать данные по узлам в алфавитном порядке с помощью xslt1.0

My Input xml Looka like below : 
    <?xml version="1.0" encoding="utf-8" ?> 
     <countries> 
     <country> 
      <state>Ontario</state> 
      <country1>CANADA</country1> 
      </country> 
      <country> 
      <state>Swindon</state> 
      </country> 
      <country> 
      <state>CAMDEN</state> 
      </country> 
      <country> 
      <state>NJ</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>NJ</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>NY</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>DE</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>Queenland</state> 
      <country1>Australia</country1> 
      </country> 
      <country> 
      <state>APstate</state> 
      </country> 
      <country> 
      <state>ANstate</state> 
      </country> 
     </countries> 

Мои вывод выглядит следующим образом:

A 
America 
    - DE 
    - NJ 
    - NY 
ANstate 
APstate 
Australia 
    -Queenland 
C 
CAMDEN 
CANADA 
    -Ontario 
S 
Swindon 
+0

возможно дубликат [Сортировка данных в XML алфавитном порядке] (http://stackoverflow.com/questions/17845199/sort-data-in-the-xml-alphabetical-order) –

ответ

0

Это дает точный результат, который вы запрашиваете.

Обратите внимание, что это трехуровневая группировка, которая в XSLT 1.0 легче всего выполняется с помощью переменных вместо использования метода Muenchian. Это еще проще в XSLT 2.0.

t:\ftemp>type countries.xml 
<?xml version="1.0" encoding="utf-8" ?> 
     <countries> 
     <country> 
      <state>Ontario</state> 
      <country1>CANADA</country1> 
      </country> 
      <country> 
      <state>Swindon</state> 
      </country> 
      <country> 
      <state>CAMDEN</state> 
      </country> 
      <country> 
      <state>NJ</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>NJ</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>NY</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>DE</state> 
      <country1>America</country1> 
      </country> 
      <country> 
      <state>Queenland</state> 
      <country1>Australia</country1> 
      </country> 
      <country> 
      <state>APstate</state> 
      </country> 
      <country> 
      <state>ANstate</state> 
      </country> 
     </countries> 
t:\ftemp>call xslt countries.xml countries.xsl 

A 
America 
    - DE 
    - NJ 
    - NY 
ANstate 
APstate 
Australia 
    - Queenland 
C 
CAMDEN 
CANADA 
    - Ontario 
S 
Swindon 
t:\ftemp>type countries.xsl 
<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 

<xsl:output method="text"/> 

<xsl:template match="countries"> 
    <!--set the population to be all top-level sorted constructs--> 
    <xsl:variable name="countries" 
       select="country/country1 | country[not(country1)]/state"/> 
    <xsl:for-each select="$countries"> 
    <xsl:sort select="."/> 
    <xsl:if test="generate-id(.)= 
        generate-id($countries[substring(.,1,1) = 
             substring(current(),1,1)][1])"> 
     <!--at first of the letter--> 
     <xsl:variable name="letters" 
        select="$countries[substring(.,1,1) = 
             substring(current(),1,1)]"/> 
     <xsl:text>&#xa;</xsl:text> 
     <xsl:value-of select="substring(.,1,1)"/> 
     <xsl:for-each select="$letters"> 
     <xsl:sort select="."/> 
     <xsl:if test="generate-id(.)= 
         generate-id($letters[. = current()][1])"> 
      <!--at first of a country--> 
      <xsl:text>&#xa;</xsl:text> 
      <xsl:value-of select="."/> 
      <xsl:variable name="states" 
         select="$letters[. = current()] 
              [self::country1]/../state"/> 
      <xsl:for-each select="$states"> 
      <xsl:sort select="."/> 
      <xsl:if test="generate-id(.)= 
          generate-id($states[.=current()][1])"> 
       <!--each of the states for a country--> 
       <xsl:text>&#xa; - </xsl:text> 
       <xsl:value-of select="."/> 
      </xsl:if> 
      </xsl:for-each> 
     </xsl:if> 
     </xsl:for-each> 
    </xsl:if> 
    </xsl:for-each> 
</xsl:template> 

</xsl:stylesheet> 
t:\ftemp>rem Done! 
+0

Спасибо за ответ Он работал отлично. – Blossom

Смежные вопросы