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.
Related
I would like to convert xml file format to another format;
using XSL version 1.0 or 2.0.
Input XML file:
<ROOT_XML>
<Struct id="_6" name="Result" context="_1" members="_9 _10 _11 _13 _14 "/>
<FundamentalType id="_7" name="int" size="32" align="32"/>
<FundamentalType id="_8" name="float" size="32" align="32"/>
<Field id="_9" name="angle" type="_8" offset="0" context="_6"/>
<Field id="_10" name="row" type="_7" offset="32" context="_6"/>
<Field id="_11" name="cloth" type="_18" offset="96" context="_6"/>
<Destructor id="_13" name="EmptyClass" artificial="1" throw="" context="_6">
</Destructor>
<Constructor id="_14" name="Result" context="_6">
<Argument type="_20" location="f0:2" file="f0" line="2"/>
</Constructor>
<Constructor id="_15" name="Result" context="_6"/>
<FundamentalType id="_17" name="unsigned int" size="32" align="32"/>
<ArrayType id="_18" min="0" max="29u" type="_21" size="240" align="8"/>
<ReferenceType id="_19" type="_6" size="32" align="32"/>
<ReferenceType id="_20" type="_6c" size="32" align="32"/>
<FundamentalType id="_21" name="char" size="8" align="8"/>
</ROOT_XML>
Output XML file:
<Struct Result>
<Fields>
<Field name="angle" type="float" size="32"/>
<Field name="row" type="int" size="32"/>
<Field name="cloth" type="char" size="240"/>
</Fields>
</Struct>
The is an example on how to parse the 'members' attribute list
http://www.w3.org/1999/XSL/Transform" version = "1.0">
<xsl:template match="/ROOT_XML/Struct">
<Struct><xsl:value-of select="name"/>
<xsl:choose>
<xsl:when test="boolean(./#members)">
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="./#members"/>
<xsl:with-param name="delimiter" select="' '"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</Struct>
</xsl:template>
<!--############################################################-->
<!--## Template to tokenize strings ##-->
<!--############################################################-->
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<member>
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring-before($list,$delimiter)"/>
</member>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<member>
<xsl:value-of select="$list"/>
</member>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
This code is a starting point to extract the relevant id's from the 'members' attribute of the 'Struct' node, and later be used to emit only the 'Field' nodes.
In addition, the output XML file might contains more than one 'Struct' node,
For Instance:
<Struct Result>
<Fields>
<Field name="angle" type="float" size="32"/>
<Field name="row" type="int" size="32"/>
<Field name="cloth" type="char" size="240"/>
</Fields>
</Struct>
<Struct Answer>
<Fields>
<Field name="direction" type="float" size="32"/>
<Field name="col" type="int" size="32"/>
<Field name="paper" type="char" size="232"/>
</Fields>
</Struct>
Thanks for the reply. still, I would like to emphasize the logic on how to get the xml output.
The xslt processor needs to parse the 'members' attribute of the Struct node.
The 'members' attribute is a list of Field's ids.
In the above example:
Only "members=_9 _10 _11" are Field nodes! and therefore they should be output as done by Michael previously.
The rest of the items in the list are omitted (i.e. members="_13 _14")
The combined code: (I need assistance to continue...)
http://www.w3.org/1999/XSL/Transform" version = "1.0">
<Struct><xsl:value-of select="name"/>
<xsl:choose>
<xsl:when test="boolean(./#members)">
<xsl:call-template name="tokenizeString">
<xsl:with-param name="list" select="./#members"/>
<xsl:with-param name="delimiter" select="' '"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise/>
</xsl:choose>
</Struct>
</xsl:template>
<!--############################################################-->
<!--## Template to tokenize strings ##-->
<!--############################################################-->
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<member>
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring-before($list,$delimiter)"/>
<!-- TODO: select holds the member's id...
Q: how should we continue from here?? -->
</member>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$list = ''">
<xsl:text/>
</xsl:when>
<xsl:otherwise>
<member>
<xsl:value-of select="$list"/>
<!-- TODO: select holds the member's id...
Q: how should we continue from here?? -->
</member>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="Field[key('f-type', #type)]">
<xsl:variable name="f-type" select="key('f-type', #type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$f-type/#size}"/>
</xsl:template>
<xsl:template match="Field[key('a-type', #type)]">
<xsl:variable name="a-type" select="key('a-type', #type)" />
<xsl:variable name="f-type" select="key('f-type', $a-type/#type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$a-type/#size}"/>
</xsl:template>
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:strip-space elements="*"/>
<xsl:key name="f-type" match="FundamentalType" use="#id" />
<xsl:key name="a-type" match="ArrayType" use="#id" />
<xsl:template match="ROOT_XML">
<Struct>
<xsl:apply-templates select="Field"/>
</Struct>
</xsl:template>
<xsl:template match="Field[key('f-type', #type)]">
<xsl:variable name="f-type" select="key('f-type', #type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$f-type/#size}"/>
</xsl:template>
<xsl:template match="Field[key('a-type', #type)]">
<xsl:variable name="a-type" select="key('a-type', #type)" />
<xsl:variable name="f-type" select="key('f-type', $a-type/#type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$a-type/#size}"/>
</xsl:template>
</xsl:stylesheet>
when applied to your example input, will return:
<?xml version="1.0" encoding="UTF-8"?>
<Struct>
<Field name="angle" type="float" size="32"/>
<Field name="row" type="int" size="32"/>
<Field name="cloth" type="char" size="240"/>
</Struct>
How this works:
If a Field's type matches an id of a FundamentalType, then
the type and the size values are looked up from the matching
FundamentalType;
If a Field's type matches an id of an ArrayType, then the size value is looked up from the matching ArrayType, while the type value is looked up from the FundamentalType whose id matches the type attribute of the ArrayType.
Edit:
If you want each Struct to include only Fields whose id is listed in its members attribute, you can do it this way:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="field" match="Field" use="#id" />
<xsl:key name="f-type" match="FundamentalType" use="#id" />
<xsl:key name="a-type" match="ArrayType" use="#id" />
<xsl:variable name="xml" select="/" />
<xsl:template match="/ROOT_XML">
<root>
<xsl:for-each select="Struct">
<Struct name="{#name}">
<xsl:apply-templates select="key('field', tokenize(#members, ' '))"/>
</Struct>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="Field[key('f-type', #type)]">
<xsl:variable name="f-type" select="key('f-type', #type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$f-type/#size}"/>
</xsl:template>
<xsl:template match="Field[key('a-type', #type)]">
<xsl:variable name="a-type" select="key('a-type', #type)" />
<xsl:variable name="f-type" select="key('f-type', $a-type/#type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$a-type/#size}"/>
</xsl:template>
</xsl:stylesheet>
Edit 2
The same thing in XSLT 1.0:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="field" match="Field" use="#id" />
<xsl:key name="f-type" match="FundamentalType" use="#id" />
<xsl:key name="a-type" match="ArrayType" use="#id" />
<xsl:variable name="xml" select="/" />
<xsl:template match="/ROOT_XML">
<root>
<xsl:for-each select="Struct">
<xsl:variable name="members">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="#members"/>
</xsl:call-template>
</xsl:variable>
<Struct name="{#name}">
<xsl:apply-templates select="key('field', exsl:node-set($members)/token)"/>
</Struct>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="Field[key('f-type', #type)]">
<xsl:variable name="f-type" select="key('f-type', #type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$f-type/#size}"/>
</xsl:template>
<xsl:template match="Field[key('a-type', #type)]">
<xsl:variable name="a-type" select="key('a-type', #type)" />
<xsl:variable name="f-type" select="key('f-type', $a-type/#type)" />
<Field name="{#name}" type="{$f-type/#name}" size="{$a-type/#size}"/>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text, $delimiter)">
<token>
<xsl:value-of select="substring-before($text, $delimiter)"/>
</token>
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<token>
<xsl:value-of select="$text"/>
</token>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
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>
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>
I am trying to loop child elements under import and export. Create a parent element fields and put all the elements which the name is field into it, just under import, not include the elements field under structure, Create element structures over all elements structure, and the last work is rename all the element with the value of attribute name. I just know copy all of them first and can not go to next step to create the right template.
input:
<?xml version='1.0'?>
<jco name="TEST" timestamp="1275691115508" version="1.0">
<import>
<field name="RESERVED_IN">12345</field>
<structure name="GM_HEADER">
<field name="PSTNG_DATE">2004-07-02</field>
<field name="DOC_DATE">2004-04-02</field>
</structure>
<structure name="TESTRUN">
<field name="TESTRUN"></field>
</structure>
</import>
<export>
<field name="RESERVED_OUT"></field>
<structure name="GM_HEADER_RET">
<field name="MAT_DOC"></field>
<field name="DOC_YEAR">0000</field>
</structure>
</export>
</jco>
desired output:
<?xml version="1.0" ?>
<jco version="1.0" name="TEST">
<import>
<fields>
<RESERVED_IN>12345</RESERVED_IN>
</fields>
<structures>
<GM_HEADER>
<PSTNG_DATE>2004-07-02</PSTNG_DATE>
<DOC_DATE>2004-04-02</DOC_DATE>
</GM_HEADER>
<TESTRUN>
<TESTRUN></TESTRUN>
</TESTRUN>
</structures>
</import>
<export>
<fields>
<RESERVED_OUT></RESERVED_OUT>
</fields>
<structures>
<GM_HEADER_RET>
<MAT_DOC></MAT_DOC>
<DOC_YEAR>0000</DOC_YEAR>
</GM_HEADER_RET>
</structures>
</export>
</jco>
Below is my xslt, but it seems messed up.
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*" />
</xsl:copy>
</xsl:template>
<xsl:template match="import">
<fields>
<xsl:for-each select="/field">
<xsl:call-template name="field" />
</xsl:for-each>
</fields>
<structures>
<xsl:for-each select="/structure">
<xsl:element name="{#name}">
<xsl:value-of select="." />
</xsl:element>
<xsl:call-template name="field" />
</xsl:for-each>
</structures>
</xsl:template>
<xsl:template name="field">
<xsl:for-each select="/field">
<xsl:element name="{#name}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</xsl:template>
Use templates and apply-templates, not for-each, and it works out rather elegantly:
<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 indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* , node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="import | export">
<xsl:copy>
<fields>
<xsl:apply-templates select="field"/>
</fields>
<structures>
<xsl:apply-templates select="structure"/>
</structures>
</xsl:copy>
</xsl:template>
<xsl:template match="field | structure">
<xsl:element name="{#name}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I have an XML file. I should find for same field2 start(min) date and End(max) date from field3. Maybe xsl have some function to find it.Because I try do it trying to find min month and min day and max month, max day.
XML:
<document>
<line id="0">
<field id="2">X111</field>
<field id="3">2011-03-31</field>
</line>
<line id="1">
<field id="2">X111</field>
<field id="3">2011-04-04</field>
</line>
<line id="2">
<field id="2">X111</field>
<field id="3">2011-04-02</field>
</line>
<line id="3">
<field id="2">X222</field>
<field id="3">2011-04-04</field>
</line>
<line id="4">
<field id="2">X222</field>
<field id="3">2011-04-01</field>
</line>
<line id="4">
<field id="2">X333</field>
<field id="3">2011-04-01</field>
</line>
</document>
Output:
<document>
<Message>
<ID>X111</ID>
<dateStart>2011-03-31</dateStart>
<dateEnd>2011-04-04</dateEnd>
</Message>
<Message>
<ID>X222</ID>
<dateStart>2011-04-01</dateStart>
<dateEnd>2011-04-04</dateEnd>
</Message>
<Message>
<ID>X333</ID>
<dateStart>2011-04-01</dateStart>
<dateEnd>2011-04-01</dateEnd>
</Message>
</document>
Please help to solve it. I'm working with stylesheet version="1.0".
This can probably be optimized but it returns the requested results:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="test" match="line" use="field[#id=2]"/>
<xsl:template match="/">
<document>
<xsl:for-each select="//line[generate-id()=generate-id(key('test',field[#id=2]))]">
<xsl:sort select="field[#id=2]"/>
<Message>
<ID>
<xsl:value-of select="field[#id=2]"/>
</ID>
<xsl:apply-templates select="key('test',field[#id=2])">
<xsl:sort select="field[#id=3]"/>
</xsl:apply-templates>
</Message>
</xsl:for-each>
</document>
</xsl:template>
<xsl:template match="line">
<xsl:if test="position()=1">
<dateStart>
<xsl:value-of select="field[#id=3]"/>
</dateStart>
</xsl:if>
<xsl:if test="position()=last()">
<dateEnd>
<xsl:value-of select="field[#id=3]"/>
</dateEnd>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I upvoted #mousio's answer, but I'd prefer to see the first of each line type handled in its own template. So, in the spirit of TMTOWTDI, here's how I would have done it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:key name="byField2" match="line" use="field[#id=2]" />
<xsl:template match="/">
<document>
<xsl:apply-templates select="document/line" />
</document>
</xsl:template>
<xsl:template match="line[count(.|key('byField2', field[#id=2])[1])=1]">
<Message>
<ID><xsl:value-of select="field[#id=2]" /></ID>
<xsl:apply-templates select="key('byField2', field[#id=2])" mode="m">
<xsl:sort select="field[#id=3]" />
</xsl:apply-templates>
</Message>
</xsl:template>
<xsl:template match="line" mode="m">
<xsl:if test="position()=1">
<dateStart><xsl:value-of select="field[#id=3]" /></dateStart>
</xsl:if>
<xsl:if test="position()=last()">
<dateEnd><xsl:value-of select="field[#id=3]" /></dateEnd>
</xsl:if>
</xsl:template>
<xsl:template match="line" />
</xsl:stylesheet>
I think this is easier to read (and probably more efficient on large documents, since it doesn't abuse //).
This transformation shows how to find the wanted minimum and maximum:
<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="/*">
<xsl:apply-templates select="line">
<xsl:sort select="field[#id=3]"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="line">
<xsl:if test="position()=1">
Earliest:
<xsl:copy-of select="."/>
</xsl:if>
<xsl:if test="position()=last()">
Latest:
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<document>
<line id="0">
<field id="2">X111</field>
<field id="3">2011-03-31</field>
</line>
<line id="1">
<field id="2">X111</field>
<field id="3">2011-04-04</field>
</line>
<line id="2">
<field id="2">X111</field>
<field id="3">2011-04-02</field>
</line>
<line id="3">
<field id="2">X222</field>
<field id="3">2011-04-04</field>
</line>
<line id="4">
<field id="2">X222</field>
<field id="3">2011-04-01</field>
</line>
<line id="4">
<field id="2">X333</field>
<field id="3">2011-04-01</field>
</line>
</document>
produces:
Earliest:
<line id="0">
<field id="2">X111</field>
<field id="3">2011-03-31</field>
</line>
Latest:
<line id="3">
<field id="2">X222</field>
<field id="3">2011-04-04</field>
</line>
Explanation:
Choosing the first and last element from the sorted node-list. The dates are in a "good" format, so they are sorted just as strings.