Page 1 of 1

Sort based on attribute values

Posted: Mon Oct 17, 2005 5:51 pm
by Iwan Jauch
Hi everybody

I have a problem to sort my xml document. I would like to sort the document based on the attribute value of the attribute "Code department". What is the problem?

I can create an new document but it is empty.

XML-Document:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="styles.xslt"?>
<Request xmlns="newsalesorder">
<Parameters Method="NewSalesOrder">
<Order Service="0" DocumentType="Order">
<OrderFields>
<Field Name="Sell-to Customer No.">150649</Field>
<Field Name="Your Reference">TEST</Field>
<Field Name="External Document No.">20050514</Field>
</OrderFields>
<Lines>
<LineFields Type="Item">
<Field Name="No.">273080</Field>
<Field Name="Quantity">1</Field>
<Field Name="Code Department">2</Field>
</LineFields>
</Lines>
<Lines>
<LineFields Type="Item">
<Field Name="No.">273081</Field>
<Field Name="Quantity">1</Field>
<Field Name="Code Department">1</Field>
</LineFields>
</Lines>
</Order>
</Parameters>
</Request>


XSLT-Document:
<?xml version="1.0" encoding="UTF-8"?>
<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:template match="/">
<xsl:apply-templates select="Request/Parameters/Order/Lines/LineFields/Field">
<xsl:sort select="Field/@Name = Code Department" order="ascending" />
</xsl:apply-templates>
</xsl:template>
</xsl:stylesheet>

Thanks a lot for your help.
Greetings Iwan

Posted: Mon Oct 17, 2005 6:16 pm
by george
Hi Iwan,

The first problem is that you need to use a prefix in the XPath expressions in your stylesheet to refer to elements from a specified namespace, and you specified a default namespace for your XML document. There are also a few other problems in the way you set the sort criteria, the working sample below may help you:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nso="newsalesorder" exclude-result-prefixes="nso"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<result>
<xsl:apply-templates
select="nso:Request/nso:Parameters/nso:Order/nso:Lines/nso:LineFields/nso:Field[@Name='Code Department']">
<xsl:sort select="." order="ascending"/>
</xsl:apply-templates>
</result>
</xsl:template>
<xsl:template match="nso:Field">
[<xsl:value-of select="../nso:Field[@Name='No.']"/>: <xsl:value-of select="."/>]
</xsl:template>
</xsl:stylesheet>
Best Regards,
George

Posted: Tue Oct 18, 2005 8:02 am
by Iwan Jauch
Thanks for your help George !

I tryed it out and it worked!

The output is at the moment a html file.

What can I do, that the output file look the same as the input xml file just sorted in an oder way? Do you have any ideas?

Thank for your help
Greetings Iwan

Posted: Tue Oct 18, 2005 9:19 am
by Iwan Jauch
Hi everybody

I tried out this version but it doesn't work! I receive an XML-Document as output but the sorting doesn't work anymore!

Does anybody know where the problem is?

Thanks a lot for your help
Greetings Iwan

[code]<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nso="newsalesorder" exclude-result-prefixes="nso"
>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" >
<xsl:sort select="nso:Request/nso:Parameters/nso:Order/nso:Lines/nso:LineFields/nso:Field[@Name='Code Department']" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
[/code]

Posted: Tue Oct 18, 2005 11:10 am
by george
Hi Iwan,

Something like below may do what you want:

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nso="newsalesorder" exclude-result-prefixes="nso">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="nso:Order">
<xsl:copy>
<xsl:apply-templates select="nso:OrderFields"/>
<xsl:apply-templates select="nso:Lines">
<xsl:sort select="nso:LineFields/nso:Field[@Name='Code Department']"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George

Posted: Tue Oct 18, 2005 11:59 am
by Iwan Jauch
Hi George

Thank very very mutch for your help. It is working!!!!

Thanks and have a nice day

Best regards
Iwan

Posted: Thu Oct 20, 2005 10:42 am
by Iwan Jauch
Hi everybody

I have a small problem with the sort tag! My XML file changed and I have to change my stylesheet but I have not really an idee what to change!

Please can anybody help me? This is my new XML File and I still use the same stylesheet:

XML Document:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="styles.xsl"?>
<Request xmlns="http://www.mga.ch/navision/newsalesorder">
<Parameters Method="NewSalesOrder">
<Order Service="0">
<OrderFields>
<Field Name="Sell-to Customer No.">150649</Field>
<Field Name="Your Reference">Webshop </Field>
<Field Name="External Document No.">20050514</Field>
</OrderFields>
<Lines>
<LineFields Type="Item">
<Field Name="No.">101045</Field>
<Field Name="Quantity">1</Field>
<Field Name="Code Department">1</Field>
</LineFields>
<LineFields Type="Item">
<Field Name="No.">5530 BH</Field>
<Field Name="Quantity">3</Field>
<Field Name="Code Department">2</Field>
</LineFields>
<LineFields Type="Item">
<Field Name="No.">273080</Field>
<Field Name="Quantity">1</Field>
<Field Name="Code Department">1</Field>
</LineFields>
</Lines>
</Order>
</Parameters>
</Request>

Stylesheet from George (see above)

PS: does anybody knows a good book about xsl?

Thanks a lot for your help
Best Regards Iwan

Posted: Thu Oct 20, 2005 12:34 pm
by george
Hi Iwan,

You can find good XSLT books on our Resources/Books section:

http://www.oxygenxml.com/xml_books.html

You changed the namespace on the source document and the structure. The adjusted stylesheet is below:

Code: Select all


<?xml version="1.1" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:nso="http://www.mga.ch/navision/newsalesorder" exclude-result-prefixes="nso">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="nso:Lines">
<xsl:copy>
<xsl:apply-templates select="nso:LineFields">
<xsl:sort select="nso:Field[@Name='Code Department']"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Best Regards,
George

Posted: Mon Oct 24, 2005 10:07 am
by Iwan Jauch
Hi George

Thanks very very mutch for your great help. Everything is working wonderful!

Have a nice day
Best regards,
Iwan