how to extract digit from string in xslt?
I have most title looks like:
Cumlaude S Ferulic Serum Bi Gel 30ml
Cumlaude Sunlaude Pediatrics Baby Spray Emulsion Spf50+ 200ml
Cumlaude Mucus Duplo Lubricating Gel 2x30ml
How Can I extrac only capacity?
example: 30ml
<xsl:element name="name">
<xsl:value-of select="name"/>
</xsl:element>
sample:
<xsl:element name="name">
Cumlaude S Ferulic Serum Bi Gel 30ml
</xsl:element>
Result:
30ml
Related
Searched but could not find any related posts - hope someone can help.
I am trying to figure out how to get the difference of the sum of all credits and the sum all debits for a given company - this generates only one row in a journal.
The third field establishes whether the amount is a debit or a credit in the example below.
The file contains a number of fields (pipe delimited) but only ones needed here are the following:
* Col1 = Companies
* Col2 = Amount (All positive values)
* Col3 = Amount Type containing Credits or Debits ("C" or "D" values)
File Example:
----------------
A|200.00|D
A|250.00|C
A|100.00|D
B|50.00|D
B|25.00|D
C|20.00|D
C|25.00|C
C|10.00|D
C|5.00|D
The rows for these sums should be: sum(Debits) - sum(Credits)
Company A should = 50.00 (300.00 - 250.00)
Company B should = 75.00 (75.00 - 0.00)
Company C should = 10.00 (35.00 - 25.00)
How can I fix the calculation? --toward bottom of code.
The code I have sums up the total amount without distinguishing between credits and debits, but I need the difference between those two sums.
For company "A", the following code yields 550.00 instead of the desired 50.00 of sum(Debits) - sum(Credits)
<xsl:value-of select="sum(currentgroup()/col2)"/>
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="root">
<!-- TODO: Auto-generated template -->
<root>
<!-- All companies except "A" Company -->
<xsl:for-each-group select="row[col1 !='A']" group-by ="col1">
<journal>
<xsl:for-each select="current-group()">
<row>
<Company>
<xsl:value-of select="col1"/>
</Company>
<Amount>
<xsl:value-of select="col2"/>
</Amount>
<AmountType>
<xsl:value-of select="col3"/>
</AmountType>
</row>
</xsl:for-each>
</journal>
</xsl:for-each-group>
<!-- only for "A" -->
<journal>
<xsl:for-each-group select="row" group-by ="col1">
<xsl:choose>
<!-- The details from input file for Company "A" -->
<xsl:when test="current-group()/col1='A'">
<xsl:for-each select="current-group()">
<row>
<Company>
<xsl:value-of select="col1"/>
</Company>
<Amount>
<xsl:value-of select="col2"/>
</Amount>
<AmountType>
<xsl:value-of select="col3"/>
</AmountType>
</row>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:for-each select="current-group()[1]">
<row>
<Company>
<xsl:value-of select="'A'"/>
</Company>
<Amount>
<xsl:value-of select="sum(current-group()/col2)"/>
</Amount>
<AmountType>
<xsl:value-of select="col3"/>
</AmountType>
</row>
</xsl:for-each>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</journal>
</root>
</xsl:template>
</xsl:stylesheet>
You are currently doing this to get the sum... (or rather, you are doing currentgroup() without the hyphen in your XSLT, which is a typo).
<xsl:value-of select="sum(current-group()/col2)"/>
Assuming col3 holds either C or D, then you can do this
<xsl:value-of select="sum(current-group()[col3 = 'C']/col2) - sum(current-group()[col3 = 'D']/col2)"/>
I'm looking for the other references regarding the transformation of XML file to Flat File format and I have seen many of them. I've tried some of the codes that I saw over the internet and it helps a lot. I tried to do my own XSLT file and I can't get what I want in my output. Also, I need to minimize my coding in the XSLT since I have a lot of coding and condition to applied from Header Record, Detail/Contra Record and Trailer. The value of the header record is correct, however, 2nd and 3rd row of the current output is incorrect. I need to populate for every Transaction there should have 1 detail and 1 Contra. The output should look like what's on the expected output.
Thank you.
SAMPLE XML FILE
<SyncCreditTransfer xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" releaseID="9.2" versionID="2.12.3" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.12.x/InforOAGIS/BODs/SyncCreditTransfer.xsd">
<Application>
<Sender>
<LogicalID>company department</LogicalID>
</Sender>
<CreationDateTime>2016-07-01T05:50:16.208Z</CreationDateTime>
</Application>
<Data>
<Sync>
<ID>1122EDF6394</ID>
<EntityID>SampleFiele</EntityID>
</Sync>
<Record>
<Header>
<DateTime>2016-07-01T05:51:16</DateTime>
</Header>
<Payment>
<DisplayID>Payment1: 09459732</DisplayID>
<DebtorParty>
<FinancialAccount>
<ID>11111</ID>
</FinancialAccount>
</DebtorParty>
<Transaction sequence="1">
<TransactionID>BOA-t-121212</TransactionID>
<InstructedAmount currencyID="EUR">123.43</InstructedAmount>
<CreditorParty>
<FinancialAccount>
<ID>AAAAA</ID>
</FinancialAccount>
</CreditorParty>
</Transaction>
<Transaction sequence="1">
<TransactionID>BOA-t-343434</TransactionID>
<InstructedAmount currencyID="GBP">123.43</InstructedAmount>
<CreditorParty>
<FinancialAccount>
<ID>BBBBB</ID>
</FinancialAccount>
</CreditorParty>
</Transaction>
</Payment>
<Payment>
<DisplayID>Payment2: 12435435</DisplayID>
<DebtorParty>
<FinancialAccount>
<ID>22222</ID>
</FinancialAccount>
</DebtorParty>
<Transaction sequence="1">
<TransactionID>BOA-t-090909</TransactionID>
<InstructedAmount currencyID="EUR">123.43</InstructedAmount>
<CreditorParty>
<FinancialAccount>
<ID>AAAAA</ID>
</FinancialAccount>
</CreditorParty>
</Transaction>
<Transaction sequence="1">
<TransactionID>BOA-t-878787</TransactionID>
<InstructedAmount currencyID="GBP">123.43</InstructedAmount>
<CreditorParty>
<FinancialAccount>
<ID>BBBBB</ID>
</FinancialAccount>
</CreditorParty>
</Transaction>
</Payment>
</Record>
</Data>
XSLT FILE
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="myfunc">
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:function name="func:trunc">
<xsl:param name="str"/>
<xsl:param name="len"/>
<xsl:value-of select="substring($str,1,$len)"/>
</xsl:function>
<xsl:template match="/">
<!-- Start of Header Record -->
<xsl:element name="UserHeadLabel">
<xsl:text>UHL</xsl:text>
</xsl:element>
<xsl:element name="Constant01">
<xsl:text>1</xsl:text>
</xsl:element>
<xsl:element name="Filler01">
<xsl:text> </xsl:text>
</xsl:element>
<xsl:element name="PaymentDate">
<xsl:if test="//*:Header/*:DateTime[normalize-space()]!=''">
<xsl:value-of select="func:trunc(//*:Header/*:DateTime,5)"/>
</xsl:if>
</xsl:element>
<xsl:element name="Constant02">
<xsl:text>999999</xsl:text>
</xsl:element>
<xsl:element name="Filler02">
<xsl:text> </xsl:text>
</xsl:element>
<xsl:element name="CurrencyCode">
<xsl:choose>
<xsl:when test="//*:Payment/*:Transaction/*:InstructedAmount/#currencyID[normalize-space()]!='' and //*:Payment/*:Transaction/*:InstructedAmount/#currencyID='EUR'">
<xsl:text>01</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>00</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:element name="Constant03">
<xsl:text>000000</xsl:text>
</xsl:element>
<xsl:element name="Constant04">
<xsl:text>1 DAILY </xsl:text>
</xsl:element>
<xsl:element name="FileNumber">
<xsl:text>001</xsl:text>
</xsl:element>
<xsl:element name="Filler03">
<xsl:text> </xsl:text>
</xsl:element>
<xsl:element name="Optional01">
<xsl:text> </xsl:text>
</xsl:element>
<xsl:element name="Optional02">
<xsl:text> </xsl:text>
</xsl:element>
<xsl:element name="UserOptional">
<xsl:text>000000000000</xsl:text>
</xsl:element>
<xsl:text>
</xsl:text>
<!-- End of Header Record -->
<!-- Start of Detail Record -->
<xsl:element name="DestinationSortCodeNo">
<xsl:if test="//*:Payment/*:Transaction/*:CreditorParty/*:FinancialAccount/*:ID[normalize-space()]!=''">
<xsl:value-of select="//*:Payment/*:Transaction/*:CreditorParty/*:FinancialAccount/*:ID"/>
</xsl:if>
</xsl:element>
<xsl:element name="DestinationAccountNo">
<xsl:if test="//*:Payment/*:Transaction/*:TransactionID[normalize-space()]!=''">
<xsl:value-of select="//*:Payment/*:Transaction/*:TransactionID"/>
</xsl:if>
</xsl:element>
<xsl:element name="Zero01">
<xsl:text>0</xsl:text>
</xsl:element>
<xsl:element name="TransactionCode">
<xsl:text>99</xsl:text>
</xsl:element>
<xsl:text>
</xsl:text>
<!-- End of Detail Record -->
<!-- Start of Contra Record -->
<xsl:element name="UserSortCodeNo1">
<xsl:if test="//*:Payment/*:DebtorParty/*:FinancialAccount/*:ID[normalize-space()]!=''">
<xsl:value-of select="//*:Payment/*:DebtorParty/*:FinancialAccount/*:ID"/>
</xsl:if>
</xsl:element>
<xsl:element name="UserAccountNo1">
<xsl:if test="//*:Payment/*:DisplayID[normalize-space()]!=''">
<xsl:value-of select="//*:Payment/*:DisplayID"/>
</xsl:if>
</xsl:element>
<xsl:element name="Zero01">
<xsl:text>0</xsl:text>
</xsl:element>
<xsl:element name="TransactionCode">
<xsl:text>17</xsl:text>
</xsl:element>
<xsl:text>
</xsl:text>
<!-- End of Contra Record -->
</xsl:template>
CURRENT OUTPUT
UHL1 2016-999999 010000001 DAILY 001 000000000000
AAAAA BBBBB CCCCC DDDDDBOA-t-121212 BOA-t-343434 BOA-t-090909 BOA-t-878787099
11111 22222Payment1: 09459732 Payment2: 12435435017
EXPECTED OUTPUT
UHL1 2016-999999 010000001 DAILY 001 000000000000
AAAAAABOA-t-12099
11111MPayment1017
BBBBBMBOA-t-34099
11111MPayment1017
CCCCCMBOA-t-09099
22222MPayment2017
DDDDDMBOA-t-87099
22222MPayment2017
Explanation: The value AAAAAA comes from the Payment/Transaction/CreditorParty/FinancialAccount/ID and should only have 6 characters. The BOA-t-12 comes from the Payment/Transaction/TransactionID and this field should only have 8characters. 0 is the hardcoded value, as well as, the value 99. On the next line, the 11111M comes from the Payment/DebtorParty/FinancialAccount/ID, Payment1 is from the Payment/DisplayID and 0 and 17 are the hardcoded value. From the next line and soon, it will only repeat the process and this time the value will be get from the next occurrence of Payment/Transaction.
For every occurrence of Payment/Transaction, it will create 1 Detail record and 1 Contra record. In my example, I have 4 Transaction, and the output should have:
Detail - 1st occurrence of Transaction
Contra - 1st occurrence of Transaction
Detail - 2nd occurrence
Contra - 2nd occurrence
Detail - 3rd occurrence
Contra - 3rd occurrence
Detail - 4th occurrence
Contra - 4th occurrence
This is a fixed-length format.
Try this as your starting point:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://schema.infor.com/InforOAGIS/2">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/SyncCreditTransfer">
<!-- Start of Header Record -->
<!-- skipped for the purpose of this example -->
<!-- End of Header Record -->
<!-- Records -->
<xsl:for-each select="Data/Record/Payment/Transaction">
<!-- Start of Detail Record -->
<xsl:value-of select="substring(CreditorParty/FinancialAccount/ID, 1 , 6)"/>
<xsl:value-of select="substring(TransactionID, 1 , 8)"/>
<xsl:text>099
</xsl:text>
<!-- End of Detail Record -->
<!-- Start of Contra Record -->
<xsl:value-of select="../DebtorParty/FinancialAccount/ID"/>
<xsl:value-of select="../DisplayID"/>
<xsl:text>017
</xsl:text>
<!-- End of Contra Record -->
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
This still needs more work on the "contra" record, but you have not explained that part.
Note:
the use of xpath-default-namespace to handle the namespace used by your input;
the use of xsl:for-each to create a record for each transaction;
Note also that when the output method is text, using xsl:element makes no sense.
Using XSLT 2.0 in Saxon-PE 9.2.0.6, I need to pull from an Excel data sheet.xml into an element ‘w:gridSpan’ the number of spanned columns in Row 2 using the values from the sheet’s mergecells element. Can anyone give me a suggestion on how to do this?
The input XML is this:
<row r="2" spans="1:6" ht="33" customHeight="1">
<c r="A2" s="6" t="s">
<v>33</v>
</c>
<c r="B2" s="12" t="s">
<v>0</v>
</c>
<c r="C2" s="13"/>
<c r="D2" s="13"/>
<c r="E2" s="13"/>
<c r="F2" s="13"/>
</row>
…………
<mergeCells count="2">
<mergeCell ref="B1:F1"/>
<mergeCell ref="B2:F2"/>
</mergeCells>
The portion of my XSLT pertaining to the input XML:
<w:tc>
<w:tcPr>
<w:vAlign w:val="bottom"/>
<xsl:choose>
<xsl:when test="**e:sheetData/e:row[position() = 2]**">
<xsl:element name="w:gridSpan">
<xsl:attribute name="w:val">
<xsl:value-of select="**dg3:get_column_span**"/>
</xsl:attribute>
</xsl:element>
</xsl:when>
</xsl:choose>
Desired output:
<w:tc>
<w:tcPr>
<w:vAlign w:val="bottom"/>
<w:gridSpan w:val="1"/>
</w:tcPr>
I have also had suggested use of the following two functions, but do not know how to implement them to obtain the number of columns spanned in row 2 example of spreadsheet rows 1 and 2, and the suggested 2 funcs follow:
A B C D E F
Optimum Fixed Income Fund Class A
Year Ended
<xsl:function name="dg3:get_column_span">
<xsl:param name="cell"/>
<xsl:choose>
<xsl:when test="$mergeCells[e:mergeCell[matches(#ref, concat('^',$cell/#r,':'))]]">
<xsl:variable name="merge"
select="$mergeCells//e:mergeCell[matches(#ref, concat('^',$cell/#r,':'))]"/>
<xsl:value-of
select="dg3:get_column_number(replace($merge/#ref, '([^:]+):([^;]+)','$2')) -
dg3:get_column_number(replace($merge/#ref, '([^:]+):([^;]+)','$1')) + 1"
/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:function>
<xsl:function name="dg3:get_column_number">
<xsl:param name="range"/>
<xsl:value-of
select="string-to-codepoints(substring($range,1,1)) -
string-to-codepoints('A') + 1"/>
</xsl:function>
Not sure where you are trying to start from, which would dictate the precise route but:
<xsl:attribute name="w:val">
<xsl:value-of select="mergeCells/#count"/>
</xsl:attribute>
Gets that 2 for you...
Also I'm confused why you are using when/choose here and it isn't a straight template match.
I have an XSLT file that is collecting information from an XML document, and then presenting it out to a CSV file.
Presenting all of the information works fine, however what I need to do now is to sum all information of a node i:totalDurationInSeconds where i:issueBatchNumber = D12345
This is the code I have so far
<xsl:template match="/">
<Textblock>Batch Number,Formula Code,Formula Name,Material Code,Material Name,Final Weight, Target Weight, Date, Material Weight, Job Name, Started At, Finished At</Textblock>
<xsl:value-of select="$newline"/>
<xsl:variable name ="BatchIds" select="//i:issues/i:issue"/>
<!-- begin for each loop -->
<xsl:for-each select="$BatchIds">
<xsl:variable name="currentPosition" select="position()"/>
<xsl:variable name="nextPosition" select="position()+1"/>
<xsl:variable name="lastPosition" select="position()-1" />
<xsl:variable name="lastBatchId" select="//i:issues/i:issue[$lastPosition]/i:issueBatchNumber"/>
<xsl:variable name="thisBatchId" select="i:issueBatchNumber" />
<xsl:choose>
<xsl:when test="not($lastBatchId = $thisBatchId)" >
<xsl:variable name="startingBatchId" select="//i:issues/i:issue[1]/i:issueBatchNumber"/>
Batch ID: <xsl:value-of select="i:issueBatchNumber"/><xsl:value-of select="$newline"/>
Total Time in Seconds: <sum value here -- this is the bit I dont know how to do>
</xsl:when>
<xsl:otherwise >
</xsl:otherwise>
</xsl:choose>
outputting csv data here
</xsl:for-each>
This is a sample of the xml Data I am working with
<issues>
<issue>
<weight>0.903999984264374</weight>
<materialBatch>
<Code>WB821</materialCode>
<weight>0</weight>
<cost>0</cost>
</materialBatch>
<issueBatchNumber>D15601001</issueBatchNumber>
<jobNumber>Default Job 2015-4</jobNumber>
<date>2015-04-30T02:36:47</date>
<dateStarted>2015-04-30T02:33:38</dateStarted>
<finishedAt>2015-04-30T02:36:03</finishedAt>
<dispenseDurationInSeconds>144.78</dispenseDurationInSeconds>
</issue>
<issue>
<weight>0.903999984264374</weight>
<materialBatch>
<Code>WB821</materialCode>
<weight>0</weight>
<cost>0</cost>
</materialBatch>
<issueBatchNumber>D15601001</issueBatchNumber>
<jobNumber>Default Job 2015-4</jobNumber>
<date>2015-04-30T02:36:47</date>
<dateStarted>2015-04-30T02:36:03</dateStarted>
<finishedAt>2015-04-30T02:49.33</finishedAt>
<dispenseDurationInSeconds>13.3</dispenseDurationInSeconds>
</issue>
<issue>
<weight>0.903999984264374</weight>
<materialBatch>
<Code>WB821</materialCode>
<weight>0</weight>
<cost>0</cost>
</materialBatch>
<issueBatchNumber>D15601001</issueBatchNumber>
<jobNumber>Default Job 2015-4</jobNumber>
<date>2015-04-30T02:36:47</date>
<dateStarted>2015-04-30T02:49.33</dateStarted>
<finishedAt>2015-04-30T02:54.22</finishedAt>
<dispenseDurationInSeconds>5.99</dispenseDurationInSeconds>
</issue>
</issues>
You can try this way :
<xsl:value-of
select="sum(../i:issue[i:issueBatchNumber=$thisBatchId]/i:dispenseDurationInSeconds)">
above xpath return sum of all dispenseDurationInSeconds from parent issue elements having child issueBatchNumber equals current $thisBatchId value.
I have a comma separated list coming from C#, which I am parsing in XSLT and loading it as drop down. After the user selects the option from the drop down and submits the page, If other fields are not filled in the page, I try to reload the page with the selected option for this drop down.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template name="parseString">
<xsl:param name="list"/>
<xsl:if test="contains($list, ',')">
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:value-of select="substring-before($list, ',')"/>
<xsl:if test="substring-before($list, ',')=$carrier">
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
</xsl:if>
</xsl:element>
<xsl:call-template name="parseString">
<xsl:with-param name="list" select="substring-after($list, ',')"/>
</xsl:call-template>
</xsl:if>
But upon reload, the selected value in the drop down is not maintained.
But I can see the text - 'sel value' meeting the condition and displayed. For example in the Image you can see the text for carrier - Metro PCS.
Any Help would be appreciated.
Thanks.
EDIT: I have tried multiple ways for selected attribute like
<xsl:attribute name="SELECTED"></xsl:attribute>
<xsl:attribute name="SELECTED">True</xsl:attribute>
<xsl:attribute name="SELECTED">selected</xsl:attribute>
None of them seem to work.
Try swapping these two lines:
: sel value
<xsl:attribute name="SELECTED"></xsl:attribute>
to be
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
I think you are trying to add an attribute to the ": sel value" text node which obviously won't work.
EDIT
Taking a closer look at your template I think it is an issue like suggested above (adding an attribute to a text node).
Try this:
<xsl:element name="option">
<xsl:attribute name="value">
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:attribute>
<xsl:if test="substring-before($list, ',')=$carrier">
<xsl:attribute name="SELECTED"></xsl:attribute>
: sel value
</xsl:if>
<xsl:value-of select="substring-before($list, ',')"/>
</xsl:element>
When your if is true it is trying to add an attribute to the text node added by the value-of. All of your attribute additions need to come before adding any child nodes, be they text or otherwise.