XSLT 1.0 - change the order of lines - xslt

I want to change the order of line cac:InvoiceLine depending on this node:
cac:AdditionalItemProperty/cbc:Value
All InvoiceLines that have Item type=RC must be gruop at the end of lines, and all that have CU must be on the top.

If the mentioned values are the only ones you are concerned about, then it seems like you could just sort alphabetically by that value; see xsl:sort. You could just put this inside the xsl:for-each or xsl:apply-templates where you process your invoice lines:
<xsl:sort select="cac:AdditionalItemProperty/cbc:Value" />
On the other hand, if you only want to output only the line items with the mentioned values, you could select them separately. For example, assuming you have a template which matches your invoice lines, you'd first apply it to the 'CU' ones and then to the 'RC' ones:
<xsl:apply-templates select="cac:InvoiceLine[cac:AdditionalItemProperty/cbc:Value='CU']" />
<xsl:apply-templates select="cac:InvoiceLine[cac:AdditionalItemProperty/cbc:Value='RC']" />

Related

Manage flags in xslt?

All,
i am searching in a list of fields those who has the type clob and i am writing it separed by a comma like this [field1, field2, ... fieldn]
my problem is how to identify the first matched field to write it without comma ( i can't use position() because the first field matched can be the first of the list or the last of the list)
I want to make this algorithm in xslt,
variable is_first = TRUE;
if(is_first) {
do smthng;
isfirst = False;
}
Actually it is not possible to make something like this in xslt since variable are immutable. There probably could be workarounds but you have to specify your need in more details.
edit:
If your input is string with values separated by commas...
<xsl:variable name="inputString" select="'field1,field2,field3a,field4,field3b'" />
... you could use tokenize() functions...
<xsl:variable name="tokenized" select="tokenize($inputString, ',')" />
... and then select items corresponding to your condition
<!-- Select item corresponding to condition (e.g. it contains 3). Take first one if there are several such items -->
<xsl:value-of select="$tokenized[contains(., '3')][1]" />
Edit2:
You can use separator attribute of xsl:value-of (xslt 2.0) for output of delimited values.
Assuming following variable
<xsl:variable name="list">
<item>first</item>
<item>second</item>
<item>third</item>
</xsl:variable>
this <xsl:value-of select="$list/item" separator="," /> makes desired output first,second,third
You need to write this using functional code rather than procedural code. It's not possible to do the conversion without seeing the context (it's much easier to work from the problem rather than from the solution in a lower-level language).
But the most common equivalent in XSLT would take the form
<xsl:for-each select=".....">
<xsl:if test="position() = 1"><!-- first time code --></xsl:if>
....
</xsl:for-each>

Setting a variable that counts the number of items in a for-each loop in XSLT

I need to set a variable that is total number of "Line Items" in each order in the following for-each loop:
<xsl:for-each select="Customer/Order/Item">
</xsl:for-each>
For instance, if an order has:
1X SKI GLOVES $4.99
3X TACOS $5.99
2X SNOWBOARDS $6.99
Therefore the number of line items in this order is 3.
I would like the variable to output "3". I will use this variable in the for-each loop to divide another number... Basically I am looking for something that outputs the number of line items to a variable. It should probably look like:
<xsl:for-each select="Customer/Order/Item">
<xsl:variable name="lineitemqty" select="# OF LINE ITEMS" />
<xsl:value-of select="$lineitemqty" /><xsl:text> </xsl:text>
</xsl:for-each>
Where $lineitemqty = 3 if the loop is processing the order above...
Because you are looping over all Item ( for all Order and for all Customer) and (as I understood) you like to know how many Items are belong to the current Order, this it what you are looking for.
<xsl:variable name="lineitemqty" select="count(../Item)" />
Just use (outside of the loop):
<xsl:variable name="lineitemqty" select="Customer/Order/Item"/>
If you want to know this number inside the loop, simply reference the (outer-level) variable $lineitemqty.
Alternatively, inside the loop you may use the function last():
<xsl:variable name="lineitemqty" select="last()"/>

XSL 1.0 - sort a list from a different list

i'm new with XSL and have tried to look through all the examples on here but none match my problem.
i have a sort order list of movies (order from left to right)
<movies>movieF,movieC,movieG</movies>
now i want to take that sort order list and sort on top of this huge movies list of mine
<moviesList>
<movie>movieA</movie>
<movie>movieB</movie>
<movie>movieC</movie>
<movie>movieD</movie>
<movie>movieE</movie>
<movie>movieF</movie>
<movie>movieG</movie>
<movie>movieH</movie>
</moviesList>
result i want:
<moviesList>
<movie>movieF</movie>
<movie>movieC</movie>
<movie>movieG</movie>
<movie>movieA</movie>
<movie>movieB</movie>
<movie>movieD</movie>
<movie>movieE</movie>
<movie>movieH</movie>
</moviesList>
would someone please give me some guidance of how to achieve such thing. i've tried to create a variable $sortlist, and then add delimited character around and then use substring-before trick on the sort. result is my sorted list did show up on top before the rest of the movies but it's not on the right order. please help.
Try this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="sortlist" select="';movieF;movieC;movieG;'"/>
<xsl:template match="moviesList">
<xsl:copy>
<xsl:for-each select="*[contains($sortlist, concat(';',.,';'))]">
<xsl:sort select="substring-before($sortlist,concat(';',.,';'))" />
<xsl:copy-of select="."/>
</xsl:for-each>
<xsl:copy-of select="*[not(contains($sortlist, concat(';',.,';')))]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Notice, I didn't use string-length to sort, I just used the string itself. XSLT1.0 can sort alphabetically instead of numerically, which would place 14 above 7, resulting in the order movieF, movieG, movieC.
It actually uses the portion of the list of movie names that comes before each one as the sort key; alphabetically speaking, ;movieF; comes before ;movieF;movieC;, therefore it'll place movieC above movieG when sorting.
Personally I tend to avoid using commas as a separator as they can be used in some names, such as 'Crouching Tiger, Hidden Dragon'. A semicolon's far less likely, but you could use any character you like as long as it's not one that appears in a movie name.

Break the XSLT for-each loop when first match is found

I am having trouble to display the first matching value, like
<test>
<p>30</p>
<p>30{1{{23{45<p>
<p>23{34</p>
<p>30{1{98</p>
</test>
<test2>
<p1>text</p1>
</test2>
So i want to loop through the <test></test> and find the value of <p> node whose string length is greater than 2 and that contains 30. I want only the first value.
so i tired the following code
<xsl:variable name="var_test">
<xsl:for-each select="*/*/test()>
<xsl:if string-length(p/text())>2 and contains(p/text(),'30'))
<xsl:value-of select="xpath">
</xsl:variable>
the problem is the var_test is being null always.
if i try directly with out any variable
<xsl:for-each select="*/*/test()>
<xsl:if string-length(p/text())>2 and contains(p/text(),'30'))
<xsl:value-of select="xpath">
I am getting the following output
<p>30{1{23{4530{1{98</p>
but the desired output is
<p>0{1{23{45</p>
so how can i achieve this?
Instead of the for-each, use
<xsl:copy-of select="(*/*/test/p[string-length() > 2 and
contains(.,'30'))] )[1]" />
The [1] selects only the first matching <p>. (Updated: I changed the XPath above in response to #markusk's comment.)
The above will output that <p> element as well as its text content, as shown in your "desired output". If you actually want only the value of the <p>, that is, its text content, use <xsl:value-of> instead of <xsl:copy-of>.
Addendum:
The idea of breaking out of a loop does not apply to XSLT, because it is not a procedural language. In a <xsl:for-each> loop, the "first" instantiation (speaking in terms of document order, or sorted order) of the loop is not necessarily evaluated at a time chronologically before the "last" instantiation. They may be evaluated in any order, or in parallel, because they do not depend on each other. So trying to "break out of the loop", which is intended to cause "subsequent" instantiations of the loop not to be evaluated, cannot work: if it did, the outcome of later instantiations would be dependent on earlier instantiations, and parallel evaluation would be ruled out.

XSL unique value key

Goal
(XSLT 1.0). My goal is to take a set of elements, S, and produce another set, T, where T contains the unique elements in S. And to do so as efficiently as possible. (Note: I don't have to create a variable containing the set, or anything like that. I just need to loop over the elements that are unique).
Example Input and Key
<!-- My actual input consists of a bunch of <Result> elements -->
<AllMyResults>
<Result>
<someElement>value</state>
<otherElement>value 2</state>
<subject>Get unique subjects!</state>
</Result>
</AllMyResults>
<xsl:key name="SubjectKey" match="AllMyResults/Result" use="subject"/>
I think the above works, but when I go to use my key, it is incredibly slow. Below is the code for how I use my key.
<xsl:for-each select="Result[count(. | key('SubjectKey', subject)[1]) = 1]">
<xsl:sort select="subject" />
<!-- Do something with the unique subject value -->
<xsl:value-of select="subject" />
</xsl:for-each>
Additional Info
I believe I am doing this wrong because it slowed down my XSL considerably. As some additional info, the code shown above is in a separate XSL file from my main XSL file. From the main XSL, I am calling a template that contains the xsl:key and the for-each shown above. The input to this template is an xsl:param containing my node-set (similar to the example input shown above).
I can't see any reason from the information given why the code should be slow. It might be worth seeing if the slowness is something that happens on all XSLT processors, or if it's peculiar to one.
Try substituting
count(. | key('SubjectKey', subject)[1]) = 1
with:
generate-id() = generate-id(key('SubjectKey', subject)[1])
In some XSLT processors the latter is much faster.