How to sort some data with either/or condition
tamilselvi wrote: > How to sort some data with either/or condition Can you be more specific? How does your data look? What kind of sorting criteria do you want to use exactly? Sorting in XSLT 1.0 is done with the xsl:sort element as a child element of xsl:for-each or xsl:apply-templates, see http://www.w3.org/TR/xslt#sorting With XSLT 2.0 you have xsl:sort and additionally xsl:perform-sort, see http://www.w3.org/TR/xslt20/#creating-sorted-sequence. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
On Jun 19, 4:20 pm, Martin Honnen wrote: > tamilselvi wrote: > > How to sort some data with either/or condition > > Can you be more specific? How does your data look? What kind of sorting > criteria do you want to use exactly? > Sorting in XSLT 1.0 is done with the xsl:sort element as a child element > of xsl:for-each or xsl:apply-templates, seehttp://www.w3.org/TR/xslt#sorting > With XSLT 2.0 you have xsl:sort and additionally xsl:perform-sort, seehttp://www.w3.org/TR/xslt20/#creating-sorted-sequence. > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/ I have two fields Name & Sortname , here i have to sort by "Name" if Name is null in that record i have to sort by Sortname... 1. How to find the field "Name " is null in sort? 2. If "Name" is null how to sort by "Sortname" ?
tamilselvi wrote: > I have two fields Name & Sortname , here i have to sort by "Name" if > Name is null in that record i have to sort by Sortname... > > 1. How to find the field "Name " is null in sort? > 2. If "Name" is null how to sort by "Sortname" ? We are talking about XML and sorting, right? What do you have, elements named 'Name' and 'Sortname', or attributes named 'Name' and 'Sortname'? And how exactly does a "null" value look in your XML? Does that mean the element or attribute is not present at all or does it have a special value indicating null? -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
The XML format is <Company Name="Tamil" Sortname="abc"/> <Company Name="Tamils" Sortname="xyz"/> <Company Name="ABCs" Sortname="Www"/>
tamilselvi wrote: > The XML format is > > <Company Name="Tamil" Sortname="abc"/> > <Company Name="Tamils" Sortname="xyz"/> > <Company Name="ABCs" Sortname="Www"/> So you have attributes named 'Name' and 'Sortname'. Try whether <xsl:apply-templates select="Company"> <xsl:sort select="@Name" data-type="text"/> <xsl:sort select="@Sortname" data-type="text"/> </xsl:apply-templates> does what you are looking for. That sorts on @Name first, then on @Sortname. If the above is not what you are looking for then you need to tell us exactly what kind of "null" values you have i.e. whether the attribute is not present or whether it has a special value indicating null. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Martin Honnen wrote: > tamilselvi wrote: >> The XML format is >> >> <Company Name="Tamil" Sortname="abc"/> >> <Company Name="Tamils" Sortname="xyz"/> >> <Company Name="ABCs" Sortname="Www"/> > > So you have attributes named 'Name' and 'Sortname'. Try whether > <xsl:apply-templates select="Company"> > <xsl:sort select="@Name" data-type="text"/> > <xsl:sort select="@Sortname" data-type="text"/> > </xsl:apply-templates> > does what you are looking for. That sorts on @Name first, then on > @Sortname. > > If the above is not what you are looking for then you need to tell us > exactly what kind of "null" values you have i.e. whether the attribute > is not present or whether it has a special value indicating null. Assuming "null" value means that the Name attribute is not present you can use <xsl:apply-templates select="Company"> <xsl:sort select="(@Name | @Sortname)[1]"/> </xsl:apply-templates> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
Martin Honnen wrote: > Assuming "null" value means that the Name attribute is not present you > can use > <xsl:apply-templates select="Company"> > <xsl:sort select="(@Name | @Sortname)[1]"/> > </xsl:apply-templates> Slight correction: <xsl:apply-templates select="Company"> <xsl:sort select="(@Name | @Sortname[not(../@Name)])[1]"/> </xsl:apply-templates> -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/
On Jun 19, 6:56 pm, Martin Honnen wrote: > Martin Honnen wrote: > > Assuming "null" value means that the Name attribute is not present you > > can use > > <xsl:apply-templates select="Company"> > > <xsl:sort select="(@Name | @Sortname)[1]"/> > > </xsl:apply-templates> > > Slight correction: > > <xsl:apply-templates select="Company"> > <xsl:sort select="(@Name | @Sortname[not(../@Name)])[1]"/> > </xsl:apply-templates> > > -- > > Martin Honnen --- MVP XML > http://JavaScript.FAQTs.com/ Thanks <xsl:apply-templates select="Company"> <xsl:sort select="(@Name | @Sortname[not(../@Name)])[1]"/> </xsl:apply-templates> Its working fine. Can u explain this concept <xsl:sort select="(@SortName | @Name[not(../ @SortName)])[1]"/>
tamilselvi wrote: > <xsl:apply-templates select="Company"> > <xsl:sort select="(@Name | @Sortname[not(../@Name)])[1]"/> > </xsl:apply-templates> > > Its working fine. > Can u explain this concept <xsl:sort select="(@Name | @Sortname[not(../@Name)])[1]"/> can be further simplified to <xsl:sort select="(@Name | @Sortname[not(../@Name)])"/> The '|' is the union operator so what you do is taking the union of the 'Name' attribute and the 'Sortname' attribute not having a parent with a 'Name' attribute. That way you ensure that, if the 'Name' attribute is present, you sort on the 'Name' attribute, and if 'Name' is not present, then you sort on the 'Sortname' attribute. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/