 |
| 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
|
|
|
|

July 20th, 2007, 07:57 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 20th, 2007, 08:47 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 23rd, 2007, 08:47 AM
|
|
Registered User
|
|
Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 23rd, 2007, 08:56 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

July 23rd, 2007, 11:35 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|
 |