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.
Related
I need nesting of list in the following manner, I have to create n-1 list wrappers, where n is the value of #virtual-nesting:
XML Doc:
<l virtual-nesting="3">
<li>
<lilabel/>
<p>
<text>Data used:</text>
</p>
</li>
</l>
Output Required:
<list>
<listitem>
<bodytext>
<list>
<listitem>
<label/>
<bodytext>
<p>
<text>Data used:</text>
</p>
</bodytext>
</listitem>
</list>
</bodytext>
</listitem>
</list>
XSLT Itried. As I am new to this:
<xsl:template match="l">
<xsl:choose>
<xsl:when test="#virtual-nesting">
<xsl:variable name="virtual"><xsl:value-of select="#virtual-nesting"/></xsl:variable>
<xsl:if test="$virtual>0">
<xsl:apply-templates select="generate-id(following-sibling::node()),#virtual-nesting-1"/>
</xsl:if>
<xsl:apply-templates select="li"/>
</xsl:when>
<xsl:otherwise>
<xsl:element name="list">
<xsl:apply-templates select="li"/> <!-- Template for list and lilabel is already created -->
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
Kindly guide me through this.
Recursion is the key to creating nested structures in XSLT.
The following uses a recursive <xsl:template match="l"> with a $level parameter. This parameter defaults to #virtual-nesting - 1 and after that is decremented with every recursive step.
The template takes two different paths, depending on its value.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="l">
<xsl:param name="level" select="#virtual-nesting - 1" />
<list>
<listitem>
<xsl:if test="$level <= 1">
<xsl:apply-templates />
</xsl:if>
<xsl:if test="$level > 1">
<bodytext>
<xsl:apply-templates select=".">
<xsl:with-param name="level" select="$level - 1" />
</xsl:apply-templates>
</bodytext>
</xsl:if>
</listitem>
</list>
</xsl:template>
<xsl:template match="li">
<label>
<xsl:apply-templates select="lilabel/node()" />
</label>
<bodytext>
<xsl:apply-templates select="node()[not(self::lilabel)]" />
</bodytext>
</xsl:template>
</xsl:transform>
Result:
<list>
<listitem>
<bodytext>
<list>
<listitem>
<label/>
<bodytext>
<p>
<text>Data used:</text>
</p>
</bodytext>
</listitem>
</list>
</bodytext>
</listitem>
</list>
I have a scenario where I need to convert the input XML to a CSV file. The output should have values for every attribute with their respective XPATH.
For example: If my input is
<School>
<Class>
<Student name="" class="" rollno="" />
<Teacher name="" qualification="" Employeeno="" />
</Class>
</School>
The expected output would be:
School/Class/Student/name, School/Class/Student/class, School/Class/Student/rollno,
School/Class/Teacher/name, School/Class/Teacher/qualification, School/Class/Teacher/Employeeno
An example does not always embody a rule. Assuming you want a row for each element that has any attributes, no matter where in the document it is, and a column for each attribute of an element, try:
Edit:
This is an improved version, corrected to work properly with nested elements.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="*">
<xsl:param name="path" />
<xsl:variable name="newpath" select="concat($path, '/', name())" />
<xsl:apply-templates select="#*">
<xsl:with-param name="path" select="$newpath"/>
</xsl:apply-templates>
<xsl:if test="#*">
<xsl:text>
</xsl:text>
</xsl:if>
<xsl:apply-templates select="*">
<xsl:with-param name="path" select="$newpath"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="#*">
<xsl:param name="path" />
<xsl:value-of select="substring(concat($path, '/', name()), 2)"/>
<xsl:if test="position()!=last()">
<xsl:text>, </xsl:text>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied to the following test input:
<Root>
<Parent parent="1" parent2="1b">
<Son son="11" son2="11b"/>
<Daughter daughter="12" daughter2="12b">
<Grandson grandson="121" grandson2="121b"/>
<Granddaughter granddaughter="122" granddaughter2="122b"/>
</Daughter>
<Sibling/>
</Parent>
</Root>
the result is:
Root/Parent/parent, Root/Parent/parent2
Root/Parent/Son/son, Root/Parent/Son/son2
Root/Parent/Daughter/daughter, Root/Parent/Daughter/daughter2
Root/Parent/Daughter/Grandson/grandson, Root/Parent/Daughter/Grandson/grandson2
Root/Parent/Daughter/Granddaughter/granddaughter, Root/Parent/Daughter/Granddaughter/granddaughter2
Note that the number of columns in each row can vary - this is often unacceptable in a CSV document.
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.
Given siblings, some of which are <row> elements and some not, like this,
<h />
<row id='v' />
<a />
<b />
<row id='w' />
<d />
<row id='x' />
<row id='y' />
<f />
<r />
<row id='z' />
using xslt 1.0, I need to process them in order but to group the non-row ones together as I go, like this,
<notRow>
<h />
</notRow>
<row id='v' />
<notRow>
<a />
<b />
</notRow>
<row id='w' />
<notRow>
<d />
<row id='x' />
<row id='y' />
<notRow>
<f />
<r />
</notRow>
<row id='z' />
The first and last may or may not be <row> elements.
How?
It can be as short and simple as this (no need of calling templates several times, xsl:for-each, xsl:if). Here is the complete transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing" match="*/*[not(self::row)]"
use="concat(generate-id(..), '+',
generate-id(preceding-sibling::row[1])
)"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template priority="2" match=
"*/*[not(self::row)
and
(preceding-sibling::*[1][self::row]
or not(preceding-sibling::*)
)]">
<notRow>
<xsl:copy-of select=
"key('kFollowing', concat(generate-id(..), '+',
generate-id(preceding-sibling::row[1])
))"/>
</notRow>
</xsl:template>
<xsl:template match="*/*[not(self::row)]"/>
</xsl:stylesheet>
When this transformation is applied on the provided XML (wrapped into a single top element to make it well-formed):
<t>
<h />
<row id='v' />
<a />
<b />
<row id='w' />
<d />
<row id='x' />
<row id='y' />
<f />
<r />
<row id='z' />
</t>
the wanted, correct result is produced:
<t>
<notRow>
<h/>
</notRow>
<row id="v"/>
<notRow>
<a/>
<b/>
</notRow>
<row id="w"/>
<notRow>
<d/>
</notRow>
<row id="x"/>
<row id="y"/>
<notRow>
<f/>
<r/>
</notRow>
<row id="z"/>
</t>
Update:
The OP has expressed an additional requirement that nodes need be processed by matching templates -- not just copied.
This requires only minimal change:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kFollowing" match="*/*[not(self::row)]"
use="concat(generate-id(..), '+',
generate-id(preceding-sibling::row[1])
)"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template priority="2" match=
"*/*[not(self::row)
and
(preceding-sibling::*[1][self::row]
or not(preceding-sibling::*)
)]">
<notRow>
<xsl:apply-templates mode="group" select=
"key('kFollowing', concat(generate-id(..), '+',
generate-id(preceding-sibling::row[1])
))"/>
</notRow>
</xsl:template>
<!-- This template can be replaced with whatever processing needed -->
<xsl:template match="*" mode="group">
<xsl:copy-of select="."/>
</xsl:template>
<xsl:template match="*/*[not(self::row)]"/>
</xsl:stylesheet>
The template that operates in mode "group" should be substituted with template(s) that implement the exact wanted processing. In this case it copies the matched element -- but in the real application any wanted processing would go here.
You might be able to do a trick with a key to group each non-row element by its preceding row (if there is one), or its parent element if not:
<xsl:key name="elementsFollowingRow"
match="*[not(self::row)]"
use="generate-id( (.. | preceding-sibling::row )[last()])" />
and define a named template to put in a notRow if the current element has any associated elements according to the key
<xsl:template name="addNotRow">
<xsl:if test="key('elementsFollowingRow', generate-id())">
<notRow>
<xsl:copy-of select="key('elementsFollowingRow', generate-id())" />
</notRow>
</xsl:if>
</xsl:template>
Then in the template where you're matching the parent element (the one that contains all these row and non-row elements you can do
<xsl:call-template name="addNotRow" />
<xsl:for-each select="row">
<xsl:copy-of select="." />
<xsl:call-template name="addNotRow" />
</xsl:for-each>
The first call-template outside the for-each will deal with any notRow that is required before the first row, and the call inside the for-each will put in any notRow required after the row in question.
This isn't pretty, but it works.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="t">
<xsl:if test="row[1]/preceding-sibling::*">
<notRow>
<xsl:for-each select="row[1]/preceding-sibling::*" >
<xsl:copy />
</xsl:for-each>
</notRow>
</xsl:if>
<xsl:for-each select="row">
<xsl:copy-of select="."/>
<xsl:if test="following-sibling::row[1]/preceding-sibling::*[generate-id(preceding-sibling::row[1])=generate-id(current())]">
<notRow>
<xsl:for-each select="following-sibling::row[1]/preceding-sibling::*[generate-id(preceding-sibling::row[1])=generate-id(current())]">
<xsl:copy />
</xsl:for-each>
</notRow>
</xsl:if>
</xsl:for-each>
<xsl:if test="row[last()]/following-sibling::*">
<notRow>
<xsl:for-each select="row[last()]/following-sibling::*" >
<xsl:copy />
</xsl:for-each>
</notRow>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
On this XML source
<t>
<h />
<i />
<row id='v' />
<a />
<b />
<row id='w' />
<d />
<row id='x' />
<row id='y' />
<f />
<r />
<row id='z' />
<i />
</t>
it returns the correct result:
<notRow>
<h/>
<i/>
</notRow>
<row id="v"/>
<notRow>
<a/>
<b/>
</notRow>
<row id="w"/>
<notRow>
<d/>
</notRow>
<row id="x"/>
<row id="y"/>
<notRow>
<f/>
<r/>
</notRow>
<row id="z"/>
<notRow>
<i/>
</notRow>
but it does seem there should be something simpler.
I have the following input XML file:
<root>
<a>
<b>1</b>
</a>
<c>
<d>
<e>2</e>
<f>3</f> or <e>3</e>
</d>
<g h="4"/>
<i>
<j>
<k>
<l m="5" n="6" o="7" />
<l m="8" n="9" o="0" />
</k>
</j>
</i>
</c>
</root>
I would like to use XSLT to transform it into the follow outputs:
OUTPUT 1
<root>
<row b="1" e="2" f="3" h="4" m="5" n="6" o="7" />
<row b="1" e="2" f="3" h="4" m="8" n="9" o="0" />
<root>
OUTPUT 2
<root>
<row b="1" e="2" h="4" m="5" n="6" o="7" />
<row b="1" e="2" h="4" m="8" n="9" o="0" />
<row b="1" e="3" h="4" m="5" n="6" o="7" />
<row b="1" e="3" h="4" m="8" n="9" o="0" />
<root>
Can anyone help my XSLT isn't very strong. Thanks.
It will be easier if you let the occurrence of <e> determine the outer loop constructing your <row>s and have a inner loop iterating over all <l>s.
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>
<xsl:template match="root">
<root>
<xsl:apply-templates />
</root>
</xsl:template>
<xsl:template match="e">
<!-- store values of 'e' and 'f' in params -->
<xsl:param name="value_of_e" select="." />
<xsl:param name="value_of_f" select="ancestor::d[1]/f" />
<!-- iterate over all 'l's -->
<xsl:for-each select="//l">
<xsl:element name="row">
<xsl:attribute name="b">
<xsl:value-of select="//b" />
</xsl:attribute>
<xsl:attribute name="e">
<xsl:value-of select="$value_of_e" />
</xsl:attribute>
<!-- only include 'f' if it has a value -->
<xsl:if test="$value_of_f != ''">
<xsl:attribute name="f">
<xsl:value-of select="$value_of_f" />
</xsl:attribute>
</xsl:if>
<xsl:attribute name="h">
<xsl:value-of select="ancestor::c[1]/g/#h" />
</xsl:attribute>
<xsl:attribute name="m">
<xsl:value-of select="./#m" />
</xsl:attribute>
<xsl:attribute name="n">
<xsl:value-of select="./#n" />
</xsl:attribute>
<xsl:attribute name="o">
<xsl:value-of select="./#o" />
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template match="b" />
<xsl:template match="f" />
<xsl:template match="g" />
</xsl:stylesheet>