how to access Custom Attributes

Questions about writing stylesheets for transforming (Tools > Transform Tasklist) your tasklist

Moderator: abstr

ispyridis
Posts: 95
Joined: Fri Apr 07, 2023 7:05 pm

how to access Custom Attributes

Post by ispyridis » Sun Apr 30, 2023 12:53 pm

Hi I did some reading on the xsl posts here and in codeproject site of TDL.
I managed to get some access to the custom attributes but I do not know how to take it further to produce the correct document.

from the examples given I modified one. The attributes i'm requesting are CUST_QUANTITY and CUST_UNITPRICE

Code: Select all

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
	<!-- This file is an example from
	http://www.codeproject.com/Articles/898616/ToDoLists-StyleSheets-a-tutorial
	May 2015 -->
	<xsl:output method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" />

	<xsl:template match="/TODOLIST">
		<xsl:element name="html">
			<xsl:element name="head">
				<xsl:element name="title">
					<xsl:value-of select="@PROJECTNAME" />
				</xsl:element>
			</xsl:element>
			<xsl:element name="body">
				<xsl:value-of select="@PROJECTNAME" />
				<xsl:element name="br" />
				<xsl:value-of select="@FILENAME" />
				<xsl:element name="br" />
				<xsl:element name="br" />
				<xsl:apply-templates select="TASK" />

			</xsl:element>
		</xsl:element>
	</xsl:template>


	<xsl:template match="TASK">
		<xsl:value-of select="@TITLE" />
		<xsl:element name="br" />

		
		<xsl:apply-templates select="TASK" />
			<xsl:if test="CUSTOMATTRIB">
			<xsl:for-each select="(TASK/CUSTOMATTRIB[@ID='CUST_QUANTITY'])">
				<xsl:value-of select="@VALUE"/>
		<xsl:element name="br" />
			</xsl:for-each>

			<xsl:for-each select="(TASK/CUSTOMATTRIB[@ID='CUST_UNITPRICE'])">
			<xsl:text> x </xsl:text>
				<xsl:value-of select="@VALUE"/>
		<xsl:element name="br" />
			</xsl:for-each>
			</xsl:if>
			


		<xsl:element name="br" />
			
	</xsl:template>




	


</xsl:stylesheet>
The result that i'm getting is this:
My list
My list.tdl

Δημόσια Έργα
Δασικό έργο ντα βρύση καγκελούδια
Σύνταξη λογαριασμού

24
x 34

1
x 0

Ανακαινίσεις
Κατοικία Αγ. Λαύρας 9 Καβάλα
Εξωτερικές επισκευές
Μόνωση ταράτσας

Πλαϊνό βάψιμο πολυκατοικίας
Βάψιμο, σοβάτισμα , καθαρισμός ποταμων

Μισθωση σκαλωσιας

4
3
x 23
x 56

Κατασκευές στα μπαλκόνια
Κάγκελο
Βάψιμο, στοκαρισμα

στοκάρισμα στο κάγκελο

4
6
x 5
x 6

Κατασκευή Πέργκολας
Προσδιορισμός υλικών πέργκολας

Ανέβασμα υλικών

Προετοιμασία υλικών, τρίψιμο, βερνικωμα

Εργασίες κατασκευής στησίματος

10
2
45
45
x 24
x 3
x 4
x 3

0
0
x 0
x 0

5
0
0
x 5
x 0
x 0

Εσωτερικές επισκευές
Τοποθέτηση καθρεφτών και ξύλων ανάρτησης στο γυμναστήριο.

Δημιουργία γραφείου και βιβλιοθήκης στο μικρό δωμάτιο.

Καθαίρεση κεντρικού επίπλου κουζίνας και δημιουργία πάγκου

Άνοιγμα στο πίσω μπαλκόνι της κρεβατοκάμαρας

Καθαίρεση σοβάδωνκαι επισκευή στην περιοχή της σκάλας

Βάψιμο εσωτερικών χώρων

Τοποθέτηση κάγκελο σκάλα


0
x 0

0
x 0

Αυθαίρετα
1. Ψηφιακή Σάρωση σχεδίων

2. Σχεδιασμός επί σαρωμένων των αποτυπομένων υποστυλωμάτων


The document i want to produce would be:
TASK POSISITION | TASK NAME | CUST_UNIT | CUST_QUANTITY | CUST_UNITPRICE | CUST_TOTALPRICE* |

*I also realized that CUST_TOTALPRICE being a calculated attribute, is not getting exported to tdl with the tasks, is saved only as definition.
So I suppose the last column has to be calculated in the xsl file but how we do calculations in xsl? Also this attribute is cumulative.

Also why the custom attributes have only the option to do calculations between only two attributes? wouldn't be easier and more versatile just to have a formula field?
And maybe we would need attribute groups that would also help with the interface? With foldable groups of attributes? Timing attributes/Location Attributes/Costing Attributes/Allocation attributes etc.

User avatar
zonfeld
Posts: 173
Joined: Sun Mar 21, 2021 11:41 am
Contact:

Re: how to access Custom Attributes

Post by zonfeld » Sun Apr 30, 2023 4:37 pm

In general, it's a good idea to provide a working example so that your results can be reproduced.

Based on the attached example tasklist and the XSLT further down (I'm not allowed to attach XSLT files, it seems), you'll get this result:

Custom_attribute_example.jpg

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
	<xsl:output encoding="UTF-8" method="html" indent="yes" doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"/>
	<xsl:template match="/TODOLIST">
		<xsl:element name="html">
			<xsl:element name="head">
				<xsl:element name="title">
					<xsl:value-of select="@PROJECTNAME"/>
				</xsl:element>
				<style type="text/css">
				.table-with-borders { background-color:#eee;border-collapse:collapse; }
				.table-with-borders th { background-color:#000;color:white;width:20%; }
				.table-with-borders td, .table-with-borders th { padding:5px;border:1px solid #000; }
				</style>				
			</xsl:element>
			<xsl:element name="body">
				<xsl:value-of select="@PROJECTNAME"/>
				<xsl:element name="br"/>
				<xsl:value-of select="@FILENAME"/>
				<xsl:element name="br"/>
				<xsl:element name="br"/>
				<xsl:call-template name="create-table"/>
			</xsl:element>
		</xsl:element>
	</xsl:template>
	<xsl:template name="create-table">
		<table class="table-with-borders">
			<tbody>
				<tr>
					<th>Item</th>
					<th>Unit</th>
					<th>Unit price</th>
					<th>Quantity</th>					
					<th>Price</th>
				</tr>
				<xsl:for-each select="//TASK[CUSTOMATTRIB]">
					<tr>
						<xsl:call-template name="create-table-row"/>
					</tr>
				</xsl:for-each>
				<tr>
					<td colspan="5"  align="right">
						<xsl:value-of select="format-number(sum(//TASK[CUSTOMATTRIB]/(CUSTOMATTRIB[@ID='CUST_QUANTITY']/@VALUE * CUSTOMATTRIB[@ID='CUST_UNITPRICE']/@VALUE)), '#.00')"/>
					</td>
				</tr>
			</tbody>
		</table>
	</xsl:template>
	<xsl:template name="create-table-row">
		<td>
			<xsl:value-of select="@TITLE"/>
		</td>
		<td>
			<xsl:value-of select="CUSTOMATTRIB[@ID='CUST_UNIT']/@VALUE"/>
		</td>
		<td align="right">
			<xsl:value-of select="format-number(CUSTOMATTRIB[@ID='CUST_UNITPRICE']/@VALUE, '#.00')"/>
		</td>
		<td align="right">
			<xsl:value-of select="CUSTOMATTRIB[@ID='CUST_QUANTITY']/@VALUE"/>
		</td>		
		<td align="right">
			<xsl:value-of select="format-number(CUSTOMATTRIB[@ID='CUST_QUANTITY']/@VALUE * CUSTOMATTRIB[@ID='CUST_UNITPRICE']/@VALUE, '#.00')"/>
		</td>
	</xsl:template>
</xsl:stylesheet>
My knowledge of XSLT is a bit rusty and I'm quite certain that there are more elegant solutions but that should be enough to get you started.

Please note that I'm using XSLT 2.0 because in version 1.0 it's a pain in the behind creating the desired total sum.
Attachments
Custom_attribute_example.tdl
(5.33 KiB) Downloaded 29 times
Last edited by zonfeld on Sun Apr 30, 2023 5:25 pm, edited 1 time in total.

ispyridis
Posts: 95
Joined: Fri Apr 07, 2023 7:05 pm

Re: how to access Custom Attributes

Post by ispyridis » Sun Apr 30, 2023 5:15 pm

Hi @zonfeld thanks for the fast response this is exactly what i was looking for. Could you please zip the xsl and upload?
I copied the code in notepad and saved as xsl and it produces unknown error

User avatar
zonfeld
Posts: 173
Joined: Sun Mar 21, 2021 11:41 am
Contact:

Re: how to access Custom Attributes

Post by zonfeld » Sun Apr 30, 2023 5:23 pm

Happy to hear you're finding it helpful. Let's see if the ZIP file works...

What XSLT processor are you using? Maybe it's a compatibility issue. I ran it using an old version of XML Spy.
Attachments
Custom_attribute_handling.zip
(929 Bytes) Downloaded 31 times

ispyridis
Posts: 95
Joined: Fri Apr 07, 2023 7:05 pm

Re: how to access Custom Attributes

Post by ispyridis » Sun Apr 30, 2023 5:40 pm

i tried i w3schools tryit editor

also i use jaxe to check for errors it doesn't seem to understand it
2023-04-30_20h38_58.jpg

User avatar
zonfeld
Posts: 173
Joined: Sun Mar 21, 2021 11:41 am
Contact:

Re: how to access Custom Attributes

Post by zonfeld » Sun Apr 30, 2023 5:43 pm

Okay, I see. It's picky (rightfully so, I guess) about the representation of the HTML elements. Let me fix that...

ispyridis
Posts: 95
Joined: Fri Apr 07, 2023 7:05 pm

Re: how to access Custom Attributes

Post by ispyridis » Sun Apr 30, 2023 5:52 pm

did you try it in TDL?
TDL does not read .xslt it needs .xsl

the stylesheet version of your file is 2.0

tdl's examples do not state version of stylesheet

what i can do to downgrade? will the same commands work in earlier version?

User avatar
zonfeld
Posts: 173
Joined: Sun Mar 21, 2021 11:41 am
Contact:

Re: how to access Custom Attributes

Post by zonfeld » Sun Apr 30, 2023 7:13 pm

ispyridis wrote:
Sun Apr 30, 2023 5:52 pm
did you try it in TDL?
I just did. I expected it to rely on XSLT 1.0 and that seems to be the case. It fails.

Consequently, I changed two things to run the tranformation in ToDoList: In the XSLT file,
  • I set the XSLT version to 1.0
    (<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">) and
  • I removed the line that calculates the sum
    <xsl:value-of select="format-number(sum(//TASK[CUSTOMATTRIB]/(CUSTOMATTRIB[@ID='CUST_QUANTITY']...)
That's not helping you much since the sum is what you're after but at least you'll experience that the transformation basically is able to be processed.
ispyridis wrote:
Sun Apr 30, 2023 5:52 pm
what i can do to downgrade? will the same commands work in earlier version?
No, they will not. Or specifically, the sum() function will not. I used XSLT 2.0 because building the sum is not possible in XSLT 1.0 like that. It is quite painful there and knowing your way around XSLT/XPath is required to understand what's going on. Take a look:
XSLT 1.0 : sum of math calculations of sub-elements

To hopefully fix the problem with your XML editor, I've changed the XSLT file so that all HTML elements and attributes are constructed with xsl:element or xsl:attribute. I cannot remember if, why, and when that is necessary but it cannot hurt to do it this way. ;)

To see that the transformation actually works in XSLT 2.0, try it here: https://xslttest.appspot.com/

XSLT_online_test_tool.jpg

I suggest you play around with it a bit and then try to get to the point where
  • you know enough to translate it to XSLT 1.0,
  • or run it in an environment that supports XSLT 2.0 (The free version of Saxon, for example, which you can still call it from ToDoList with a user-defined tool),
  • or maybe you can get @abstr to support XSLT 2.0.
As you might have found out already, XSLT isn't for the faint of heart but if you're desperate or determined enough, you'll make it work. :)
Attachments
Custom_attribute_handling.zip
(947 Bytes) Downloaded 32 times

User avatar
abstr
Site Admin
Posts: 368
Joined: Sun Jul 28, 2019 12:22 pm

Re: how to access Custom Attributes

Post by abstr » Mon May 01, 2023 1:27 am

zonfeld wrote:
Sun Apr 30, 2023 4:37 pm
I'm not allowed to attach XSLT files, it seems
Fixed.

User avatar
abstr
Site Admin
Posts: 368
Joined: Sun Jul 28, 2019 12:22 pm

Re: how to access Custom Attributes

Post by abstr » Mon May 01, 2023 1:29 am

zonfeld wrote:
Sun Apr 30, 2023 7:13 pm
maybe you can get @abstr to support XSLT 2.0.
I'm using the MSXML6 (Microsoft) component to do the transformations.

I'll investigate what else I need to do to support XSLT 2.0... Oh dear

Perhaps an alternative transformation tool can be set up as a UDT, or we may need some new preferences for specifying a tool which the app will use in place of MSXML6...

Post Reply