使用xslt1.0按字母顺序对节点数据进行排序
我想按字母顺序显示州和国家的名称。 如果有任何county1节点与状态关联,我需要显示所有 国家名称中的状态为。但是如果没有以开头的状态 一个特定的字母表,比如说“X”,那么它不应该显示一个空的。 我很确定xslt可以做到这一点,但我不知道如何去做。 各位专家,请帮帮我。我正在使用visaul stuido2010 xml编辑器和xslt1.0。。 我没有办法更改xslt版本..我在这里被击中使用xslt1.0按字母顺序对节点数据进行排序,xslt,xslt-1.0,xslt-grouping,Xslt,Xslt 1.0,Xslt Grouping,我想按字母顺序显示州和国家的名称。 如果有任何county1节点与状态关联,我需要显示所有 国家名称中的状态为。但是如果没有以开头的状态 一个特定的字母表,比如说“X”,那么它不应该显示一个空的。 我很确定xslt可以做到这一点,但我不知道如何去做。 各位专家,请帮帮我。我正在使用visaul stuido2010 xml编辑器和xslt1.0。。 我没有办法更改xslt版本..我在这里被击中 My Input xml Looka like below : <?xml vers
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>
这将为您提供所需的精确输出 注意这是三级分组,在XSLT1.0中,使用变量而不是使用Muenchian方法最容易实现。在XSLT2.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>
</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>
</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>
 - </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!
t:\ftemp>type countries.xml
安大略
加拿大
斯温顿
卡姆登
新泽西州
美国
新泽西州
美国
纽约
美国
判定元件
美国
昆士兰州
澳大利亚
囊状
安斯塔特
t:\ftemp>调用xslt countries.xml countries.xsl
A.
美国
-德
-新泽西州
-纽约
安斯塔特
囊状
澳大利亚
-皇后区
C
卡姆登
加拿大
-安大略省
s
斯温顿
t:\ftemp>type countries.xsl
;
;
;-
t:\ftemp>rem完成!
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>
</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>
</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>
 - </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!