I'm trying to check if two XSL variables are the same:
<?xml version="1.0" encoding="UTF-8"?>
<files>
<file filename="export.csv" path="/export" active="true" ftp="false">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:php="http://php.net/xsl">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="quote">"</xsl:variable>
<xsl:variable name="sepstart" select="','"/>
<xsl:template match="/">
<xsl:text>"CustomerID"</xsl:text>
<xsl:variable name="second_check">0</xsl:variable>
<xsl:for-each select="orders/order">
<xsl:for-each select="items/item">
<xsl:variable name="output">
<xsl:variable name="first_check"><xsl:value-of select="normalize-space(../../increment_id)"/></xsl:variable>
<xsl:choose>
<xsl:when test="$first_check = $second_check">
Do something...
</xsl:when>
<xsl:otherwise>
Do something...
<xsl:variable name="second_check"><xsl:value-of select="normalize-space(../../increment_id)"/></xsl:variable>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:value-of select="$output" />
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
</file>
</files>
However, it's just skipping over both and not outputting anything.
Can anyone point out what I'm doing wrong?
You cannot "re-declare" variables in XSL. When you write
<xsl:variable name="second_check">
<xsl:value-of select="normalize-space(../../increment_id)"/>
</xsl:variable>
That has no affect, because of this
<xsl:variable name="second_check">0</xsl:variable>
I can't help you though (yet), because I'm not really sure what you're trying to do
Related
These are just three records of my client's xml, but it shows the problem:
<Export>
<Record>
<JournalX>01-252-5390</JournalX>
<PositionX></PositionX>
<DepartmentX></DepartmentX>
<Description><![CDATA[UK - Take The Cash]]></Description>
<Amount>116.66</Amount>
<PayDate>06/30/2022</PayDate>
<PPEndDate>06/30/2022</PPEndDate>
</Record>
<Record>
<JournalX>5200</JournalX>
<PositionX></PositionX>
<DepartmentX>262</DepartmentX>
<Description><![CDATA[UK - Salary]]></Description>
<Amount>3655.92</Amount>
<PayDate>06/30/2022</PayDate>
<PPEndDate>06/30/2022</PPEndDate>
</Record>
<Record>
<JournalX>5200</JournalX>
<PositionX>311</PositionX>
<DepartmentX>310</DepartmentX>
<Description><![CDATA[UK - Salary]]></Description>
<Amount>1424.99</Amount>
<PayDate>06/30/2022</PayDate>
<PPEndDate>06/30/2022</PPEndDate>
</Record>
</Export>
What needs to happen is to sum Amount by Description and AcctNo. As you can see there is not AcctNo in the XML. This is how I created an AcctNo variable:
<xsl:variable name="AcctNo">
<xsl:choose>
<xsl:when test="string-length(JournalX) > 4">
<xsl:value-of select="JournalX" />
</xsl:when>
<xsl:when test="string-length(PositionX) != 3 or PositionX = '' ">
<xsl:value-of select="concat('01-', DepartmentX, '-', JournalX)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('01-', PositionX, '-', JournalX)"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
Alternatively, I created an AcctNo function in the CData section, basically the same algorithm. But the problem with either of these, is that I have to create the Key outside the template definition and the variable is defined within the template, therefore not available to the variable.
An additional problem, at least I think it may be, is that they want the output to be csv.
Can anyone help me? Thank You so much, Greg
I have copied code I found on the web and tried to modify it which is how I found out about the limitation.
Any help would be really appreciated.
Using XSLT 3 with
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="text" indent="yes" html-version="5"/>
<xsl:template match="/Export">
<xsl:for-each-group select="Record" composite="yes" group-by="Description, if (string-length(JournalX) > 4) then JournalX else if (string-length(PositionX) != 3 or PositionX = '') then concat('01-', DepartmentX, '-', JournalX) else concat('01-', PositionX, '-', JournalX)">
<xsl:value-of select="sum(current-group()/Amount)"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
I get
116.66
3655.92
1424.99
using XSLT 1.0 with a two step transformation I get the same result
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output method="text" indent="yes" />
<xsl:key name="group-by-desc-and-acctno" match="Record" use="concat(Description, '|', AcctNo)"/>
<xsl:template match="/Export">
<xsl:variable name="records-with-added-acctno-rtf">
<xsl:apply-templates select="Record"/>
</xsl:variable>
<xsl:variable name="records-with-added-acctno" select="exsl:node-set($records-with-added-acctno-rtf)/Record"/>
<xsl:for-each select="$records-with-added-acctno[generate-id() = generate-id(key('group-by-desc-and-acctno', concat(Description, '|', AcctNo))[1])]">
<xsl:value-of select="sum(key('group-by-desc-and-acctno', concat(Description, '|', AcctNo))/Amount)"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
<xsl:template match="Record">
<xsl:copy>
<xsl:copy-of select="*"/>
<AcctNo>
<xsl:choose>
<xsl:when test="string-length(JournalX) > 4">
<xsl:value-of select="JournalX" />
</xsl:when>
<xsl:when test="string-length(PositionX) != 3 or PositionX = '' ">
<xsl:value-of select="concat('01-', DepartmentX, '-', JournalX)" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="concat('01-', PositionX, '-', JournalX)"/>
</xsl:otherwise>
</xsl:choose>
</AcctNo>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I started learning Linux couple of days ago and currently I'm stuck at XSLT. Sorry if it's a stupid question or already answered elsewhere, I'm quite new here.
My XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<lac xmlns:t="http://smth.de">
<header>
<order id="20210323346730329408"/>
<adress id="IZ0009"/>
</header>
<items>
<item id="1"><material><code>IS-0001-BT-1</code><lotcode/></material><qty>10,000000</qty><expiry/></item>
<item id="2"><material><code>IS-0001-BT-2</code><lotcode/></material><qty>20,000000</qty><expiry/></item>
<item id="3"><material><code>IS-0001-AZ-1</code><lotcode/></material><qty>30,000000</qty><expiry/></item>
<item id="4"><material><code>IS-0001-AZ-2</code><lotcode/></material><qty>40,000000</qty><expiry/></item>
</items>
</lac>
I want to get this output:
Order ID,Adress ID,Item ID,MaterialCode,MaterialLotCode,MaterialQty,Expiry
20210323346730329408,IZ0009,1,IS-0001-BT-1,,10,000000,
20210323346730329408,IZ0009,2,IS-0001-BT-2,,20,000000,
20210323346730329408,IZ0009,3,IS-0001-AZ-1,,30,000000,
20210323346730329408,IZ0009,4,IS-0001-AZ-2,,40,000000,
This is what I got sofar, Your help is very welcome:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:strip-space elements="*" />
<xsl:template match="/">
<xsl:text>Order ID,Adress ID,Item ID,MaterialCode,MaterialLotCode,MaterialQty,Expiry
</xsl:text>
<xsl:apply-templates mode="runHeader"/>
<xsl:apply-templates mode="runItems"/>
</xsl:template>
<xsl:template match="header" mode="runHeader">
<xsl:apply-templates mode="runOrder"/>
<xsl:apply-templates mode="runAdress"/>
</xsl:template>
<xsl:template match="order" mode="runOrder">
<xsl:value-of select="./#id"/>
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="adress" mode="runAdress">
<xsl:value-of select="./#id"/>
<xsl:text>,</xsl:text>
</xsl:template>
<xsl:template match="items" mode="runItems">
<xsl:for-each select="//item">
<xsl:value-of select="./#id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="./material"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="./lotcode"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="./qty"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="./expiry"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Many thanks in advance.
I would suggest a different approach:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/lac">
<!-- header row -->
<xsl:text>Order ID,Adress ID,Item ID,MaterialCode,MaterialLotCode,MaterialQty,Expiry
</xsl:text>
<!-- common data -->
<xsl:variable name="common">
<xsl:value-of select="header/order/#id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="header/adress/#id"/>
<xsl:text>,</xsl:text>
</xsl:variable>
<!-- data rows -->
<xsl:for-each select="items/item">
<xsl:copy-of select="$common"/>
<xsl:value-of select="#id"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="material/code"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="material/lotcode"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="qty"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="expiry"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note that this assumes that each item has at most one material (your XML structure allows for more).
--
P.S. That's not how you spell address.
I know this is asked before but I can't find a simple example. Just trying to change output format. My existing XSLT is this and works but output format is always .txt.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:text>*,</xsl:text>
<xsl:value-of select="//AccountFields/AccountID"/>
<xsl:text>
</xsl:text>
<xsl:for-each select="//TransactionLine">
<xsl:variable name="i" select="position()" />
<xsl:value-of select="//AccountFields/AccountID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="ItemFields/ItemID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="TransactionLineFields/UnitQuantity"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="TransactionLineFields/UnitPrice"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
I tried adding <xsl:result-document href="Order.csv" method="text"> but the file fails to generate completely and the tool I'm using doesn't give me a clear way to see errors.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:result-document href="Order.csv" method="text">
<xsl:text>*,</xsl:text>
<xsl:value-of select="//AccountFields/AccountID"/>
<xsl:text>
</xsl:text>
<xsl:for-each select="//TransactionLine">
<xsl:variable name="i" select="position()" />
<xsl:value-of select="//AccountFields/AccountID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="ItemFields/ItemID"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="TransactionLineFields/UnitQuantity"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="TransactionLineFields/UnitPrice"/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:result-element>
</xsl:template>
</xsl:stylesheet>
xsl:result-document is only available in XSLT 2.0 and later versions.
If you can use XSLT-2.0, you could pass parameters and set the method attribute with an attribute value template to
"xml" | "html" | "xhtml" | "text" | "json" | "adaptive" | eqname
Here, the file extension and the output method are set dynamically by parameters passed to the stylesheet:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="fileExt" select="'.csv'" />
<xsl:param name="fileMethod" select="'xml'" />
<xsl:template match="/">
<xsl:result-document href="{concat('Order',$fileExt)}" method="{$fileMethod}">
<xsl:text>*,</xsl:text>
...
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
It has been tested with Saxon.
If you wanted to change the xsl:output method instead, have a look at this question.
I want to precalculate a subtree of nodes in an source XML, and the process them seperately (because I want the subset to be processed in different ways), and access some values from ancestors.
simple example
<numbers count="5">
<number value="1"/>
<number value="2"/>
<number value="3"/>
<number value="4"/>
<number value="5"/>
</numbers>
and lets say I have an xslt (MSXML) to extract the even nodes somehow
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<evens>
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<even>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="#value"/>
</xsl:attribute>
</even>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
and we get..
<evens>
<even count="5" value="2" />
<even count="5" value="4" />
</evens>
nice...
but how can I seperate the filtering from the processing so something like...
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="calculateNodes">
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<xsl:copy-of select="."/>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="nodes">
<xsl:call-template name="calculateNodes"/>
</xsl:variable>
<evens>
<xsl:for-each select="msxsl:node-set($nodes)/number">
<even>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="#value"/>
</xsl:attribute>
</even>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
this gives.
<evens>
<even count="" value="2" />
<even count="" value="4" />
</evens>
so...the ancestors arent copied.
Is there an idiomatic way to get out of this?
A copied node exists on its own, outside of the original tree. In your example, the parent of number is the $nodes variable, which does not have any attributes.
Why don't you do simply:
<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="/numbers">
<xsl:variable name="nodes" select="number[#value mod 2 = 1]"/>
<evens>
<xsl:for-each select="$nodes">
<even count="{../#count}" value="{#value}"/>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
This way you have a variable containing a reference to the original nodes, instead of a copy. Then you also have access to the original parent. And the content of the variable is a node-set; you don't need to convert it.
this seems to work
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="calculateNodes">
<xsl:for-each select="numbers/number">
<xsl:choose>
<xsl:when test="#value mod 2 = 0">
<numberWrapper>
<xsl:attribute name="count">
<xsl:value-of select="../#count"/>
</xsl:attribute>
<xsl:copy-of select="."/>
</numberWrapper>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template match="/">
<xsl:variable name="nodes">
<xsl:call-template name="calculateNodes"/>
</xsl:variable>
<evens>
<xsl:for-each select="msxsl:node-set($nodes)/numberWrapper">
<even>
<xsl:attribute name="count">
<xsl:value-of select="#count"/>
</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="number/#value"/>
</xsl:attribute>
</even>
</xsl:for-each>
</evens>
</xsl:template>
</xsl:stylesheet>
can I use max function in a variable in XSLT 1?
I need to find a maximum value inside some nodes and I'll need to call this from more places.
So I tried to create a template:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekKarty">
<xsl:value-of
select="max($CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments)" />
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
In XML Spy I get this error:
Error in XPath expression Unknown
function - Name and number of
arguments do not match any function
signature in the static context -
'max'.
What is wrong?
Thanks a lot,
Peter
Use the well known maximum idiom:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:essox="urn:essox-scripts">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template name="Field001_max_dluznych_splatek">
<xsl:param name="CrRep"/>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Installments
/InstDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="PocetDluznychSplatekSplatky">
<xsl:call-template name="maximun">
<xsl:with-param name="pSequence"
select="$CrRep
/Response
/ContractData
/Cards
/CardDetail
/NrOfDueInstalments"/>
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="$PocetDluznychSplatekSplatky
>= $PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="maximun">
<xsl:param name="pSequence"/>
<xsl:for-each select="$pSequence">
<xsl:sort select="." data-type="number" order="descending"/>
<xsl:if test="position()=1">
<xsl:value-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Note: In a named template for reuse.
There is no max function in XSLT 1.0. You can work around this by sorting your elements in descending order and then taking the value of the first one.
Here's another (slower) way to do it:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="PocetDluznychSplatekSplatky"
select="/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]" />
<xsl:variable name="PocetDluznychSplatekKarty"
select="/test/PocetDluznychSplatekKarty/val[not(../val > .)][1]" />
<xsl:template match="/">
<xsl:choose>
<xsl:when
test="$PocetDluznychSplatekSplatky >=
$PocetDluznychSplatekKarty">
<xsl:value-of select="$PocetDluznychSplatekSplatky" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$PocetDluznychSplatekKarty" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Sample source document:
<test>
<PocetDluznychSplatekSplatky>
<val>22</val>
<val>3241</val>
<val>13</val>
</PocetDluznychSplatekSplatky>
<PocetDluznychSplatekKarty>
<val>1</val>
<val>3234341</val>
<val>13</val>
</PocetDluznychSplatekKarty>
</test>
Output:
3234341
The XPath in each variable's select looks like this:
/test/PocetDluznychSplatekSplatky/val[not(../val > .)][1]
Or, select the val element having no val siblings with a greater value (i.e. the max).
(Obviously, you'll need to adjust the XPath to fit your source document.)
Note: The sort solution performs much better (assuming an n*log(n) sort implementation). The second approach needs to compare each val to every one of its siblings and is therefore quadratic.