Use values of the elements as XPath expression to extract another elements using XSLT 1.0 - xslt-1.0

Suppose there is such a document:
<?xml version="1.0" encoding="utf-8" ?>
<root>
<paths>
<item>
<name>username</name>
<path>user/name</path>
</item>
<item>
<name>useremail</name>
<path>concat(user/name,': ',user/email)</path>
</item>
</paths>
<user>
<name>John</name>
<email>john#gmail.com</email>
</user>
</root>
the output required to obtain such a document:
<?xml version="1.0" encoding="utf-8"?>
<results>
<item name="username" value="John"/>
<item name="useremail" value="John: john#gmail.com"/>
</results>
The point is that the number of path elements can vary, and their values can refer to different elements of the source document (including using functions).
Is there a way to use the values of some elements as an expression of the search for other?

Related

Grouping XSLT elements by value (XSLT 1.0)

I am having a really hard time trying to group different elements by a common value using XSLT 1.0.
Using the following XML:
<root>
<segment>
<id>ABCD123</id>
</segment>
<segment>
<contact>
<field1>ABCD123</field1>
<field2>(111)345-7890</field2>
</contact>
</segment>
<segment>
<details>
<field1>ABCD123</field1>
<field5>More Details for ABCD123</field5>
</details>
</segment>
<segment>
<id>XZX098</id>
</segment>
<segment>
<contact>
<field1>XZX098</field1>
<field2>(111)443-9999</field2>
</contact>
</segment>
<segment>
<details>
<field1>XZX098</field1>
<field5>More Details for XZX098</field5>
</details>
</segment>
</root>
Transform into this:
<File>
<Record>
<id>ABCD123</id>
<phone>(111)345-7890</phone>
<details>More Details for ABCD123</details>
</Record>
<Record>
<id>XZX098</id>
<phone>(111)443-9999</phone>
<details>More Details for XZX098</details>
</Record>
</File>
I'm trying to group records by the 'id', and then get the contact, and details information that matches that 'id'.
Any help is greatly appreciated.
I don't see any grouping per se required here - just a simple lookup of cross-referenced data:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="contact-details" match="contact|details" use="field1" />
<xsl:template match="root">
<File>
<xsl:for-each select="segment/id">
<Record>
<xsl:copy-of select="."/>
<phone>
<xsl:value-of select="key('contact-details', .)/field2"/>
</phone>
<details>
<xsl:value-of select="key('contact-details', .)/field5"/>
</details>
</Record>
</xsl:for-each>
</File>
</xsl:template>
</xsl:stylesheet>

XSLT 1.0 grouping on one or multiple levels

Today's challenge was grouping in XSLT 1.0. Found out there are something called keys and the Muenchian grouping.
Input XML:
<Items>
<Item>
<ID>1</ID>
<Name>A</Name>
<Country>Sweden</Country>
<Region>Småland</Region>
</Item>
<Item>
<ID>2</ID>
<Name>B</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
<Item>
<ID>3</ID>
<Name>C</Name>
<Country>USA</Country>
<Region>Alaska</Region>
</Item>
<Item>
<ID>4</ID>
<Name>D</Name>
<Country>USA</Country>
<Region>Texas</Region>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
</Items>
I need to make thins XML into a better structure, and from this sample XML I't like to get items structured by country and region. Below is wanted result where country and region gets sorted as well:
<Items>
<Country Name="Sweden">
<Region Name="Norrland">
<Item>
<ID>2</ID>
<Name>B</Name>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
</Item>
</Region>
<Region Name="Småland">
<Item>
<ID>1</ID>
<Name>A</Name>
</Item>
</Region>
</Country>
<Country Name="USA">
<Region Name="Alaska">
<Item>
<ID>3</ID>
<Name>C</Name>
</Item>
</Region>
<Region Name="Texas">
<Item>
<ID>4</ID>
<Name>D</Name>
</Item>
</Region>
</Country>
</Items>
EDIT:
I also want to make sure regions end up in their own country, even if there are duplicates. I edited the answer accordingly.
Also, I'd like to hint about xsltfiddle.liberty-development.net as an easy way of doing trial-and-error XSLT development...
Inspired by this article, I found a neat solution to this problem:
I have included comments for using it for single or double grouping, see comments in the code. Notice how I use first key (index) as input to the secon for-each loop:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="country" match="Item" use="Country" />
<xsl:key name="region" match="Item" use="concat(Region, '|', Country)" />
<xsl:template match="/Items">
<Items>
<xsl:for-each select="Item[generate-id(.) = generate-id(key('country', Country))]">
<xsl:sort select="Country" />
<xsl:variable name="_country" select="Country" />
<xsl:element name="Country">
<xsl:attribute name="Name"><xsl:value-of select="$_country" /></xsl:attribute>
<!-- single level grouping -->
<!--<xsl:apply-templates select="key('country', Country)" />-->
<!-- double grouping -->
<!-- START -->
<xsl:for-each select="key('country', Country)[generate-id(.) = generate-id(key('region', concat(Region, '|', Country)))]">
<xsl:sort select="Region" />
<xsl:variable name="_region" select="Region" />
<xsl:element name="Region">
<xsl:attribute name="Name"><xsl:value-of select="$_region" /></xsl:attribute>
<xsl:apply-templates select="key('region', concat(Region, '|', Country))" />
</xsl:element>
</xsl:for-each>
<!-- END -->
</xsl:element>
</xsl:for-each>
</Items>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="Item">
<xsl:element name="ID"><xsl:value-of select="ID" /></xsl:element>
<xsl:element name="Name"><xsl:value-of select="Name" /></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

Filter keys out of response

I have an xslt sheet in which I have 2 response objects. $response1 contains a list of ids something like:
<response>
<idlist>
<id>1</id>
<id>2</id>
</idlist>
</response>
And $response2 contains a number of objects:
<response2>
<obj id="1" name="obj1"/>
<obj id="2" name="obj2"/>
<obj id="3" name="obj3"/>
<obj id="4" name="obj4"/>
</response2>
I want to make a copy of response2 but filtering out any objects where the id matches thos contained in response 1
<xsl:variable name="copy">
<xsl:copy-of select="$response2/*[not contains($response1, id)]"/>
</xsl:variable>
any ideas greatly appreciated
C
Given a well-formed input such as:
<root>
<response>
<idlist>
<id>1</id>
<id>2</id>
</idlist>
</response>
<response2>
<obj id="1" name="obj1"/>
<obj id="2" name="obj2"/>
<obj id="3" name="obj3"/>
<obj id="4" name="obj4"/>
</response2>
</root>
the following stylesheet:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root">
<xsl:variable name="ids" select="response/idlist/id" />
<output>
<xsl:copy-of select="response2/obj[not(#id=$ids)]"/>
</output>
</xsl:template>
</xsl:stylesheet>
will return:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<obj id="3" name="obj3"/>
<obj id="4" name="obj4"/>
</output>
A better solution is to use a key to link the nodes by matching id:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="id" match="id" use="." />
<xsl:template match="/root">
<output>
<xsl:copy-of select="response2/obj[not(key('id', #id))]"/>
</output>
</xsl:template>
</xsl:stylesheet>

How to return a list of objects from soap webservice with rails

I'm using Savon to access a soap webservice, but I can only ever return a single result, when what I want is an array of results.
Here is my call:
response = client.call(:get_events, message: { username: "xxxx", password: "xxxxxxxx", company_code: "12TCE" })
I want 'response' to return all records with the company_code of "12TCE" and for me to be able to output them all doing something like:
response.to_hash[:get_events_response].each do |a|
a[:return][:item][:name]
end
How can I return all records and output as desired?
UPDATE 1:
This is the link to the wsdl: http://www.brrmedia.co.uk/webservices/event/index.php?wsdl
This is the response I get:
HTTPI POST request to www.brrmedia.co.uk (httpclient)
SOAP response (status 200)
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="http://www.brrmedia.co.uk/webservices/event" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getEventsResponse xmlns:ns1="http://www.brrmedia.co.uk/webservices/event">
<return xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:objEvent[1]">
<item xsi:type="tns:objEvent">
<id xsi:type="xsd:int">119466</id>
<name xsi:type="xsd:string">blur Group - 2000 projects milestone</name>
<summary xsi:type="xsd:string"/>
<location xsi:type="xsd:string"/>
<date xsi:type="xsd:string">2013-12-17 11:30</date>
<link xsi:type="xsd:string">http://www.brrmedia.co.uk/event/119466/partner/brrsoap</link>
<company xsi:type="tns:objCompany">
<name xsi:type="xsd:string">blur Group</name>
<codes xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:objCompanyCode[1]">
<item xsi:type="tns:objCompanyCode">
<code xsi:type="xsd:string">BLUR</code>
<collection xsi:type="xsd:string">London Stock Exchange (AIM)</collection>
</item>
</codes>
<website xsi:type="xsd:string">http://www.blurgroup.com/</website>
<category xsi:type="xsd:string">Technology</category>
</company>
<presenter xsi:type="tns:objPresenter">
<name xsi:type="xsd:string"> Philip Letts</name>
<image xsi:type="xsd:string">http://www.brrmedia.co.uk/getimage/id/31215</image>
</presenter>
<media xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="tns:objMediaItem[3]">
<item xsi:type="tns:objMediaItem">
<src xsi:type="xsd:string">http://s3-us-west-2.amazonaws.com/brr-streamguys/files/BLUR/blur20131217.pdf</src>
<duration xsi:type="xsd:string">00:00:00</duration>
<filesize xsi:type="xsd:string">380</filesize>
<media_type xsi:type="xsd:string">pdf</media_type>
</item>
<item xsi:type="tns:objMediaItem">
<src xsi:type="xsd:string">http://s3-us-west-2.amazonaws.com/brr-streamguys/files/BLUR/BLUR20131217</src>
<duration xsi:nil="true" xsi:type="xsd:string"/>
<filesize xsi:nil="true" xsi:type="xsd:string"/>
<media_type xsi:type="xsd:string">presimages</media_type>
</item>
<item xsi:type="tns:objMediaItem">
<src xsi:type="xsd:string">http://s3-us-west-2.amazonaws.com/brr-streamguys/files/BLUR/BLUR20131217editv1.mp3</src>
<duration xsi:type="xsd:string">00:07:54</duration>
<filesize xsi:type="xsd:string">5557</filesize>
<media_type xsi:type="xsd:string">audio</media_type>
</item>
</media>
<type xsi:type="xsd:string">audio</type>
<height xsi:type="xsd:int">900</height>
<width xsi:type="xsd:int">680</width>
</item>
</return>
</ns1:getEventsResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

Filter xml in .Net with param

I am trying to filter an xml file in a .Net application I am developing. Some sample xml below, obviously not the proper xml, but near enough :-)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Row A1="1" A2="AMS">
<Name>Ashley</Name>
<Team>Team B</Team>
<Date>3/25/2012</Date>
<Value>511681.15</Value>
</Row>
<Row A1="2" A2="AMS">
<Name>Kylie</Name>
<Team>Team A</Team>
<Date>9/28/2010</Date>
<Value>408438.47</Value>
</Row>
<Row A1="3" A2="AMS">
<Name>Gianna</Name>
<Team>Team B</Team>
<Date>40004</Date>
<Value>109709.22</Value>
</Row>
<Row A1="4" A2="AMS">
<Name>Chase</Name>
<Team>Team F</Team>
<Date>40152</Date>
<Value>279018.79</Value>
</Row>
The stylesheet has a param that is set by XsltArgumentList in the application. The param is passed into the stylesheet, but does not filter the xml. I have tried using the ms node-set and exsl node-set but only get the top level root returned. Stylesheet below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes" encoding="UTF-8" />
<xsl:decimal-format name="NN" NaN="0" />
<xsl:param name="Filter" />
<xsl:template match="/">
<Root>
<xsl:for-each select="exsl:node-set($Filter)">
<Row>
<xsl:attribute name="A1">
<xsl:value-of select="#A1" />
</xsl:attribute>
<xsl:attribute name="A2">
<xsl:value-of select="#A2" />
</xsl:attribute>
<Team>
<xsl:value-of select="Team"/>
</Team>
</Row>
</xsl:for-each>
</Root>
</xsl:template>
The filter i am trying to pass could be using any of the combination of the xml elements. The filter is am currently attempting is below
<xsl:param name="Filter" select="//Row[Team='Team A']" />
But this is just returns
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Row A1="" A2="">
<Team></Team>
</Row>
</Root>
Any help or pointers would be appreciated!
Thanks
This will not woke like you tried it, because you can't have a xpath expression in an XLST parameter or variable. You can use variable or parameter in please of const values.
Therefore you can use something like
<xsl:param name="teamFilter" select="'Team A'" />
...
<xsl:for-each select="//Row[Team='$teamFilter']">
Or if you have to filter for different nodes you may try:
<xsl:param name="filterValue" select="'Team A'" />
<xsl:param name="filterNode" select="'Team'" />
...
<xsl:for-each select="//Row[*[name()= $filterNode and . =$filterValue]]">
Other alternatives would be:
Patch the XSLT before loading it
Use xpath iterator with XPathExpression from .NET.