|
| Re: xsl:variables. |
 |
Thu, 27 Mar 2008 14:40:49 +010 |
Guoqi Zheng wrote:
> I got a XML which I just need to display them based on Alphabet order,
> and at the beginning of every Alphabet, I write out the Alphabet letter.
Assuming the input XML is
<root>
<item>Canada</item>
<item>America</item>
<item>Belgium</item>
<item>Benelux</item>
<item>Australia</item>
<item>China</item>
</root>
then this stylesheet
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:key name="by-letter" match="item"
use="substring(., 1, 1)"/>
<xsl:template match="root">
<xsl:apply-templates select="item[generate-id() =
generate-id(key('by-letter', substring(., 1, 1))[1])]"
mode="group">
<xsl:sort select="substring(., 1, 1)"
data-type="text"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item" mode="group">
<xsl:value-of select="concat(substring(., 1, 1),
' ')"/>
<xsl:apply-templates select="key('by-letter', substring(., 1,
1))">
<xsl:sort select="." data-type="text"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="item">
<xsl:value-of select="concat(' ', ., ' ')"/>
<xsl:if test="position() = last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
creates the following result:
A
America
Australia
B
Belgium
Benelux
C
Canada
China
With XSLT 2.0 you can achieve the same as follows:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:output method="text"/>
<xsl:template match="root">
<xsl:for-each-group select="item" group-by="substring(.,
1, 1)">
<xsl:sort select="substring(., 1, 1)"/>
<xsl:value-of select="concat(current-grouping-key(),
' ')"/>
<xsl:apply-templates select="current-group()">
<xsl:sort select="." data-type="text"/>
</xsl:apply-templates>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="item">
<xsl:value-of select="concat(' ', ., ' ')"/>
<xsl:if test="position() = last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
--
Martin Honnen --- MVP XML
|
| Post Reply
|
| xsl:variables. |
 |
Thu, 27 Mar 2008 20:32:33 +080 |
Dear Sir,
I got a XML which I just need to display them based on Alphabet order, and
at the beginning of every Alphabet, I write out the Alphabet letter.
So I want the list to be something like
A
America
Australia
...
B
Belgium
Benelux
...
C
China
Canada
....
I was thinking to use xsl:variable and xsl:if test, however I can not change
value of xsl:variables. Any idea how should I do this?
Regards,
Guoqi Zheng
http://www.ureader.com
|
| Post Reply
|
| Re: xsl:variables. |
 |
Fri, 28 Mar 2008 02:56:51 +080 |
Martin,
You are really great. Everything works. However do you mind explaining to me
what does below code do?
generate-id() = generate-id(key('by-letter', substring(., 1, 1))[1])
I do not understand what does the generate-id do here? do you mind explain?
Regards,
Guoqi Zheng
"Martin Honnen" <mahotrash@yahoo.de> 写入消息
news:erQ2sABkIHA.2304@TK2MSFTNGP05.phx.gbl...
> Guoqi Zheng wrote:
>
>> I got a XML which I just need to display them based on Alphabet order,
>> and at the beginning of every Alphabet, I write out the Alphabet
letter.
>
> Assuming the input XML is
>
> <root>
> <item>Canada</item>
> <item>America</item>
> <item>Belgium</item>
> <item>Benelux</item>
> <item>Australia</item>
> <item>China</item>
> </root>
>
> then this stylesheet
>
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="1.0">
>
> <xsl:output method="text"/>
>
> <xsl:key name="by-letter" match="item"
use="substring(., 1, 1)"/>
>
> <xsl:template match="root">
> <xsl:apply-templates select="item[generate-id() =
> generate-id(key('by-letter', substring(., 1, 1))[1])]"
mode="group">
> <xsl:sort select="substring(., 1, 1)"
data-type="text"/>
> </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="item" mode="group">
> <xsl:value-of select="concat(substring(., 1, 1),
' ')"/>
> <xsl:apply-templates select="key('by-letter', substring(., 1,
1))">
> <xsl:sort select="." data-type="text"/>
> </xsl:apply-templates>
> </xsl:template>
>
> <xsl:template match="item">
> <xsl:value-of select="concat(' ', ., ' ')"/>
> <xsl:if test="position() = last()">
> <xsl:text> </xsl:text>
> </xsl:if>
> </xsl:template>
>
> </xsl:stylesheet>
>
> creates the following result:
>
>
> A
> America
> Australia
>
> B
> Belgium
> Benelux
>
> C
> Canada
> China
>
>
> With XSLT 2.0 you can achieve the same as follows:
>
> <xsl:stylesheet
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> version="2.0">
>
> <xsl:output method="text"/>
>
> <xsl:template match="root">
> <xsl:for-each-group select="item"
group-by="substring(., 1, 1)">
> <xsl:sort select="substring(., 1, 1)"/>
> <xsl:value-of select="concat(current-grouping-key(),
' ')"/>
> <xsl:apply-templates select="current-group()">
> <xsl:sort select="." data-type="text"/>
> </xsl:apply-templates>
> </xsl:for-each-group>
> </xsl:template>
>
> <xsl:template match="item">
> <xsl:value-of select="concat(' ', ., ' ')"/>
> <xsl:if test="position() = last()">
> <xsl:text> </xsl:text>
> </xsl:if>
> </xsl:template>
>
> </xsl:stylesheet>
>
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
|
| Post Reply
|
| Re: xsl:variables. |
 |
Fri, 28 Mar 2008 03:13:04 +080 |
Also in this template
-----------------------------------------------------------
<xsl:template match="item">
<xsl:value-of select="concat(' ', ., ' ')"/>
<xsl:if test="position() = last()">
<xsl:text> </xsl:text>
</xsl:if>
</xsl:template>
-----------------------------------------------------------
I need to get the position of this item in the whole data, not just this
group only, How am I going to get the position()
Thanks.
Guoqi Zheng
"Guoqi Zheng" <no@sorry.com> 写入消息
news:E22A88F1-A666-4738-BC81-5D60F9F9CD42@microsoft.com...
> Martin,
>
> You are really great. Everything works. However do you mind explaining to
> me what does below code do?
> generate-id() = generate-id(key('by-letter', substring(., 1, 1))[1])
>
> I do not understand what does the generate-id do here? do you mind
> explain?
>
> Regards,
>
> Guoqi Zheng
>
> "Martin Honnen" <mahotrash@yahoo.de> 写入消息
> news:erQ2sABkIHA.2304@TK2MSFTNGP05.phx.gbl...
>> Guoqi Zheng wrote:
>>
>>> I got a XML which I just need to display them based on Alphabet
order,
>>> and at the beginning of every Alphabet, I write out the Alphabet
letter.
>>
>> Assuming the input XML is
>>
>> <root>
>> <item>Canada</item>
>> <item>America</item>
>> <item>Belgium</item>
>> <item>Benelux</item>
>> <item>Australia</item>
>> <item>China</item>
>> </root>
>>
>> then this stylesheet
>>
>> <xsl:stylesheet
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> version="1.0">
>>
>> <xsl:output method="text"/>
>>
>> <xsl:key name="by-letter" match="item"
use="substring(., 1, 1)"/>
>>
>> <xsl:template match="root">
>> <xsl:apply-templates select="item[generate-id() =
>> generate-id(key('by-letter', substring(., 1, 1))[1])]"
mode="group">
>> <xsl:sort select="substring(., 1, 1)"
data-type="text"/>
>> </xsl:apply-templates>
>> </xsl:template>
>>
>> <xsl:template match="item" mode="group">
>> <xsl:value-of select="concat(substring(., 1, 1),
' ')"/>
>> <xsl:apply-templates select="key('by-letter', substring(.,
1, 1))">
>> <xsl:sort select="."
data-type="text"/>
>> </xsl:apply-templates>
>> </xsl:template>
>>
>> <xsl:template match="item">
>> <xsl:value-of select="concat(' ', .,
' ')"/>
>> <xsl:if test="position() = last()">
>> <xsl:text> </xsl:text>
>> </xsl:if>
>> </xsl:template>
>>
>> </xsl:stylesheet>
>>
>> creates the following result:
>>
>>
>> A
>> America
>> Australia
>>
>> B
>> Belgium
>> Benelux
>>
>> C
>> Canada
>> China
>>
>>
>> With XSLT 2.0 you can achieve the same as follows:
>>
>> <xsl:stylesheet
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> version="2.0">
>>
>> <xsl:output method="text"/>
>>
>> <xsl:template match="root">
>> <xsl:for-each-group select="item"
group-by="substring(., 1, 1)">
>> <xsl:sort select="substring(., 1, 1)"/>
>> <xsl:value-of select="concat(current-grouping-key(),
' ')"/>
>> <xsl:apply-templates select="current-group()">
>> <xsl:sort select="."
data-type="text"/>
>> </xsl:apply-templates>
>> </xsl:for-each-group>
>> </xsl:template>
>>
>> <xsl:template match="item">
>> <xsl:value-of select="concat(' ', .,
' ')"/>
>> <xsl:if test="position() = last()">
>> <xsl:text> </xsl:text>
>> </xsl:if>
>> </xsl:template>
>>
>> </xsl:stylesheet>
>>
>>
>> --
>>
>> Martin Honnen --- MVP XML
>> http://JavaScript.FAQTs.com/
|
| Post Reply
|
| Re: xsl:variables. |
 |
Fri, 28 Mar 2008 04:00:59 +080 |
Also the function works as case sensitive, how can I make it
case-insensitive?
Regards,
Guoqi Zheng
"Guoqi Zheng" <no@sorry.com> 写入消息
news:8E37EA2D-6CB9-4B36-B423-7E2DC932D24D@microsoft.com...
> Also in this template
> -----------------------------------------------------------
> <xsl:template match="item">
> <xsl:value-of select="concat(' ', ., ' ')"/>
> <xsl:if test="position() = last()">
> <xsl:text> </xsl:text>
> </xsl:if>
> </xsl:template>
> -----------------------------------------------------------
>
> I need to get the position of this item in the whole data, not just this
> group only, How am I going to get the position()
>
> Thanks.
>
> Guoqi Zheng
>
>
> "Guoqi Zheng" <no@sorry.com> 写入消息
> news:E22A88F1-A666-4738-BC81-5D60F9F9CD42@microsoft.com...
>> Martin,
>>
>> You are really great. Everything works. However do you mind explaining
to
>> me what does below code do?
>> generate-id() = generate-id(key('by-letter', substring(., 1, 1))[1])
>>
>> I do not understand what does the generate-id do here? do you mind
>> explain?
>>
>> Regards,
>>
>> Guoqi Zheng
>>
>> "Martin Honnen" <mahotrash@yahoo.de> 写入消息
>> news:erQ2sABkIHA.2304@TK2MSFTNGP05.phx.gbl...
>>> Guoqi Zheng wrote:
>>>
>>>> I got a XML which I just need to display them based on Alphabet
order,
>>>> and at the beginning of every Alphabet, I write out the
Alphabet
>>>> letter.
>>>
>>> Assuming the input XML is
>>>
>>> <root>
>>> <item>Canada</item>
>>> <item>America</item>
>>> <item>Belgium</item>
>>> <item>Benelux</item>
>>> <item>Australia</item>
>>> <item>China</item>
>>> </root>
>>>
>>> then this stylesheet
>>>
>>> <xsl:stylesheet
>>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>>> version="1.0">
>>>
>>> <xsl:output method="text"/>
>>>
>>> <xsl:key name="by-letter" match="item"
use="substring(., 1, 1)"/>
>>>
>>> <xsl:template match="root">
>>> <xsl:apply-templates select="item[generate-id() =
>>> generate-id(key('by-letter', substring(., 1, 1))[1])]"
mode="group">
>>> <xsl:sort select="substring(., 1, 1)"
data-type="text"/>
>>> </xsl:apply-templates>
>>> </xsl:template>
>>>
>>> <xsl:template match="item"
mode="group">
>>> <xsl:value-of select="concat(substring(., 1, 1),
' ')"/>
>>> <xsl:apply-templates select="key('by-letter',
substring(., 1, 1))">
>>> <xsl:sort select="."
data-type="text"/>
>>> </xsl:apply-templates>
>>> </xsl:template>
>>>
>>> <xsl:template match="item">
>>> <xsl:value-of select="concat(' ', .,
' ')"/>
>>> <xsl:if test="position() = last()">
>>> <xsl:text> </xsl:text>
>>> </xsl:if>
>>> </xsl:template>
>>>
>>> </xsl:stylesheet>
>>>
>>> creates the following result:
>>>
>>>
>>> A
>>> America
>>> Australia
>>>
>>> B
>>> Belgium
>>> Benelux
>>>
>>> C
>>> Canada
>>> China
>>>
>>>
>>> With XSLT 2.0 you can achieve the same as follows:
>>>
>>> <xsl:stylesheet
>>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>>> version="2.0">
>>>
>>> <xsl:output method="text"/>
>>>
>>> <xsl:template match="root">
>>> <xsl:for-each-group select="item"
group-by="substring(., 1, 1)">
>>> <xsl:sort select="substring(., 1, 1)"/>
>>> <xsl:value-of select="concat(current-grouping-key(),
' ')"/>
>>> <xsl:apply-templates
select="current-group()">
>>> <xsl:sort select="."
data-type="text"/>
>>> </xsl:apply-templates>
>>> </xsl:for-each-group>
>>> </xsl:template>
>>>
>>> <xsl:template match="item">
>>> <xsl:value-of select="concat(' ', .,
' ')"/>
>>> <xsl:if test="position() = last()">
>>> <xsl:text> </xsl:text>
>>> </xsl:if>
>>> </xsl:template>
>>>
>>> </xsl:stylesheet>
>>>
>>>
>>> --
>>>
>>> Martin Honnen --- MVP XML
>>> http://JavaScript.FAQTs.com/
|
| Post Reply
|
|
|