XSLT 2.0 How to do counters and variables across various loops and structure - mutable

So, I know you can't access variables outside scope, that they're immutable, that XSLT is functional not imperative, etc...
But I need a general purpose approach to something that would be trivial with global mutable variables (that sounds evil just saying it :). Here's an example...
<xsl:template match="t1">
<xsl:if test="someLogic">
<!-- I know, can't do this but just to explain... -->
<xsl:variable name="varName">numberOrText, maybe even some arithmetic like $varName+1</xsl:variable>
</xsl:if>
</xsl:template>
<xsl:template match="t2">
<xsl:value-of select="$varName"/>
</xsl:template>
And the challenge is there could be any number of templates like t1 and t2 at any time during processing and templates that both modify and use the variable.
Maybe part of the problem is that the values depends on the order of processing, but that's deliberate--that's what is required.
One possibility I thought of was to pass the values everywhere as parameters. But the problem is that one leaf template might need to change it, and then processing goes back up... it loses that change. Unless there is some way for a template to return parameters and then I could pass those returned parameters on? Thinking of a general purpose pure functional programming language, it seems like that's how one might do this--calling recursively, but using the return values for further calls so one can "carry forward" the values.
I have seen this done using extensions--calling out to Java methods or something like that and then you can have global mutable values, but... I'd really rather not "cheat" like that.
Any pointers, ideas, etc., welcome.

I think the answer was included in my question and other comments. Here's quoting some of it:
"some way for a template to return parameters and then I could pass those returned parameters on? Thinking of a general purpose pure functional programming language, it seems like that's how one might do this--calling recursively, but using the return values for further calls so one can "carry forward" the values."
From user357812: "I think that maybe this can be done with a most fine-grained tree traversal pattern and tunnel param. Only with input and output it's posible to say exactly how."

Related

How to style parts of i18n messages when using thymeleaf

I'm not sure this is the right place to ask this. I would like to know how best to style parts of messages from l10n properties files. For example, my client want this message and formatting in a help window:
This is a self-assessment and comparison application.
Simplest solution would be to include the HTML tags in the messages.properties entry for this label. The problem with that is that the 40 translators that will process the messages.properties are bound to make mistakes like deleting the <, translating the attributes or styles of the HTML markup etc. Also it makes maintaining the markup and styling difficult for the devs.
Any better way to do this?
The solution I've seen typically done just uses th:utext with HTML tags in the .properties files. I would opine it does create a maintenance hassle as you mention and should be kept to a minimum.
One workaround is to create separate strings in some cases, like:
<span th:text=#{thisIsA}>This is a </span><strong><span th:text="#{selfAssessment}">self-assessment</span></strong>
However, this is error-prone since certain languages may change the order of the words. So that's not a great option.
If the HTML tags specifically are an issue, another way albeit somewhat ugly could be:
thisIsASelfAssessment=This is a {0}self-assessment{1}.
Or even
thisIsA=This is a {0}.
selfAssessment=self-assessment
But that might be confusing for the next developer reading it and may introduce the same issue you have with the 40 translators looking at it since you have curly braces. It also all becomes very tedious and generates more lines.
So in the end, you're likely best going with the simplest solution of utext.
Project-wise, you could have the initial translation done without the markup and add the markup in after they are done with a first pass at translating it. The issue may arise in the future when you need to change strings, but doing this would minimize some headache. It could make sense to keep these strings in a separate block in the .properties file so you can target them later.
Good question as I've had this issue myself.

Parameterized sub-flow

Is it possible to create a parameterized sub-flow in Mule? I have to do the same complex process twice, almost exactly the same, with a few minor differences. Is the best way to do this to set a bunch of flow variables in advance of the flow reference and then use those variables in the flow I want to parameterize, or is there simpler way?
You are in a correct direction. IF you have a same piece of functionality you want to operate with different set's of data, go with sub-flows. The approach you have mentioned works and it is the best possible I can see.

General stategy for designing Flexible Language application using ANTLR4

Requirement:
I am trying to develop a language application using antlr4. The language in question is not important. The important thing is that the grammar is very vast (easily >2000 rules!!!). I want to do a number of operations
Extract bunch of informations. These can be call graphs, variable names. constant expressions etc.
Any number of transformations:
if a loop can be expanded, we go ahead and expand it
If we can eliminate dead code we might choose to do that
we might choose to rename all variable names to conform to some norms.
Each of these operations can be applied independent of each other. And after application of these steps I want the rewrite the input as close as possible to the original input.
e.g. So we might want to eliminate loops and rename the variable and then output the result in the original language format.
Questions:
I see a need to build a custom Tree (read AST) for this. So that I can modify the tree with each of the transformations. However when I want to generate the output, I lose the nice abilities of the TokenStreamRewriter. I have to specify how to write each of the nodes of the tree and I lose the original input formatting for the places I didn't do any transformations. Does antlr4 provide a good way to get around this problem?
Is AST the best way to go? Or do I build my own object representation? If so how do I create that object efficiently? Creating object representation is very big pain for such a vast language. But may be better in the long run. Again how do I get back the original formatting?
Is it possible to work just on the parse tree?
Are there similar language applications which do the same thing? If so what strategy do they use?
Any input is welcome.
Thanks in advance.
In general, what you want is called a Program Transformation System (PTS).
PTSs generally have parsers, build ASTs, can prettyprint the ASTs to recover compilable source text. More importantly, they have standard ways to navigate/inspect/modify the ASTs so that you can change them programmatically.
Many offer these capabilities in the form of pattern-matching code fragments written in the surface syntax of the language being transformed; this avoids the need to forever having to know excruciatingly fine details about which nodes are in your AST and how they are related to children. This is incredibly useful when you big complex grammars, as most of our modern (and our legacy languages) all seem to have.
More sophisticated PTSs (very few) provide additional facilities for teasing out the semantics of the source code. It is pretty hard to analyze/transform most code without knowing what scopes individual symbols belong to, or their type, and many other details such as data flow. Full disclosure: I build one of these.

Programming constructs

A wise man told me that to learn how a syntax works does not mean your a good programmer, but rather to grasp programming constructs like iterators and conditionals, thus, meaning you can pick up any syntax easier.
How would one go about learning these constructs??
The easiest construct you mention is a conditional.
The basic pattern of a conditional is:
if <some-condition> then
<do-action>
else
<do-other-action>
end if
This basic pattern is expressed in many different ways according to the language of choice, but is the basic decision-making building block of any program.
An iterator is a construct which abstracts the physical layout of a data structure, allowing you to iterate (pass through) it without worrying about where in memory each element in the data structure is.
So, for example, you can define a data structure such as any of Array, Vector, Deque, Linked List, etc.
When you go to iterate, or pass through the data structure one element at a time, the iterator presents you with an interface in which each element in the data structure follows sequentially, allowing you to loop through with a basic for loop structure:
for <element> in <data-structure>
<do-action>
end loop
As for other constructs, take a look at some books on Data Structures and Algorithms (usually a 2nd-year level computer science course).
Syntax is only a technical form of expressing your solution. The way you implement and the concepts you use in your solution are the ones who makes the different between a beginner and an experienced developer. Programming languages are the means not the wits !

Best Practices in Building and Processing Macros

I'm building an IOS application, and would like to offer the user the ability to use macros for different aspects of the system.
For example, I might have a simple macro this this:
{include name="some name" pre="some it of htmk" post="some other bit of html"}
That would include the contents of the item named "some name" in the body of the document the user is working on.
or I might have something more complex like this:
{notesForTag name={ListAllTags pre="some bit of html" post="some other bit of html"} pre="..." post="..."}
Which would list all the documents in the system grouped by tag.. the ability to add on data (like html) at the beginning and the end of each tag returned would allow the user, for example, to format the response as a table, or use particular styling, etc.
Conceptually, I know how I want this to work, but I'm wondering if there are any macro construction and processing best practices out there that would help me on my way. Anything geared towards Objective C / IOS would be most helpful.
Edit: To add some clarity here, what I'm looking to discover is an efficient and accurate way to parse something like this. Having parsed things, I think the rest will be fairly straightforward.
Thank you.
NSScanner would probably work well for parsing something like this, perhaps with a recursive function to allow nested macros like the second one. You may also want to consider using XML to represent your macros, which would allow you to use NSXMLParser to parse it, so you only have to worry about the content and not the structure.