I am pretty new in XSLT and I am stuck at once place.
I have one input xml file as below
<Roots>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>1</Count>
</Root>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>10</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>userxyz</Value2>
<Count>10</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>usermnp</Value2>
<Count>10</Count>
</Root>
<Root>
<Value1>Links3</Value1>
<Value2>user123</Value2>
<Count>5</Count>
</Root>
...
...
...
</Roots>
Now, I want to sum the count value for similar values of Value1 and Value2 so that it looks like below:
<Roots>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>11</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>userxyz</Value2>
<Count>20</Count>
</Root>
<Root>
<Value1>Links3</Value1>
<Value2>user123</Value2>
<Count>5</Count>
</Root>
</Roots>
I have been trying a lot to transform this xml and here is my code:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs" >
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Roots">
<Roots>
<Root>
<xsl:for-each-group select="Root" group-by=
"concat(Value1, '+', Value2)">
<xsl:copy-of select=
"current-group()[1]/*[starts-with(name(),'key')]"/>
<Count>
<xsl:value-of select="sum(current-group()/Count)"/>
</Count>
</xsl:for-each-group>
</Root>
</Roots>
</xsl:template>
</xsl:stylesheet>
I know I am doing wrong and could be lot easier to the expert but I really need some help and direction.
Thanks
Your XSLT was close, but move the <Root> element definition into the xsl:for-each-group. I couldn't make sense of your xsl:copy-of..., so I just reproduced the elements one-by-one. So use this simple XSLT-2.0 stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="/Roots">
<xsl:copy>
<xsl:apply-templates select="#*" />
<xsl:for-each-group select="Root" group-by="Value1">
<Root>
<Value1><xsl:value-of select="current-grouping-key()" /></Value1>
<Value2><xsl:value-of select="current-group()[1]/Value2" /></Value2>
<Count><xsl:value-of select="sum(current-group()/Count)" /></Count>
</Root>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
It's output is:
<Roots>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>11</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>userxyz</Value2>
<Count>20</Count>
</Root>
<Root>
<Value1>Links3</Value1>
<Value2>user123</Value2>
<Count>5</Count>
</Root>
</Roots>
Right now, your grouping concat(Value1, '+', Value2) will not yield three nodes since nodes at Links2 maintains two different value2 node values. Also, your identity transform template is redundant since you rewrite the XML tree directly from root, Roots.
One Grouping (only Value1)
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" exclude-result-prefixes="xs">
<xsl:output omit-xml-declaration="yes" indent="yes" />
<xsl:template match="Roots">
<Roots>
<xsl:for-each-group select="Root" group-by="Value1">
<Root>
<xsl:copy-of select="Value1|Value2"/>
<Count>
<xsl:value-of select="sum(current-group()/Count)" />
</Count>
</Root>
</xsl:for-each-group>
</Roots>
</xsl:template>
</xsl:stylesheet>
Returns with first value2 returned:
<Roots>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>11</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>userxyz</Value2>
<Count>20</Count>
</Root>
<Root>
<Value1>Links3</Value1>
<Value2>user123</Value2>
<Count>5</Count>
</Root>
</Roots>
Two Groupings (Value1 and Value2)
Keeping original grouping:
<xsl:for-each-group select="Root" group-by="concat(Value1, '+', Value2)">
Returns four nodes
<Roots>
<Root>
<Value1>Links1</Value1>
<Value2>notadmin</Value2>
<Count>11</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>userxyz</Value2>
<Count>10</Count>
</Root>
<Root>
<Value1>Links2</Value1>
<Value2>usermnp</Value2>
<Count>10</Count>
</Root>
<Root>
<Value1>Links3</Value1>
<Value2>user123</Value2>
<Count>5</Count>
</Root>
</Roots>
Related
I am trying to obtain a list of all the elements with values that aren't in the (Line 1, Line2), and then insert them into the tags similar to the test.
Right now I can retrieve all the elements, but I'm having trouble restricting this to just my desired values. And then I'm unsure how to match and do a for each on elements outside my match criteria. Any advice would be greatly appreciated!
Given the Following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<Line1>Element1</Line1>
<Line2>Element2</Line2>
</Header>
<ElementControl>
<Update>
<Element>test</Element>
</Update>
</ElementControl>
<Member>
<Identifier>123456789</Identifier>
<Contact>
<Person>
<Gender>MALE</Gender>
<Title>Mr</Title>
<Name>JOHN DOE</Name>
</Person>
<HomePhone/>
<eMailAddress/>
<ContactAddresses>
<Address>
<AddressType>POS</AddressType>
<Line1>100 Fake Street</Line1>
<Line2/>
<Line3/>
<Line4/>
<Suburb>Jupiter</Suburb>
<State>OTH</State>
<PostCode>9999</PostCode>
<Country>AUS</Country>
</Address>
</ContactAddresses>
</Contact>
</Member>
</Request>
Current XSL for getting elements
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()">
<xsl:for-each select="node()[text() != '']">
<xsl:value-of select="local-name()"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:apply-templates select="node()"/>
</xsl:template>
</xsl:stylesheet>
My WIP xml for inserting the result xml tags is below. I'm unsure how to insert the results of the above xsl into this,
<xsl:template match="Element">
<xsl:copy-of select="."/>
<Element>Value1</Element>
</xsl:template>
And ultimate desired output:
<?xml version="1.0" encoding="UTF-8"?>
<Request>
<Header>
<Line1>Element1</Line1>
<Line2>Element2</Line2>
</Header>
<ElementControl>
<Update>
<Element>Identifier</Element>
<Element>Gender</Element>
<Element>Title</Element>
<Element>Name</Element>
<Element>AddressType</Element>
<Element>Line1</Element>
<Element>Suburb</Element>
<Element>State</Element>
<Element>PostCode</Element>
<Element>Country</Element>
</Update>
</ElementControl>
<Member>
<Identifier>123456789</Identifier>
<Contact>
<Person>
<Gender>MALE</Gender>
<Title>Mr</Title>
<Name>JOHN DOE</Name>
</Person>
<HomePhone/>
<eMailAddress/>
<ContactAddresses>
<Address>
<AddressType>POS</AddressType>
<Line1>100 Fake Street</Line1>
<Line2/>
<Line3/>
<Line4/>
<Suburb>Jupiter</Suburb>
<State>OTH</State>
<PostCode>9999</PostCode>
<Country>AUS</Country>
</Address>
</ContactAddresses>
</Contact>
</Member>
</Request>
I would change the current template to use mode attribute, so it is only used in specific cases, rather than matching all elements. You should also change it to output elements, not text, like so:
<xsl:template match="node()" mode="copy">
<xsl:for-each select=".//node()[text() != '']">
<Element>
<xsl:value-of select="local-name()"/>
</Element>
</xsl:for-each>
</xsl:template>
Then you can call it like this....
<xsl:template match="ElementControl/Update">
<xsl:apply-templates select="../../Member" mode="copy" />
</xsl:template>
Try this XSLT. Note the use of the identity template to copy all other existing elements unchanged
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()" mode="copy">
<xsl:for-each select=".//node()[text() != '']">
<Element>
<xsl:value-of select="local-name()"/>
</Element>
</xsl:for-each>
</xsl:template>
<xsl:template match="ElementControl/Update">
<xsl:apply-templates select="../../Member" mode="copy" />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I have a source xml and from this source xml I like to select the nodes given by a path e.g. /shiporder/item/title and /shiporder/shipto/name from the sample source:
<?xml version="1.0" encoding="utf-16"?>
<shiporder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" orderid="orderid1">
<orderperson>orderperson1</orderperson>
<shipto>
<name>name1</name>
</shipto>
<item>
<title>foo</title>
</item>
<item>
<title>bar</title>
</item>
</shiporder>
And I like to transform those nodes to certain target tree e.g. each /shiporder/item/title from the source xml should copied to root/Customer/Name/Title in the target xml tree. So my idea was to generate for each level in the source path a template and call this template from the preceding level:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:func="http://www.functx.com">
<xsl:output method="xml" indent="yes" encoding="UTF-8"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="/shiporder"/>
</root>
</xsl:template>
<xsl:template match="/shiporder">
<Customer>
<xsl:apply-templates select="item"/>
<xsl:apply-templates select="shipto"/>
</Customer>
</xsl:template>
<xsl:template match="/shiporder/item">
<Name>
<xsl:apply-templates select="title"/>
</Name>
</xsl:template>
<xsl:template match="/shiporder/shipto">
<Address>
<xsl:apply-templates select="name"/>
</Address>
</xsl:template>
<xsl:template match="/shiporder/item/title">
<Title>
<xsl:value-of select="text()" />
</Title>
</xsl:template>
<xsl:template match="/shiporder/shipto/name">
<Street>
<xsl:value-of select="text()" />
</Street>
</xsl:template>
</xsl:stylesheet>
There for I get a huge stylesheet if I have a huge list of source-paths. Has some one a more feasible idea to reach the target?
I have this problem:
<?xml version="1.0" encoding="utf-16"?>
<root>
<container>
<Facility count="1" normalied="Mauritius Branch Campus">Mauritius Branch Campus</Facility>
<Facility count="1" relevance="0.2">Mauritius Branch Campus</Facility>
<Country count="1" relevance="0.8">Mauritius</Country>
<Country count="1" normalized="Mauritius">Mauritius</Country>
</container>
</root>
What I would like to achieve is this:
<?xml version="1.0" encoding="utf-16"?>
<root>
<container>
<Facility count="1" relevance="0.2" normalied="Mauritius Branch Campus">Mauritius Branch Campus</Facility>
<Country count="1" relevance="0.8" normalized="Mauritius">Mauritius</Country>
</container>
</root>
In the example e reported only elements like <Facility> and <Country> but the may be more, the same as the type of attributes.
Is there a way to achieve this via XSLT ?
Thanks in advance.
R
Using an XSLT 2.0 processor like Saxon 9 you can use for-each-group, the following code first groups by node-name(.) (assuming you only want to group all Country and all Facility together, but not elements of a different name) and then by string contents:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="container">
<xsl:copy>
<xsl:for-each-group select="*" group-by="node-name(.)">
<xsl:for-each-group select="current-group()" group-by=".">
<xsl:copy>
<xsl:copy-of select="current-group()/#*"/>
</xsl:copy>
</xsl:for-each-group>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
Online demo at http://xsltransform.net/pPzifp3.
i have input xml as
<content>
<date>
<day>14</day>
<month>06</month>
<year>2012</year>
</date>
</content>
want it to be converted into
<content>
<date>2012-06-14T00:00:00</date>
</content>
xslt used
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="content">
<content>
<xsl:apply-templates select="date"/>
</content>
</xsl:template>
<xsl:template match="date">
<date>
<xsl:variable name="year" select="year"/>
<xsl:variable name="month" select="month"/>
<xsl:variable name="day" select="day"/>
<xsl:value-of select="$year" />-<xsl:value-of select="$month"/>-
<xsl:value-of select="$day"/>
</date>
</xsl:template>
</xsl:stylesheet>
i want date in YYYYMMDDTHH:MM:SS format as example 2012-06-15T02:52:37 how can i get it.Which function in xslt "1.0" pics the strings and take the format pattern as such. Can aynone please help me in getting the above format.
This 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:template match="/*">
<content>
<xsl:apply-templates/>
</content>
</xsl:template>
<xsl:template match="date">
<date>
<xsl:value-of select="concat(year, '-', month, '-', day, 'T00:00:00')"/>
</date>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<content>
<date>
<day>14</day>
<month>06</month>
<year>2012</year>
</date>
</content>
produces the wanted, correct result:
<content>
<date>2012-06-14T00:00:00</date>
</content>
Using the XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
<xsl:output method="xml"/>
<xsl:template match="/">
<records>
<record>
<!-- Group record by bigID, for further processing -->
<xsl:for-each-group select="records/record" group-by="bigID">
<xsl:sort select="bigID"/>
<xsl:for-each select="current-group()">
<!-- Create new combined record -->
<bigID>
<!-- <xsl:value-of select="."/> -->
<xsl:for-each select=".">
<xsl:value-of select="bigID"/>
</xsl:for-each>
</bigID>
<text>
<xsl:value-of select="text"/>
</text>
</xsl:for-each>
</xsl:for-each-group>
</record>
</records>
</xsl:template>
</xsl:stylesheet>
I'm trying to change:
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
<bigID>123</bigID>
<text>Contains text for 123</text>
<bigID>456</bigID>
<text>Some 456 text</text>
<bigID>123</bigID>
<text>More 123 text</text>
<bigID>123</bigID>
<text>Yet more 123 text</text>
</record>
</records>
into:
<?xml version="1.0" encoding="UTF-8"?>
<records>
<record>
<bigID>123
<text>Contains text for 123</text>
<text>More 123 text</text>
<text>Yet more 123 text</text>
</bigID>
<bigID>456
<text>Some 456 text</text>
</bigID>
</record>
</records>
Right now, I'm just listing the grouped <bigID>s, individually. I'm missing the step after grouping, where I combine the grouped <bigID> nodes. My suspicion is that I need to use the "key" function somehow, but I'm not sure.
Thanks for any help.
Here is the wanted XSLT 2.0 transformation:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kTextforId" match="text"
use="preceding-sibling::bigID[1]"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="record">
<record>
<xsl:for-each-group select="bigID" group-by=".">
<bigID>
<xsl:sequence select="current-grouping-key()"/>
<xsl:copy-of select=
"key('kTextforId', current-grouping-key())"/>
</bigID>
</xsl:for-each-group>
</record>
</xsl:template>
</xsl:stylesheet>
When performed on the provided XML document, the wanted result is produced.
In XSLT 2.0 you don't need keys for grouping.
Since you are just copying the text elements in the group, the inner for-each can be removed.
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<records>
<record>
<xsl:for-each-group select="records/record/bigID" group-by=".">
<xsl:sort select="." data-type="number" />
<bigID>
<xsl:value-of select="." />
<xsl:copy-of select="current-group()/following-sibling::text[1]" />
</bigID>
</xsl:for-each-group>
</record>
</records>
</xsl:template>
</xsl:stylesheet>
If you instead wanted to output the bigID elements followed by their text elements, then my loop would be replaced by the following.
<xsl:for-each-group select="records/record/bigID" group-by=".">
<xsl:sort select="." data-type="number" />
<xsl:copy-of select="." />
<xsl:copy-of select="current-group()/following-sibling::text[1]" />
</xsl:for-each-group>