Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 20th, 2007, 07:57 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Getting fields, specific order from xml using xslt

Hi,

My Requirement is to get/read the data from xml file using java-xslt in the specific order, and i have written the conditions in the .xsl file to check & extract the fields in the specific(i.e random/our choice) order, but the fields are retrieved/processed in the sequence order of the fields in the xml file, and inturn storing onto the flat file in the sequence order of the fields in the xml file. So, please give me some idea/solution on this issue.

Example scenario:
xml data: Required Output Actual(Getting)Output
                (Random/specific)
Data Field1 Data Field2 Data Field1
Data Field2 Data Field1 Data Field2
Data Field3 Data Field3 Data Field3


Xml data is in the following format:

<PROPERTIES KEY="..."
     <PROPERTY attrb="..." >
         <PROPERTYKEY>Field1</PROPERTYKEY>
         <PROPERTYVALUE>Field1 Value</PROPERTYVALUE>
     </PROPERTY>
     <PROPERTY attrb="..." >
          <PROPERTYKEY>Field2</PROPERTYKEY>
           <PROPERTYVALUE>Field2 Value</PROPERTYVALUE>
     </PROPERTY>
     <PROPERTY attrb="..." >
            <PROPERTYKEY>Field3</PROPERTYKEY>
            <PROPERTYVALUE>Field3 Value</PROPERTYVALUE>
     </PROPERTY>
 </PROPERTIES>

Criteria/Conditions in the .XSL file

<xsl:template match="PROPERTY">
    <xsl:variable name="property_key" select="PROPERTYKEY"/>
    <xsl:choose>
    <xsl:when test="$property_key='Field1'">
    <xsl:value-of select="Field1 Value"/>
    <xsl:value-of select="'~'"/>
    </xsl:when>
    <xsl:when test="$property_key='Field2'">
    <xsl:value-of select="Field2 Value"/>
    <xsl:value-of select="'~'"/>
    </xsl:when>
...

Thanks,

JR


 
Old July 20th, 2007, 08:47 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

You say you've written the code to process properties in a particular order, but you haven't shown any code that tries to do this. All you've shown is code that processes a single property and tests which one it is.

Any control over ordering needs to go at the next level up, where you apply-templates to the PROPERTY elements. For example

<xsl:template match="PROPERTIES">
  <xsl:apply-templates select="PROPERTY">
    <xsl:sort select="...."/>
  </
</xsl:template>

or if you want to enumerate the order rather than achieving it by sorting

<xsl:template match="PROPERTIES">
  <xsl:apply-templates select="PROPERTY[condition1]"/>
  <xsl:apply-templates select="PROPERTY[condition2]"/>
  <xsl:apply-templates select="PROPERTY[condition3]"/>
</xsl:template>

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 23rd, 2007, 08:47 AM
Registered User
 
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Thanks for the reply,
Actually, i have not written code specifically for sorting,
I have written the conditions based on 'property key' for the display of 'property value' in the specific order i.e. the order in which the fields(property value) to be displayed. I have also shown the .xsl code in the previous topic/discussion request.

And the code part, yes i have shown only single property, as it is the one, going to be repeated for no. of times(100+ fields/properties), it is under the 'properties' tag.

Regarding Sort, i can't use '<xsl:sort ..>' as the required display order of the fields is not ascending/descending,.. it is specific i.e.

for ex. there are fields like field1,field2,field3,field4,field10,...
required order is field2,field1,field10,field3,...

Finally, i think,i got the idea/solution from your reply.
I was looking for 'enumerating of the order '
ex.:
<xsl:template match="PROPERTIES">
  <xsl:apply-templates select="PROPERTY[condition1]"/>
  <xsl:apply-templates select="PROPERTY[condition2]"/>
  <xsl:apply-templates select="PROPERTY[condition3]"/>
</xsl:template>
Only the problem is that, i have to write down all the fields(100+) along with the condition i.e. fields names with-in '[]'.


Thanks & Regards,

JR










 
Old July 23rd, 2007, 08:56 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Try creating a separate document that lists the fields in order

<field name="field3" position="1"/>
<field name="field6" position="2"/>
...

then you can sort on the value of document('lookup.xml')/field[@name=...]/@position

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old July 23rd, 2007, 11:35 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It was just a suggestion, if you don't know how to do it, then either don't try, or spend some time reading about the areas of the language that you haven't yet explored. I'm not going to write the code for you, or to give you personal tuition.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference





Similar Threads
Thread Thread Starter Forum Replies Last Post
To Select a specific nodes from XML vkommera XSLT 4 June 27th, 2008 03:39 PM
Display orders for a specific customer - XSLT two_days_late XSLT 3 April 2nd, 2007 04:40 AM
Order a recordset by specific values SoC SQL Language 2 August 28th, 2005 08:27 PM
How to get a list of specific tag from xml using v JelfMaria XML 4 January 13th, 2005 09:34 AM
ASP SQL fields in order ALoPresto Classic ASP Databases 2 July 25th, 2003 02:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.