How to determine the value format is dd-mmm-yyyy in xslt - regex

I have to determine the input value having date format of dd-mmm-yyyy. If I can find will set some attribute based on the attribute I can do the format in C# report processing class.
<td>
<xsl:if test="To write expression to match the value">
<r>
<xyz:value-of select="'Set Value'" />
</r>
</xsl:if>
</td>
Input value is "30-Jun-2019". If it matches I want to set .
Basically I have set of columns in the report. I have to identify the the values in the report if the value matches with the Date format of dd-mmm-yyy setting some attribute in the xslt and applying the same format in report parser code which is written in c#

As I said in comments, there is no regex support in XSLT 1.0, so this can get quite tedious.
Consider the following example:
XML
<input>
<item>21-Jan-1987</item>
<item>921-Jan-1987</item>
<item>15-Jul-2009</item>
<item>15-Jux-2009</item>
<item>03-Dec-2014</item>
<item>03-Dec-999</item>
</input>
XSLT 1.0
<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:strip-space elements="*"/>
<xsl:template match="/input">
<output>
<xsl:for-each select="item">
<item value="{.}">
<xsl:variable name="dd" select="substring-before(., '-')" />
<xsl:variable name="mmm" select="substring-before(substring-after(., '-'), '-')" />
<xsl:variable name="yyyy" select="substring-after(substring-after(., '-'), '-')" />
<xsl:if test="translate($dd, '123456789', '000000000') = '00' and translate($yyyy, '123456789', '000000000') = '0000' and ($mmm='Jan' or $mmm='Feb' or $mmm='Mar' or $mmm='Apr' or $mmm='May' or $mmm='Jun' or $mmm='Jul' or $mmm='Aug' or $mmm='Sep' or $mmm='Oct' or $mmm='Nov' or $mmm='Dec')">
<xsl:text>Is Date</xsl:text>
</xsl:if>
</item>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<item value="21-Jan-1987">Is Date</item>
<item value="921-Jan-1987"/>
<item value="15-Jul-2009">Is Date</item>
<item value="15-Jux-2009"/>
<item value="03-Dec-2014">Is Date</item>
<item value="03-Dec-999"/>
</output>
Note that this checks only that the input conforms to the pattern, not that the date itself is valid. Also keep in mind that XML is case-sensititve.
Added:
If you prefer, you could simplify the test to:
<xsl:if test="translate(translate(translate(., '123456789', '000000000'), 'JFMASOND', '########'), 'anebpryulgctov', '%%%%%%%%%%%%%%') = '00-#%%-0000'">
but then a value like 15-Jpt-2009 will pass as date.

In XSLT 2.0 this is fairly trivial: matches(., '[0-9]{2}-[A-Z][a-z]{2}-[0-9]
{4}')
In 1.0 it's considerably harder, and it depends a little bit how precise you want to be. But you could get close with translate(translate($input, 'ABC...abc...', 'AAAAAAAA....'), '0123456789', '9999999999') = '99-AAA-9999') where the '...' means you have to write out the rest of the alphabet.

Related

How to count created node XSL?

I was asking myself a question.
I'm on project where I've to transform a .XML file into an other one (after a treatment) but I've to do a numbered list. I know the count(//node) function but I don't think we can count the created nodes with.
For example this is what my .xsl looks like :
<xsl:template match="/">
<Type>
<List>
<xsl:apply-templates mode="PartOne" select="/Stuff/Info/TypeA"/>
<xsl:apply-templates mode="PartOneBis" select="/Stuff/Info/TypeB"/>
</List>
<AnotherList>
<xsl:apply-templates mode="PartTwo" select="/Stuff/Info/TypeB"/>
</AnotherList>
</Type>
</xsl:template>
<xsl:template mode="PartOne" match="/Stuff/Info/TypeA">
<PartOne indexlist="{position()-1}">
... treatment ...
</PartOne>
</xsl:template>
<xsl:template mode="PartOneBis" match="/Stuff/Info/TypeB">
<xsl:if test="TypeB_Indice != 'stuff'">
<PartOne indexlist="{count(//TypeA) + position()-1}">
... treatment ...
</PartOne>
</xsl:if>
</xsl:template>
<xsl:template mode="PartTwo" match="/Stuff/Info/TypeB">
<xsl:if test="TypeB_Indice = 'stuff'">
<PartTwo indexlist="{count(//TypeA) + position()-1}">
... treatment ...
</PartTwo>
</xsl:if>
</xsl:template>
And this is what my .xml looks like:
<Stuff>
<Info>
<TypeA>
<TypeA_Stuff/>
<TypeA_Indice>xxx</TypeA_Indice>
</TypeA>
<TypeB>
<TypeB_Stuff/>
<TypeB_Indice>xxx</TypeB_Indice>
</TypeB>
</Info>
</Stuff>
---------------------- edit ----------------------------
The conditions for the PartOneBis is more complacated than the one I put in this code, there's more like 6 differents factor that can change its state from ok to not ok.
I was thinking of a for-each with an if and an incrementation but this don't work because you can't overwrite a variable or may be I'm wrong in my method.
If there's a way to count the node create before your point without having to create two .xml or use a c++ function I'll like to know it.
Thanks.
I need to put the "partOne" type first and the "partTwo" second but in the xml I've got, there's some conditions that makes that the TypeB is a partOne otherwise the other cases'll be a partTwo.
TypeA -> partOne
TypeB -> if (something) partOne else partTwo
But the condition depend of several value which don't came from the same node but the wanted result is something like that
<PartOne indexlist="0">
SomeStuff
</PartOne>
<PartOne indexlist="1">
SomeStuff
</PartOne>
<PartTwo indexlist="2">
SomeStuff
</PartTwo>
I find it impossible to follow your example. Consider a simpler one:
XML
<input>
<item>red</item>
<item>red red</item>
<item>red blue</item>
<item>red green</item>
<item>blue</item>
<item>blue blue</item>
<item>blue green</item>
<item>blue red</item>
<item>green</item>
</input>
XSLT 1.0
<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:strip-space elements="*"/>
<xsl:template match="/input">
<xsl:variable name="red-items" select="item[contains(., 'red')]" />
<xsl:variable name="blue-items" select="item[contains(., 'blue')]" />
<output>
<red-list>
<xsl:apply-templates select="$red-items"/>
</red-list>
<blue-list>
<xsl:apply-templates select="$blue-items">
<xsl:with-param name="n" select="count($red-items)"/>
</xsl:apply-templates>
</blue-list>
</output>
</xsl:template>
<xsl:template match="item">
<xsl:param name="n" select="0"/>
<item i="{$n + position()}">
<xsl:value-of select="." />
</item>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<output>
<red-list>
<item i="1">red</item>
<item i="2">red red</item>
<item i="3">red blue</item>
<item i="4">red green</item>
<item i="5">blue red</item>
</red-list>
<blue-list>
<item i="6">red blue</item>
<item i="7">blue</item>
<item i="8">blue blue</item>
<item i="9">blue green</item>
<item i="10">blue red</item>
</blue-list>
</output>

XSLT Appending incremented value to existing attribute value

For my input XML, I have written the XSLT , But I cannot make the XSLT to generate the <mynewtag> correctly. Please help.
XML input:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book.child.1>
<title>charithram</title>
<author>sarika</author>
</book.child.1>
<book.child.2>
<title>doublebell</title>
<author>psudarsanan</author>
</book.child.2>
</books>
Expected Output:
<?xml version="1.0" encoding="UTF-8"?>
<newbooks>
<newbook>
<mynewtag id="book1" />
<title>charithram</title>
<author>sarika</author>
</newbook>
<newbook>
<mynewtag id="book2" />
<title>doublebell</title>
<author>psudarsanan</author>
</newbook>
</newbooks>
XSLT that I tried: [I understand the syntax is incorrect for <mynewtag> ]. But I don't know to fix it to get the desired output.
<?xml version="1.0" encoding="ISO-8859-1"?>
<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="/">
<newbooks>
<xsl:for-each select="books/child::*">
<newbook>
<mynewtag id="book<xsl:number value='position()' format='1' />" />
<title>
<xsl:value-of select="title" />
</title>
<author>
<xsl:value-of select="author" />
</author>
</newbook>
</xsl:for-each>
</newbooks>
</xsl:template>
</xsl:stylesheet>
Trying in Online XSLT transformer , http://www.freeformatter.com/xsl-transformer.html
I tried assigning the position to a variable, but still I face the same problem of not knowing how to append it with the attribute value book .
<xsl:variable name="cnt">
<xsl:number value='position()' format='1' />
</xsl:variable>
<xsl:value-of select = "$cnt" />
Note: If I remove the <xsl:number value='position()' format='1' /> from XSL, then the syntax is correct, but then I won't be able to generate book1 book2 etc as <mynewtag> attribute values.
Please help.
Added: <mynewtag> is a required element. It is like any other XML element like <title> that is required in output. It is not an element just to hold the attribute id. Sorry if there is a confusion on this.
Adding Solution here, from the answers obtained to summarize:
<mynewtag>
<xsl:attribute name="id">
<xsl:text>book</xsl:text>
<xsl:number value='position()'/>
</xsl:attribute>
</mynewtag>
or shortly:
<mynewtag id="book{position()}" />"
or
<newbook>
<xsl:variable name="cnt">
<xsl:number value='position()' format='1' />
</xsl:variable>
<mynewtag id="book{$cnt}" />
..........
Also the attribute value templates that IanRoberts mentioned.
You can't place a tag inside another tag. Try either:
<mynewtag>
<xsl:attribute name="id">
<xsl:text>book</xsl:text>
<xsl:number value='position()'/>
</xsl:attribute>
</mynewtag>
or shortly:
<mynewtag id="book{position()}" />"
ADDED:
Not directly related to your question, but I believe the id attribute should be applied to the parent <newbook> element, instead of creating an artificial child element to hold it.

msxsl:node-set() not recognized

I am trying to pull nodes out of a node set stored in a variable using the msxsl:node-set() function and am not getting anything. My xml looks like this:
<Root>
<Items olditemnumber="100" newitemnumber="200">
<Item ItemNumber="100" ItemAliasCode="1001" ItemCode="X" />
<Item ItemNumber="100" ItemAliasCode="1002" ItemCode="X" />
<Item ItemNumber="200" ItemAliasCode="2001" ItemCode="X" />
<Item ItemNumber="200" ItemAliasCode="2003" ItemCode="X" />
<Item ItemNumber="100" ItemAliasCode="1003" ItemCode="P" />
<Item ItemNumber="100" ItemAliasCode="1004" ItemCode="P" />
<Item ItemNumber="200" ItemAliasCode="2002" ItemCode="P" />
</Items>
</Root>
In my xslt I try to populate a variable with a subset of the nodes and then call them using the msxsl:node-set() function. This doesn't return anything however.
XSLT looks like this:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="//Root">
<xsl:variable name="OldItemNumber" select="/Items/#olditemnumber"/>
<xsl:variable name="NewItemNumber" select="/Items/#newitemnumber"/>
<xsl:variable name="OldItems">
<xsl:value-of select="//Item[#ItemNumber = $OldItemNumber]"/>
</xsl:variable>
<xsl:variable name="NewItems">
<xsl:value-of select="//Item[#ItemNumber = $NewItemNumber]"/>
</xsl:variable>
<xsl:for-each select="msxsl:node-set($OldItems)/Item">
...work
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The XSLT skips over the for-each loop, though I see in the watch that the the Xpath query grabs the right nodes in assigning the variables. The watch also tells me that the msxsl:node-set() function is undefined. Any help would be appreciated. What am I missing?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:template match="//Root">
<xsl:variable name="OldItemNumber" select="/Items/#olditemnumber"/>
<xsl:variable name="NewItemNumber" select="/Items/#newitemnumber"/>
<xsl:variable name="OldItems" select="//Item[#ItemNumber = $OldItemNumber]"/>
<xsl:variable name="NewItems" select="//Item[#ItemNumber = $NewItemNumber]"/>
<xsl:for-each select="$OldItems">
...work
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
msxsl:node-set is for converting a result tree fragment (a.k.a. RTF) to a node set, which is not needed on your case.
xsl:value-of is for creating text nodes, so don't use it for selecting nodes of the input tree that you want to further query/process.

Format date in xslt

I have following xml
<Report>
<Items>
<Item>
<Id>1</Id>
<TotalSent>251</TotalSent>
<Opened>48</Opened>
<LastSend>01/07/2013 16:38:18</LastSend>
<Bounced>1</Bounced>
<Unopened>202</Unopened>
</Item>
</Items>
</Report>
i want to transform it to another xml using xslt , my desired o/p is like below
<chart subcaption ="Last sent on Monday 01 July 2013 at 16:38">
<set label="Opened" value="48"/>
<set label="Bounced" value="1"/>
</chart>
I am not able to get date as i want for subcaption attribute.
I tried below xslt code but it is not working
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ms="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/">
<chart>
<xsl:variable name='lastSend' select='Report/Items/Item/LastSend' />
<xsl:attribute name="subcaption">
<xsl:value-of select="ms:format-date($lastSend, ' Last sent on MMM dd, yyyy at')"/>
<xsl:value-of select="ms:format-time($lastSend, ' hh:mm')"/>
</xsl:attribute>
<xsl:for-each select="Report/Items/Item">
<set>
<xsl:attribute name="label">Opened</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="Opened" />
</xsl:attribute>
</set>
<set>
<xsl:attribute name="label">Bounced</xsl:attribute>
<xsl:attribute name="value">
<xsl:value-of select="Bounced" />
</xsl:attribute>
</set>
</xsl:for-each>
</chart>
</xsl:template>
</xsl:stylesheet>
when i am passing hard coded value in ms:format-date() & ms:format-time() functions, like 01/07/2013 16:38:18 it was working fine , but when i am passing variable value $lastSend it is not working.
Note: I can use any version of xsl.
If you want to use XSLT 2.0 then you need to convert your custom date respectively dateTime format into an xs:dateTime and then you can use the format-dateTime function that XSLT 2.0 provides (see http://www.w3.org/TR/xslt20/#format-date):
<xsl:template match="LastSend">
<!-- 01/07/2013 16:38:18 -->
<xsl:variable name="dt" as="xs:dateTime" select="xs:dateTime(concat(substring(., 7, 4), '-', substring(., 4, 2), '-', substring(., 1, 2), 'T', substring(., 12)))"/>
<xsl:attribute name="subcaption" select="format-dateTime($dt, 'Last sent on [F] [D01] [MNn] [Y0001] at [H01]:[m01]')"/>
</xsl:template>
Take the above second argument "picture string" as an example on how to format a dateTime, you might need to adjust it for your needs, based on the picture string arguments documented in the XSLT 2.0 specification.

How to use group by in xslt

I have a xml that has so many elements and most of that contain attributes.. for some of the attributes values are same so I need to group them and generate diff xml.
I/p Ex:
<TestNode>
<ABC1 value="10.7" format="$" />
<ABC2 value="10.5" format="$" />
<ABC3 value="20" format="Rs" />
<ABC4 value="50" format="Rs" />
<ABC5 value="10.5" format="$" />
</TestNode>
I need to group the rows by format. Note: Format is not fixed... it may grow ...
O/P Ex:
is it possible to get ? Thanks in advance...
In XSLT 1.0 you would use Muenchian grouping.
Define a key "format", from which we can easily select all elements given a format name. Than apply Muenchian grouping to find the unique formats in the input.
Then it gets simple. The "*" template will be applied once per format, and uses the key() to fetch all entries for that format.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name="format" match="TestNode/*" use="#format" />
<xsl:template match="TestNode">
<body>
<xsl:apply-templates select="*[generate-id(.)=generate-id(key('format',#format)[1])]"/>
</body>
</xsl:template>
<xsl:template match="*">
<format format="{#format}">
<xsl:copy-of select="key('format', #format)" />
</format>
</xsl:template>
</xsl:stylesheet>
In XSLT 2.0 you should be able to do it with <xsl:for-each-group>, current-grouping-key() and current-group()
Example:
<xsl:for-each-group
select="TestNode/*"
group-by="#format"
>
<group format="{current-grouping-key()}">
<xsl:for-each select="current-group()">
<xsl:copy-of select="."/>
</xsl:for-each>
</group>
</xsl:for-each-group>
See: http://www.w3.org/TR/xslt20/#grouping