Create list of nodes by copying from another lists - xslt

I have a XSLT template where I need to merge two lists of nodes conditionally. I have the following two XML fragments:
<vo>
<field name="userLoginName" nameLDAP="uid" type="String"/>
<field name="displayName" nameLDAP="displayName" type="String"/>
<field name="firstName" nameLDAP="givenName" type="String"/>
<field name="lastName" nameLDAP="sn" type="String"/>
<field name="mail" nameLDAP="mail" type="String"/>
<field name="userPassword" nameLDAP="userPassword" type="String" hidden="true"/>
<field name="center" nameLDAP="center" type="String"/>
</vo>
<input>
<field name="userPassword"/>
<field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="byte[]"/>
</input>
I want to create a xml fragment that will have the same nodeas as input/field. For each one of those nodes I want to check if there is a node with the same name in the list vo/field. If there is, then it should be copied to the new list. Otherwise, it should copy the same node we are iterating over. In this case the output should be something like this:
<field name="userPassword" nameLDAP="userPassword" type="String" hidden="true"/>
<field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="String"/>
So far I've the following transformation:
<xsl:variable name="fields" select="vo/field" />
<xsl:variable name='allParameters'>
<xsl:for-each select="input/field">
<xsl:variable name="inputFieldName" select="#name"/>
<xsl:choose>
<xsl:when test="$fields[#name = $inputFieldName]">
<xsl:copy-of select="$fields[#name = $inputFieldName]"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:variable>
<xsl:message>Parameters <xsl:copy-of select="$allParameters" /> </xsl:message>
<xsl:for-each select="$allParameters">
<xsl:message>Parameter <xsl:value-of select="#name" /> found !!! </xsl:message>
</xsl:for-each>
The output is
Parameters <field name="userPassword" nameLDAP="userPassword" type="String" hidden="true"/><field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="byte[]"/>
Parameter found !!!
The first xsl:message seems to show that the copy worked fine, but when I try to iterate over it, it is obviously not working(there is only one message "Parameter found" and it is not showing the parameter name). What am I missing?

I think you want something like:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:key name="vo" match="vo/field" use="#name" />
<xsl:template match="/">
<xsl:for-each select="root/input/field">
<xsl:choose>
<xsl:when test="key('vo', #name)">
<xsl:copy-of select="key('vo', #name)"/>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Applied to a wel-formed input:
<root>
<vo>
<field name="userLoginName" nameLDAP="uid" type="String"/>
<field name="displayName" nameLDAP="displayName" type="String"/>
<field name="firstName" nameLDAP="givenName" type="String"/>
<field name="lastName" nameLDAP="sn" type="String"/>
<field name="mail" nameLDAP="mail" type="String"/>
<field name="userPassword" nameLDAP="userPassword" type="String" hidden="true"/>
<field name="center" nameLDAP="center" type="String"/>
</vo>
<input>
<field name="userPassword"/>
<field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="byte[]"/>
</input>
</root>
results in:
<field name="userPassword" nameLDAP="userPassword" type="String" hidden="true"/>
<field name="oldPasswordInQuotes" nameLDAP="unicodePwd" type="byte[]"/>
Edit:
To answer your question regarding the number of messages in your test: there is only one $allParameters node; to iterate over its inner nodes (assuming XSLT 2.0), try:
<xsl:for-each select="$allParameters/field">
<!-- your code here -->
</xsl:for-each>

Related

replace characters from selected value

I'm new in xslt and I have to transform a xml file to html. I am able to retrieve my information but I want to shrink some strings. I dont know how to call the xsl template to my value-of select call. can someone help me?
EDIT: I changed my rplace template
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Changelog</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:center">Action</th>
<th style="text-align:center">Author</th>
<th style="text-align:center">Path</th>
<th style="text-align:center">Filename</th>
<th style="text-align:center">Comment</th>
</tr>
<xsl:for-each select="//Field[#name='CPEntries']/List/Item">
<tr>
<td><xsl:value-of select="Field[#name='type']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='user']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='member']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='summary']"/></td>
<td>
<xsl:call-template name="string-replace-all">
<xsl:with-param name="input" select="Field[#name='configpath']/Item/#id"/>
<xsl:with-param name="replace" select="'E'" />
<xsl:with-param name="by" select="'...'" />
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="string-replace-all">
<xsl:param name="text" />
<xsl:param name="replace" />
<xsl:param name="by" />
<xsl:choose>
<xsl:when test="$text = '' or $replace = ''or not($replace)" >
<!-- Prevent this routine from hanging -->
<xsl:value-of select="$text" />
</xsl:when>
<xsl:when test="contains($text, $replace)">
<xsl:value-of select="substring-before($text,$replace)" />
<xsl:value-of select="$by" />
<xsl:call-template name="string-replace-all">
<xsl:with-param name="text" select="substring-after($text,$replace)" />
<xsl:with-param name="replace" select="$replace" />
<xsl:with-param name="by" select="$by" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>Changelog</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th style="text-align:center">Action</th>
<th style="text-align:center">Author</th>
<th style="text-align:center">Path</th>
<th style="text-align:center">Filename</th>
<th style="text-align:center">Comment</th>
</tr>
<xsl:for-each select="//Field[#name='CPEntries']/List/Item">
<tr>
<td><xsl:value-of select="Field[#name='type']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='user']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='member']/Item/#id"/></td>
<td><xsl:value-of select="Field[#name='summary']"/></td>
<td>
<xsl:call-template name="search-and-replace">
<xsl:with-param name="input" select=value-of select="Field[#name='configpath']/Item/#id"/>
<xsl:with-param name="search-string" select="STRINGTOSEARCHFOR"/>
<xsl:with-param name="replace-string" select="..."/>
</xsl:call-template>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
<xsl:template name="search-and-replace">
  <xsl:param name="input"/>
  <xsl:param name="search-string"/>
  <xsl:param name="replace-string"/>
  <xsl:choose>
    <xsl:when test="$search-string and contains($input,$search-string)">
      <xsl:value-of select="substring-before($input,$search-string)"/>
      <xsl:value-of select="$replace-string"/>
      <xsl:call-template name="search-and-replace">
        <xsl:with-param name="input" select="substring-after($input,$search-string)"/>
        <xsl:with-param name="search-string" select="$search-string"/>
        <xsl:with-param name="replace-string" select="$replace-string"/>
      </xsl:call-template>
    </xsl:when>
    <xsl:otherwise>
      <xsl:value-of select="$input"/>
    </xsl:otherwise>
  </xsl:choose>
</xsl:template>
</xsl:stylesheet>
EDIT 2: This is an example entry of my xml file
<Field name="CPEntries">
<List elementType="item">
<Item id="PATH" context="775412:4" modelType="si.ChangePackage.Entry" displayId="PATH:Update">
<Field name="type">
<Item id="Update" modelType="si.ChangePackage.Entry.Action" displayId="Update">
<Field name="action">
<Value dataType="string">Update</Value>
</Field>
<Field name="previousState">
<Item id="FILE.c" modelType="cpName" displayId="FILE.c">
<Field name="oldRevision">
<Item id="1.63" modelType="si.Revision" displayId="1.63">
</Item>
</Field>
</Item>
</Field>
<Field name="isCommitted">
<Value dataType="boolean">true</Value>
</Field>
<Field name="isDeferred">
<Value dataType="boolean">false</Value>
</Field>
<Field name="isDiscarded">
<Value dataType="boolean">false</Value>
</Field>
<Field name="isPending">
<Value dataType="boolean">false</Value>
</Field>
<Field name="isExclusive">
<Value dataType="boolean">false</Value>
</Field>
</Item>
</Field>
<Field name="membertype">
<Value dataType="string">Member</Value>
</Field>
<Field name="member">
<Item id="FILE.c" modelType="si.Member" displayId="FILE.c">
</Item>
</Field>
<Field name="revision">
<Item id="1.64" modelType="si.Revision" displayId="1.64">
</Item>
</Field>
<Field name="user">
<Item id="Lastname, Firstname (uid)" modelType="si.User" displayId="Lastname, Firstname (uid)">
<Field name="fullname">
<Value dataType="string"></Value>
</Field>
</Item>
</Field>
<Field name="timestamp">
<Value dataType="datetime">2017-05-23T09:09:40</Value>
</Field>
<Field name="project">
<Item id="PROJECTPATH" modelType="si.Project" displayId="PROJECTPATH">
</Item>
</Field>
<Field name="configpath">
<Item id="CONFIGPATH" modelType="si.Project" displayId="CONFIGPATH">
</Item>
</Field>
<Field name="location">
<Value dataType="string">CONFIGPATH/efils.c</Value>
</Field>
<Field name="variant">
<Value></Value>
</Field>
<Field name="id">
<Item id="775412:4" modelType="si.ChangePackage" displayId="775412:4">
</Item>
</Field>
<Field name="summary">
<Value dataType="string">teststring</Value>
</Field>
<Field name="server">
<Item id="server.net" modelType="si.Server" displayId="server.net">
<Field name="hostname">
<Value dataType="string">server.net</Value>
</Field>
<Field name="port">
<Value dataType="int">7001</Value>
</Field>
</Item>
</Field>
<Field name="linesadded">
<Value dataType="long">281</Value>
</Field>
<Field name="linesdeleted">
<Value dataType="long">278</Value>
</Field>
<Field name="bytesadded">
<Value dataType="long">0</Value>
</Field>
<Field name="bytesdeleted">
<Value dataType="long">0</Value>
</Field>
<Field name="istext">
<Value dataType="boolean">true</Value>
</Field>
</Item>
[...]
I think you need to do this...
<xsl:call-template name="search-and-replace">
<xsl:with-param name="input" select="Field[#name='configpath']/Item/#id"/>
<xsl:with-param name="search-string" select="'STRINGTOSEARCHFOR'"/>
<xsl:with-param name="replace-string" select="'STRINGTOREPLACE'"/>
</xsl:call-template>
Note the apostrophes around 'STRINGTOSEARCHFOR' (and 'STRINGTOREPLACE'). Without this, they would be treated as xpath expressions, and so it would try to look for an element called STRINGTOSEARCHFOR, rather than use the literal string value.

Match in XSLT by solr

I have a problem with field of solr. when multiple nodes does not fit, and it shows empty field.
<field name="specs"/>
the xml original:
<?xml version="1.0" encoding="UTF-8"?>
<tire xmlns="http://schema.grabber" xmlns:x="http://www.w3.org/1999/xhtml"
tire-type="2" product-type="tire" id="102694" trademark="dunlop"
season="3" width="130" height="70" wheels="12" load="62" speed="l"
host="norauto" model="d207" hostDetailId="102694" hostDbID="6">
<url>product/_102694.html</url>
<price>49.95</price>
<ecorate>1.15</ecorate>
<currency>€</currency>
<vat>true</vat>
<img>images_produits/650x650/dunlop-d207-runscoot.jpg</img>
<content>DUNLOP D207 RUNSCOOT</content>
<specs>
<spec name="b_xl">0</spec>
</specs>
</tire>
Transformation XSLT at XML solr, this is xslt of solr:
<xsl:template match="/">
<docs>
<xsl:apply-templates select="cb:tire|cb:products" />
</docs>
</xsl:template>
<xsl:template match="cb:tire">
<doc>
<xsl:apply-templates select="#*|*" />
</doc>
</xsl:template>
<xsl:template match="cb:products">
<xsl:apply-templates select="#*|*" />
</xsl:template>
<xsl:template match="*/*[#name and not(parent::cb:products)]">
<xsl:call-template name="field">
<xsl:with-param name="name" select="concat(name(),'_',#name)"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="*/*[not(#name) and not(parent::cb:products)]">
<xsl:call-template name="field"/>
</xsl:teplate>
<xsl:template match="*[parent::cb:products]">
<xsl:choose>
<xsl:when test="not(text())">
<doc>
<xsl:apply-templates select="*|#*"/>
</doc>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="field"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="*|#*">
<xsl:call-template name="field">
<xsl:with-param name="value" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="field">
<xsl:param name="name" select="name()" />
<xsl:param name="value" select="text()" />
<field name="{translate(lower-case($name),' ','_')}">
<xsl:value-of select="$value" />
</field>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
The problem is the element <specs><spec name="b_xl">0</spec></specs>, the field is empty, the result not correct.
This is result XML:
<docs>
<doc>
<field name="tire-type">1</field>
<field name="product-type">tire</field>
<field name="season">1</field>
<field name="id">135-80-r13-70-t-kingstar-sk70</field>
<field name="trademark">kingstar</field>
<field name="model">sk70</field>
<field name="width">135</field>
<field name="height">80</field>
<field name="wheels">13</field>
<field name="load">70</field>
<field name="speed">t</field>
<field name="host">tires</field>
<field name="hostdetailid">135-80-r13-70-t-kingstar-sk70</field>
<field name="hostdbid">1000</field>
<field name="url">135-80-r13-70-t-kingstar-sk70.html</field>
<field name="price">29.73</field>
<field name="currency">€</field>
<field name="vat">true</field>
<field name="img">media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/0/1/0181050080001.png</field>
<field name="content">135/80 R13 70 T KINGSTAR SK70</field>
<field name="specs" />
</doc>
</docs>
I need to display the contents of the element if it contains specs.
To handle the children of specs as field you may add a template like this:
<xsl:template match="cb:specs" priority="1">
<xsl:apply-templates />
</xsl:template>
Which will generate following field:
<field name="spec_b_xl">0</field>
I do not know if this is as expected, because you didn't tell us who the output for specs should look like.

extract name of atribute of class

how show title attribute?
I would like to extract data from the #name.
<specs>
<spec name="b_homologation">1</spec>
<spec name="s_homologation_type">mo</spec>
<spec name="b_xl">0</spec>
<spec name="b_runflat">0</spec>
<spec name="s_consumption">e</spec>
<spec name="i_noise">72</spec>
<spec name="s_grip">c</spec>
</specs>
the result has to be:
<field name="b_homologation">1</field>
<field name="s_homologation_type">mo</field>
...
Thanks.
edit:
<xsl:template match="*|#*">
<xsl:call-template name="field">
<xsl:with-param name="value" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="field">
<xsl:param name="name" select="name()" />
<xsl:param name="value" select="text()" />
<field name="{$name}">
<xsl:value-of select="$value" />
</field>
</xsl:template>
And result is(not correct):
<field name="specs">1mo00e72c</field>
As I suggested in my comment, there's no need to mess around with named templates and parameters here, it's just a plain identity transform with tweaks:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- copy input to output verbatim ... -->
<xsl:template match="#*|node()">
<xsl:copy><xsl:apply-templates select="#*|node()" /></xsl:copy>
</xsl:template>
<!-- ... except spec elements, whose name changes to field -->
<xsl:template match="spec">
<field><xsl:apply-templates select="#*|node()" /></field>
</xsl:template>
</xsl:stylesheet>
This will produce
<specs>
<field name="b_homologation">1</field>
<field name="s_homologation_type">mo</field>
<field name="b_xl">0</field>
<field name="b_runflat">0</field>
<field name="s_consumption">e</field>
<field name="i_noise">72</field>
<field name="s_grip">c</field>
</specs>
You can use the same trick if you want to rename the root specs element to something like fields, but you can't leave it out completely if you want your output to be well-formed XML.
something like this? Didn't test it, can include some mini-bugs :)
<xsl:template match="spec" >
<xsl:call-template name="outputToXml" >
<xsl:with-param name="name" select="#name" />
<xsl:with-param name="value" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="outputToXml" >
<xsl:param name="name" />
<xsl:param name="value" />
<xsl:text><field name="</xsl:text>
<xsl:value-of select="$name" />
<xsl:text>"></xsl:text>
<xsl:value-of select="$value" />
<xsl:text></field></xsl:text>
</xsl:template>

How to create an attribute node and attach it to output node..?

I'm not able to figure out a way to attach an attribute node to output node in below scenario..
input xml:
<record>
<user>
<field name="LastName">user33</field>
<field name="FirstName">user33</field>
<field name="Title"/>
<field name="Email">user33#gmail.com</field>
<field name="WorkPhone"/>
<field name="Fax"/>
<field name="Description">new user</field>
<field name="Group Member"> group1</field>
</user>
</record>
Expected output:
<add class="user" id-val="user33 user33" >
<add-value attr="LastName">
<value type="string">user33</value>
</add-attr>
<add-value attr="FirstName">
<value type="string">user33</value>
</add-value>
<add-value attr="Email">
<value type="string">user33#gamil.com</value>
</add-value>
<add-value attr="Description">
<value type="string">new user</value>
</add-value>
</add>
this is the snippet of xslt that i have so far.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:variable name="lessThan" select="'<'"/>
<xsl:variable name="GreaterThan" select="'>'"/>
<xsl:template match="user">
<xsl:variable name="temp1" select="concat(field[#name=$srcdn-field-name1],' ')"/>
<xsl:variable name="temp2" select="concat($temp1,field[#name=$srcdn-field-name2])"/>
<xsl:variable name="src" select="translate($temp2,'+=,.\','-----')"/>
<xsl:value-of disable-output-escaping="yes" select="$lessThan"/>
<xsl:text>add</xsl:text>
<xsl:value-of disable-output-escaping="yes" select="$GreaterThan"/>
<!-- it is required to add attribute id-val to element <add> with value of $src-->
<xsl:for-each select="field[string()]">
<xsl:variable name="fieldValue" select="normalize-space(.)"/>
<xsl:choose>
<xsl:when test="#name !='Group Member'">
<add-value attr="{#name}">
<value type="string">
<xsl:value-of select="$fieldValue"/>
</value>
</add-value>
</xsl:when>
<xsl:otherwise>
<xsl:value-of disable-output-escaping="yes" select="$lessThan"/>
<xsl:text>/add</xsl:text>
<xsl:value-of disable-output-escaping="yes" select="$GreaterThan"/>
<!--perform some other operations-->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Now my requirement is to have id-val and class as attributes of <add>.. In this context the <xsl:attribute> isn't working. What changes do i need to make to my xslt.?
You can't add an attribute to something that's not an element. That's one of the many reasons not to try to manually construct start and end tags. Try this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="user">
<xsl:variable name="temp1" select="concat(field[#name=$srcdn-field-name1],' ')"/>
<xsl:variable name="temp2" select="concat($temp1,field[#name=$srcdn-field-name2])"/>
<xsl:variable name="src" select="translate($temp2,'+=,.\','-----')"/>
<add class="user" id-val="{$src}">
<!-- it is required to add attribute id-val to element <add> with value of $src-->
<xsl:for-each select="field[string()]
[not((. | preceding-sibling::field)
[#name = 'Group Member'])]">
<add-value attr="{#name}">
<value type="string">
<xsl:value-of select="normalize-space()"/>
</value>
</add-value>
</xsl:for-each>
</add>
</xsl:template>
</xsl:stylesheet>

Identifying unique subtrees in XSL

I have some XML that looks like this:
<root>
<message name="peter">
<field type="integer" name="pa" />
<group name="foo">
<field type="integer" name="action" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
<message name="wendy">
<field type="string" name="wa" />
<group name="foo">
<field type="integer" name="action" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
</root>
I have some XSL that I'm using to generate Java code from this XML. Previously I've been making a key, then generating a Java class for each group.
<xsl:key name="groupsByName" match="//group" use="#name"/>
....
<xsl:for-each select="//group[generate-id(.) = generate-id(key('groupsByName',#name)[1])]">
<xsl:call-template name="class-for-group"/>
</xsl:for-each>
All was well. Now, I've discovered that some messages have groups using the same name as groups present elsewhere, but missing one of the fields. To continue the example XML from above:
<message name="nana">
<field type="string" name="na" />
<group name="foo">
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
A group named "foo" is present, but it's missing the field with name "action".
What I'd like to do is to generate a Java class for each unique subtree. Is this possible? I can't work out what the xsl:key for that would look like. The closest idea I've had is
<xsl:key name="groupsKey" match="//group" use="concat(#name,count(*))"/>
which works for the case in the example above, but is hardly elegant. If there were instead two groups named "foo" with the same number (but different types) of fields, it would fail, so it's not actually a solution.
To be clear, the ideal key (or whatever alternative) would end up calling the template only once for the "peter" and "wendy" cases above, once for the "nana" case and again once for this case:
<message name="hook">
<field type="string" name="ha" />
<group name="foo">
<field type="string" name="favourite_breakfast" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
...because the fields within the group are different to those in the other cases. My key above doesn't cover this case. Is there a way to do so?
This transformation fulfills the requirements:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ext="http://exslt.org/common"
>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kGroupByType" match="group"
use="#type"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="vrtfPass1">
<xsl:apply-templates />
</xsl:variable>
<xsl:apply-templates mode="pass2"
select="ext:node-set($vrtfPass1)/*"/>
</xsl:template>
<xsl:template match="group">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:call-template name="makeType"/>
</xsl:copy>
</xsl:template>
<xsl:template mode="pass2"
match="group[generate-id()
=
generate-id(key('kGroupByType',#type)[1])
]
">
class <xsl:value-of select="concat(#name, '|', #type)"/>
</xsl:template>
<xsl:template name="makeType">
<xsl:attribute name="type">
<xsl:text>(</xsl:text>
<xsl:for-each select="*">
<xsl:value-of select="#type"/>
<xsl:if test="not(position()=last())">+</xsl:if>
</xsl:for-each>
<xsl:text>)</xsl:text>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
When applied on the provided XML document (with all additions):
<root>
<message name="peter">
<field type="integer" name="pa" />
<group name="foo">
<field type="integer" name="action" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
<message name="wendy">
<field type="string" name="wa" />
<group name="foo">
<field type="integer" name="action" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
<message name="nana">
<field type="string" name="na" />
<group name="foo">
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
<message name="hook">
<field type="string" name="ha" />
<group name="foo">
<field type="string" name="favourite_breakfast" />
<field type="integer" name="id" />
<field type="integer" name="value" />
</group>
</message>
</root>
the wanted result is produced:
class foo|(integer+integer+integer)
class foo|(integer+integer)
class foo|(string+integer+integer)
It is left as an exercise to the reader to further adjust this to produce valid names in one's PL, and also to make this work with structures of unlimited nestedness (which I may do in another answer -- however, we need a more precise definition for this more general provlem).