XSLT 2.0 change to 1.0 - xslt

I have a XSL-File which has been written for being used with a XSLT 2.0 Transformer, but now I need to change the file in order to make it work with XSLT 1.0. I know the for-each-group-command is a XSLT 2.0 feature, but what about all the other things? How would I change my file to make it work with 1.0?
Here's the XSL-File:
<xsl:template match="ROWSET">
<ROWSET>
<xsl:for-each-group select="ROW" group-by="UKID">
<UEBERKUNDE>
<NAME><xsl:value-of select="UEBERKUNDE" /></NAME>
<xsl:copy-of select="UKID" />
<xsl:for-each-group select="current-group()" group-by="KUNDENNR">
<KUNDE>
<xsl:copy-of select="KUNDENNR" />
<xsl:copy-of select="KNAME1" />
<xsl:copy-of select="KNAME2" />
<xsl:copy-of select="KNAME3" />
<xsl:copy-of select="LAND" />
<xsl:copy-of select="PLZ" />
<xsl:copy-of select="ORT" />
<xsl:copy-of select="ADM" />
<xsl:copy-of select="KUNDENKLASSE" />
<xsl:copy-of select="MITARBEITER" />
<xsl:copy-of select="BELEGART" />
<EFAKTURA><xsl:value-of select="normalize-space(EFAKTURA)" /></EFAKTURA>
<WEBSTATUS><xsl:value-of select="normalize-space(WEBSTATUS)" /></WEBSTATUS>
<ZAHLUNGSBEDINGUNG><xsl:value-of select="normalize-space(ZAHLUNGSBEDINUNG)" /></ZAHLUNGSBEDINGUNG>
<xsl:for-each select="current-group()[count(. | key('rowsByMonth', concat(MONAT,'+',JAHR,'+',KUNDENNR))[1]) = 1]">
<ROW>
<xsl:copy-of select="JAHR" />
<xsl:copy-of select="MONAT" />
<xsl:copy-of select="HANDLING" />
<xsl:copy-of select="SOLLFRACHT" />
<xsl:for-each select="key('rowsByMonth', concat(MONAT,'+',JAHR,'+',KUNDENNR))">
<WARENGRUPPE>
<xsl:copy-of select="HGNAME" />
<xsl:copy-of select="HGID" />
<xsl:copy-of select="DG_BASIS" />
<xsl:copy-of select="EKECHT" />
<xsl:copy-of select="DB_BASIS" />
<xsl:copy-of select="NETTO" />
</WARENGRUPPE>
</xsl:for-each>
</ROW>
</xsl:for-each>
</KUNDE>
</xsl:for-each-group>
</UEBERKUNDE>
</xsl:for-each-group>
</ROWSET>
</xsl:template></xsl:stylesheet>
Thanks in advance!

If you want to do grouping in XSLT 1.0 then see http://www.jenitennison.com/xslt/grouping/muenchian.xml for simply "group by" and http://www.biglist.com/lists/xsl-list/archives/200101/msg00070.html for nested grouping.
So
<ROWSET>
<xsl:for-each-group select="ROW" group-by="UKID">
<UEBERKUNDE>
<NAME><xsl:value-of select="UEBERKUNDE" /></NAME>
<xsl:copy-of select="UKID" />
<xsl:for-each-group select="current-group()" group-by="KUNDENNR">
roughly translate into two keys
<xsl:key name="by-ukid" match="ROW" use="UKID"/>
<xsl:key name="by-nr" match="ROW" use="concat(UKID, '|', KUNDENNR)"/>
and
<ROWSET>
<xsl:for-each select="ROW[generate-id() = generate-id(key('by-ukid',UKID)[1])]">
<UEBERKUNDE>
<NAME><xsl:value-of select="UEBERKUNDE" /></NAME>
<xsl:copy-of select="UKID" />
<xsl:for-each select="key('by-ukid', UKID)[generate-id() = generate-id(key('by-nr', concat(UKID, '|', KUNDENNR))[1])]">
If you have further nested grouping you need more keys with concatenated key values.

Related

replace xml value with sequentail numbers

Hi I have been trying to replace values in a certain tag with sequential numbers, I used position function, but it did not work.
Input xml:
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<User id="swarnai" />
<Request-Id id="592149819" />
<Type name="Request" />
<Application-Source version="8.1.1.10" name="Siebel" />
<Application-Destination version="1.2.2" name="EVA" />
<Outgo-Timestamp time="11:40:59" date="2015-08-04" />
<DealerCode>82536</DealerCode>
<Market>00000</Market>
</Header>
<Content>
<ClaimContext>
<ClaimControl>
<ClaimEntryFlag>3</ClaimEntryFlag>
<ClaimSaveFlag>1</ClaimSaveFlag>
</ClaimControl>
<Claim>
<DealerClaimNumber>1091871</DealerClaimNumber>
<WHC>WDD</WHC>
<FIN>2120026L020301</FIN>
<RegistrationNumber>JH07E2786</RegistrationNumber>
<FirstRegistrationDate>2012-11-29</FirstRegistrationDate>
<Mileage>14317</Mileage>
<MileageIndicator>0</MileageIndicator>
<RepairDate>2013-12-03</RepairDate>
<RegularlyMaintained>true</RegularlyMaintained>
<NoFirstRegDateInd>false</NoFirstRegDateInd>
<ClaimCurrencyId>EUR</ClaimCurrencyId>
<Taxi>false</Taxi>
<DamagePosition>
<DamageSeqNumber>1</DamageSeqNumber>
<WarrantyType>0</WarrantyType>
<DamageCode>0121504</DamageCode>
<OperationPosition>
<SeqNumber>1</SeqNumber>
<Opcode>02770501</Opcode>
<WorkingUnits>18</WorkingUnits>
<MainOperationCode>true</MainOperationCode>
<OperationText>OPERATIONS: FITTING FOR COOLANT CONNECTION TO CYLINDER</OperationText>
<PriceGroup>01</PriceGroup>
</OperationPosition>
</DamagePosition>
<DamagePosition>
<DamageSeqNumber>1</DamageSeqNumber>
<WarrantyType>0</WarrantyType>
<DamageCode>0121504</DamageCode>
<PartPosition>
<SeqNumber>1</SeqNumber>
<Quantity>10</Quantity>
<PartNumber>A6512001056</PartNumber>
<DamageCausingPart>true</DamageCausingPart>
<RetailPriceAmount>1499</RetailPriceAmount>
<Express>false</Express>
</PartPosition>
</DamagePosition>
<DamagePosition>
<DamageSeqNumber>1</DamageSeqNumber>
<WarrantyType>0</WarrantyType>
<DamageCode>0121504</DamageCode>
<PartPosition>
<SeqNumber>1</SeqNumber>
<Quantity>20</Quantity>
<PartNumber>A0009890825 10</PartNumber>
<DamageCausingPart>false</DamageCausingPart>
<RetailPriceAmount>1319</RetailPriceAmount>
<Express>false</Express>
</PartPosition>
</DamagePosition>
</Claim>
</ClaimContext>
</Content>
</Envelope>
In this xml within PartPosition/SeqNumber tag I want to replace or generate sequence of numbers in SeqNumber tag.
I tried below xslt:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="utf-8" indent="no"/>
<xsl:key name="Damage_Group" match="/Envelope/Content/ClaimContext/Claim/DamagePosition" use="DamageCode" />
<xsl:template match="Claim">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
<xsl:for-each select="DamagePosition[count(. | key('Damage_Group', DamageCode)[1]) = 1]">
<xsl:sort select="DamageCode" />
<DamagePosition>
<DamageSeqNumber>
<xsl:value-of select="position()" />
</DamageSeqNumber>
<DamageCode>
<xsl:value-of select="DamageCode" />
</DamageCode>
<WarrantyType><xsl:value-of select="WarrantyType" /></WarrantyType>
<xsl:for-each select="key('Damage_Group', DamageCode)">
<xsl:copy-of select="current()/PartPosition"/>
<xsl:copy-of select="current()/OperationPosition"/>
<xsl:copy-of select="current()/SubletPosition"/>
</xsl:for-each>
</DamagePosition>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="DamagePosition">
</xsl:template>
</xsl:stylesheet>
but it is not giving me the desired output as below.
<?xml version="1.0" encoding="UTF-8"?>
<Envelope>
<Header>
<User id="swarnai" />
<Request-Id id="592149819" />
<Type name="Request" />
<Application-Source version="8.1.1.10" name="Siebel" />
<Application-Destination version="1.2.2" name="EVA" />
<Outgo-Timestamp time="12:15:47" date="2015-08-04" />
<DealerCode>82536</DealerCode>
<Market>00000</Market>
</Header>
<Content>
<ClaimContext>
<ClaimControl>
<ClaimEntryFlag>3</ClaimEntryFlag>
<ClaimSaveFlag>1</ClaimSaveFlag>
</ClaimControl>
<Claim>
<DealerClaimNumber>1091871</DealerClaimNumber>
<WHC>WDD</WHC>
<FIN>2120026L020301</FIN>
<RegistrationNumber>JH07E2786</RegistrationNumber>
<FirstRegistrationDate>2012-11-29</FirstRegistrationDate>
<Mileage>14317</Mileage>
<MileageIndicator>0</MileageIndicator>
<RepairDate>2013-12-03</RepairDate>
<RegularlyMaintained>true</RegularlyMaintained>
<NoFirstRegDateInd>false</NoFirstRegDateInd>
<ClaimCurrencyId>EUR</ClaimCurrencyId>
<Taxi>false</Taxi>
<DamagePosition>
<DamageSeqNumber>1</DamageSeqNumber>
<DamageCode>0121504</DamageCode>
<WarrantyType>0</WarrantyType>
<OperationPosition>
<SeqNumber>1</SeqNumber>
<Opcode>02770501</Opcode>
<WorkingUnits>18</WorkingUnits>
<MainOperationCode>true</MainOperationCode>
<OperationText>OPERATIONS: FITTING FOR COOLANT CONNECTION TO CYLINDER</OperationText>
<PriceGroup>01</PriceGroup>
</OperationPosition>
<PartPosition>
<SeqNumber>1</SeqNumber>
<Quantity>10</Quantity>
<PartNumber>A6512001056</PartNumber>
<DamageCausingPart>true</DamageCausingPart>
<RetailPriceAmount>1499</RetailPriceAmount>
<Express>false</Express>
</PartPosition>
<PartPosition>
<SeqNumber>2</SeqNumber>
<Quantity>20</Quantity>
<PartNumber>A0009890825 10</PartNumber>
<DamageCausingPart>false</DamageCausingPart>
<RetailPriceAmount>1319</RetailPriceAmount>
<Express>false</Express>
</PartPosition>
</DamagePosition>
</Claim>
</ClaimContext>
</Content>
</Envelope>
Any help is much appreciated.
Thanks
Firstly, instead of doing this, which copies the elements without further changes to it (or its descendants)
<xsl:copy-of select="current()/PartPosition"/>
<xsl:copy-of select="current()/OperationPosition"/>
<xsl:copy-of select="current()/SubletPosition"/>
You should you template matching instead, to allow you to write a template later on to change the sequence number
<xsl:apply-templates select="PartPosition"/>
<xsl:apply-templates select="OperationPosition"/>
<xsl:apply-templates select="SubletPosition"/>
Now, what you could do, is write a template that matches the various Sequence numbers and counts the preceding siblings for the same DamageCode. Something like this
<xsl:template match="PartPosition/SeqNumber">
<SeqNumber>
<xsl:variable name="DamageCode" select="../../DamageCode" />
<xsl:value-of select="count(../../preceding-sibling::*[PartPosition][DamageCode = $DamageCode]) + 1" />
</SeqNumber>
</xsl:template>
You could repeat the template for OperationPosition and SubletPosition too, although this would be quite repetitive. Alternatively, have a single generic template to match all three:
<xsl:template match="SeqNumber">
<SeqNumber>
<xsl:variable name="DamageCode" select="../../DamageCode" />
<xsl:variable name="Parent" select="name(..)" />
<xsl:value-of select="count(../../preceding-sibling::*[*[name() = $Parent]][DamageCode = $DamageCode]) + 1" />
</SeqNumber>
</xsl:template>
However, this is not necessarily that efficient, as it is not utilizing the key and so would have to check back through all preceding siblings to find a match.
Another solution would be to pass the position of the current DamagePosition in a parameter, and then use that in checking the key.
Try this XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:key name="Damage_Group"
match="/Envelope/Content/ClaimContext/Claim/DamagePosition"
use="DamageCode"/>
<xsl:template match="Claim">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:for-each select="DamagePosition[count(. | key('Damage_Group', DamageCode)[1]) = 1]">
<xsl:sort select="DamageCode"/>
<DamagePosition>
<DamageSeqNumber>
<xsl:value-of select="position()"/>
</DamageSeqNumber>
<DamageCode>
<xsl:value-of select="DamageCode"/>
</DamageCode>
<WarrantyType>
<xsl:value-of select="WarrantyType"/>
</WarrantyType>
<xsl:for-each select="key('Damage_Group', DamageCode)">
<xsl:apply-templates select="PartPosition">
<xsl:with-param name="Position" select="position()" />
</xsl:apply-templates>
<xsl:apply-templates select="OperationPosition">
<xsl:with-param name="Position" select="position()" />
</xsl:apply-templates>
<xsl:apply-templates select="SubletPosition">
<xsl:with-param name="Position" select="position()" />
</xsl:apply-templates>
</xsl:for-each>
</DamagePosition>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="DamagePosition/*">
<xsl:param name="Position" />
<xsl:copy>
<xsl:apply-templates select="#*|node()">
<xsl:with-param name="Position" select="$Position" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="SeqNumber">
<xsl:param name="Position" />
<SeqNumber>
<xsl:value-of select="count(key('Damage_Group', ../../DamageCode)[position() <= $Position][*[name() = name(current()/..)]])" />
</SeqNumber>
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="DamagePosition" />
</xsl:stylesheet>

Custom order and filter

I have a SharePoint list with news items. From that, I want to be able to select articles, and change the order of the selected items to make some kind of a newsletter.
I tried using a query-string to specify item-IDs and their order, something like this:
SortFilter.aspx?selection='i13o1,i5o2,i27o3,'
( i is for Item, o is for Order)
The filtering works ok, but I can't figure out how to sort using $elementLookup
<xsl:param name="QUERY_STRING" />
<xsl:variable name="string" select= 'substring-before(substring-after($QUERY_STRING, "&apos;"), "&apos;")'/>
<xsl:key name="myKey" match="Row" use="#ID" />
<xsl:template match="/">
<xsl:for-each select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('myKey',#ID))]">
<xsl:variable name="articleNumber">
<xsl:text>i</xsl:text><xsl:value-of select="#ID" /><xsl:text>o</xsl:text>
</xsl:variable>
<xsl:variable name="elementLookUp" select= 'substring-before(substring-after($string, $articleNumber), ",")'/>
<xsl:if test="$elementLookUp">
<xsl:value-of select="#ID" />
<xsl:text> </xsl:text>
<xsl:value-of select="#Title" />
<xsl:text> </xsl:text>
<xsl:value-of select="$elementLookUp"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
You should be able to do so like this:
<xsl:sort
select="substring-before(substring-after($string, concat('i', #ID, 'o')), ',')"
data-type="number" />
This expression will select the number that comes after i<id>o, and then you can sort on that.
After a bit of tidying, your stylesheet becomes:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="QUERY_STRING" />
<xsl:variable
name="selection"
select='substring-before(substring-after($QUERY_STRING, "selection=&apos;"), "&apos;")'/>
<xsl:key name="myKey" match="Row" use="#ID" />
<xsl:template match="/">
<xsl:for-each
select="/dsQueryResponse/Rows/Row[generate-id(.)=generate-id(key('myKey',#ID)[1])]">
<xsl:sort
select="substring-before(substring-after($selection, concat('i', #ID, 'o')), ',')"
data-type="number"
/>
<xsl:variable name="articleNumber" select="concat('i', #ID, 'o')" />
<xsl:variable name="elementLookUp"
select='substring-before(substring-after($selection, $articleNumber), ",")'/>
<xsl:if test="$elementLookUp">
<xsl:value-of select="#ID" />
<xsl:text> </xsl:text>
<xsl:value-of select="#Title" />
<xsl:text> </xsl:text>
<xsl:value-of select="$elementLookUp"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

print xpath and value of element and attribute using XSLT

I would like to print path of element and attributes if any along with values using XSLT. e.g
XML :
<root>
<node attr='abc' module='try'>
<subnode>Roshan</subnode>
<subnode>Chetan</subnode>
<subnode>Jennifer</subnode>
</node>
</root>
Output :
/root##
/root/node##
/root/node/#attr##abc
/root/node/#module##try
/root/node/subnode[1]##Roshan
/root/node/subnode[2]##Chetan
/root/node/subnode[3]##Jennifer
I am trying with below snippet, but could only print path of element and it's value
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name())" />
<xsl:if
test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
<xsl:value-of
select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')" />
</xsl:if>
<!-- <xsl:call-template name="attrData"></xsl:call-template> -->
</xsl:for-each>
<xsl:text>##</xsl:text>
<xsl:apply-templates select="node()" />
</xsl:template>
I am new to XSLT. Please help!!!!
I made the following XSLT and added also the [position] to the output. You can remove that if you need.
This gives this output:
/root[1]
/root[1]/node[1]
/root[1]/node[1]/#attr[1]##abc
/root[1]/node[1]/#module[1]##try
/root[1]/node[1]/subnode[1]##Roshan
/root[1]/node[1]/subnode[2]##Chetan
/root[1]/node[1]/subnode[3]##Jennifer
With this XSLT. With the two output template you can choose how to print the Xpath.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" version="2.0">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="''"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generateXPath">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:if test="not(empty(.))">
<xsl:variable name="thisXPath" select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:variable>
<xsl:apply-templates select="." mode="output">
<xsl:with-param name="previous" select="$previous"/>
</xsl:apply-templates>
<xsl:text>
</xsl:text>
<xsl:for-each select="*|#*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="$thisXPath"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath">
<xsl:value-of select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:value-of>
<xsl:if test="not(*)">
<xsl:value-of select="concat('##',text())"></xsl:value-of>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
<xsl:template match="#*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath" select="concat($previous, '/#', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']','##',.)"></xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
</xsl:stylesheet>

How to create an element in XSLT with a dynamic attribute?

I have some XML that looks something like this:
<ExtensionObject>
<Value xmlns="">
<Key>key01</Key>
<StringValue>somewords</StringValue>
</Value>
<Value xmlns="">
<Key>key01</Key>
<NumberValue>12345</NumberValue>
</Value>
...........hundreds more Values..............
</ExtensionObject>
I rather optimistically wrote XSLT like this:
<xsl:template match="/ExtensionObject">
<VResult>
<xsl:for-each select="Value">
<xsl:variable name="fld">
<Value><xsl:value-of select="Key"/></Value>
</xsl:variable>
<Result field="$fld">
<Value>
<xsl:choose>
<xsl:when test="NumberValue">
<xsl:value-of select="NumberValue"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="StringValue"/>
</xsl:otherwise>
</xsl:choose>
</Value>
</Result>
</xsl:for-each>
</VResult>
</xsl:template>
And expected to get this:
<VResult>
<Result field="key01"><Value>somewords</Value></Result>
<Result field="key02"><Value>12345</Value></Result>
...........hundreds more Values..............
</VResult>
Instead I got:
<VResult>
<Result field="$fld"><Value>somewords</Value></Result>
<Result field="$fld"><Value>12345</Value></Result>
...........hundreds more Values..............
</VResult>
Does anyone know how I can insert the field names with the key value?
<xsl:attribute name="field"><xsl:value-of select="Key"/></xsl:attribute>
Instead of using for-each, it's always better to use the templates:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<VResults>
<xsl:apply-templates select='*' />
</VResults>
</xsl:template>
<xsl:template match="Value">
<Result>
<xsl:attribute name='field'><xsl:value-of select='Key' /></xsl:attribute>
<xsl:apply-templates />
</Result>
</xsl:template>
<xsl:template match="NumberValue|StringValue">
<Value><xsl:value-of select='.' /></Value>
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>

xsl grouping sort problem

I have the following xsl template that I'm using to group my xsl. The problem I have is that I need to uppercase the #Title as currently my grouping is seeing upper and lowercase as seperate groups.
<xsl:key name="rows-by-title" match="Row" use="substring(#Title,1,1)" />
<xsl:template name="Meunchian" match="/dsQueryResponse/Rows">
<xsl:for-each select="Row[count(. | key('rows-by-title', substring(#Title,1,1))[1]) = 1]">
<xsl:sort select="substring(#Title,1,1)" />
<p></p><xsl:value-of select="substring(#Title,1,1)" /><br />
<xsl:for-each select="key('rows-by-title', substring(#Title,1,1))">
<xsl:value-of select="#Title" /><br/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
I tried to use call-template and set a variable but xsl does not seem to like this:
<xsl:key name="rows-by-title" match="Row" use="substring(#Title,1,1)" />
<xsl:template name="Meunchian" match="/dsQueryResponse/Rows">
<xsl:for-each select="Row[count(. | key('rows-by-title', substring(#Title,1,1))[1]) = 1]">
<xsl:variable name="myTitle">
<xsl:call-template name="to-upper">
<xsl:with-param name="text">
<xsl:value-of select="#Title"/>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<p></p><xsl:value-of select="$myTitle" /><br />
<xsl:for-each select="key('rows-by-title', substring(#Title,1,1))">
<xsl:value-of select="#Title" /><br/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
What I am trying to achieve is meunchian grouping but without case sensitivity - hope this makes Sense!
Kieran
The way to convert lower-case letters to upper is to use the XPath translate() function.
Using it, one way to express the desired transformation is the following:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml"/>
<xsl:variable name="vLower" select=
"'abcdefghijklmnopqrstuvwxyz'"
/>
<xsl:variable name="vUpper" select=
"'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
/>
<xsl:key name="rows-by-title" match="Row" use=
"translate(substring(#Title,1,1),
'abcdefghijklmnopqrstuvwxyz',
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
)" />
<xsl:template match="/">
<html>
<xsl:apply-templates select="*/*"/>
</html>
</xsl:template>
<xsl:template name="Meunchian" match="/dsQueryResponse/Rows">
<xsl:for-each select=
"Row[generate-id()
=
generate-id(key('rows-by-title',
translate(substring(#Title,1,1),
$vLower,
$vUpper)
)[1]
)
]">
<xsl:sort select="translate(substring(#Title,1,1),
$vLower,
$vUpper)" />
<p></p>
<xsl:value-of select="translate(substring(#Title,1,1),
$vLower,
$vUpper)" />
<br />
<xsl:for-each select=
"key('rows-by-title',
translate(substring(#Title,1,1),
$vLower,
$vUpper)">
<xsl:value-of select="#Title" />
<br/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
When applied on the following XML document:
<dsQueryResponse>
<Rows>
<Row Title="Agenda" />
<Row Title="Policy" />
<Row Title="policy" />
<Row Title="Report" />
<Row Title="report" />
<Row Title="Test2" />
<Row Title="test1" />
<Row Title="Boo" />
<Row Title="foo" />
</Rows>
</dsQueryResponse>
it produces the desired result:
<html>
<p/>A
<br/>Agenda
<br/>
<p/>B
<br/>Boo
<br/>
<p/>F
<br/>foo
<br/>
<p/>P
<br/>Policy
<br/>policy
<br/>
<p/>R
<br/>Report
<br/>report
<br/>
<p/>T
<br/>Test2
<br/>test1
<br/>
</html>
In XPath 2.0 one will use the upper-case() function to convert lower case to upper case.
Also, grouping in XSLT 2.0 can be better expressed using the <xsl:for-each-group>
instruction.