find adjacent alphanumeric number - xslt-1.0

My input xml
<namelist>
<order>
<name>JAMES</name>
<accNo>10D</accNo>
</order>
<order>
<name>ARAFAT</name>
<accNo>07A</accNo>
</order>
<order>
<name>anotherbranchaccount</name>
<accNo>20J</accNo>
</order>
<order>
<name>JAMES</name>
<accNo>20K</accNo>
</order>
<order>
<name>JOHN</name>
<accNo>9A</accNo>
</order>
<order>
<name>anotherbranchaccount</name>
<accNo>10E</accNo>
</order>
</namelist>
Expected output
JAMES 10D ANOTHERACCOUNT 10E
ARAFAT 07A
JAMES 20K ANOTHERACCOUNT 20J
My problem
I got to match with 'anotherbranchaccount'element which 'accNo' is adjacent to JAMES 'accNo' .There is no sequence like main account number then extra account node.
Give me solution. I dont have any idea about finding from alphanumeric format.because we have many alphanumeric comibanations like [A-Z][1 TO 100]
How to solve this issue? Please help me out.

This Regular Expression will return true for your account number format:
"(^[0-9]{1,2})+([a-zA-Z]{1})$"

Related

Extract value from XML using XSLT 1.0

I am trying to extract the value from the below XML using XSLT 1.0. Can you please advise the xslt code?
<?xml version="1.0" encoding="iso-8859-1"?>
<title>
<locale>
<properties>
<OriginalFileName>UMG_00720616207322_T3_locale.uti</OriginalFileName>
</properties>
</locale>
</title>
Desired output
720616207322
The expression:
substring-before(substring-after($string, '_'), '_')
will return "00720616207322" when $string contains "UMG_00720616207322_T3_locale.uti".

Concat distinct values in a for-each

I've run into a problem and i'm looking for a quick fix. I have an xml from which i take a some values:
<root>
<item>
<property1>value</property1>
<property2>value</property2>
<property3>value</property3>
</item>
<item>...</item>
<item>...</item>
<item>...</item>
</root>
I'm making a variable to use after using:
<xsl:for-each select="root/item"><xsl:value-of select="concat(property1,';')"/></xsl:for-each>
But i've ran into a problem when too many items, the variable gets too big (over 255 characters). So i was thinking of taking only the unique values (unique property values).
Any simple way to do it ?
Thanks
Please test the stylesheet below:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:key name="group" match="property1" use="."/>
<xsl:variable name="unique_properties">
<xsl:for-each select="//property1[count(. | key('group', .)[1]) = 1]"><!-- this selects unique values -->
<xsl:value-of select="concat(.,';')"/>
</xsl:for-each>
</xsl:variable>
<xsl:template match="root">
<xsl:value-of select="$unique_properties"/>
</xsl:template>
</xsl:stylesheet>

XSLT: How can I identify the list of values are same?

I got the following xml snippet.
<Root>
<SomeTags></SomeTags>
<Values>
<dateFrom>date 1<dateFrom>
<dateEnd>date 2<dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
More Values here....
.....................
.....................
<Values>
<dateFrom>date n<dateFrom>
<dateEnd>date n+1<dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
</Root>
I want to compare all <value1> values and <value2> values. If they are same, I want to compress the whole as following:
If values1 == same in all Values and If values2== same in all Values then this should the output.
<Values>
<dateFrom>date 1<dateFrom> should be from the first Values item
<dateEnd>date n+1<dateEnd> should be from the last Values item
<value1>10</value1>
<value2>5</value2>
</Values>
Else
different template.
How can i achieve this in XSLT (1.0) in BizTalk 2009 ?
I know iteration. for-each. But is there a way to break as in procedural language. Can I somehow compare each value and return a boolean saying all values are same or not.. ??
Thank you all for your responses.
This transformation:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Values">
<xsl:if test=
"not(preceding-sibling::Values
[value1=current()/value1 and value2=current()/value2])">
<Values>
<xsl:apply-templates select="value1|value2"/>
</Values>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
when applied on the following well-formed XML document (produced from the severely malformed text that is provided in the question):
<t>
<Values>
<dateFrom>date 1</dateFrom>
<dateEnd>date 2</dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
<somethingElse/>
<Values>
<dateFrom>date n</dateFrom>
<dateEnd>date n+1</dateEnd>
<value1>10</value1>
<value2>5</value2>
</Values>
</t>
produces the wanted result:
<t>
<Values>
<value1>10</value1>
<value2>5</value2>
</Values>
<somethingElse/>
</t>
Explanation:
Just applying the most fundamental design pattern of XSLT: Using and overriding the identity rule.

How to use parameter or variable values as node name?

I am trying to use the value of a parameter or variable as a node name inside a value-of select but so far failed..
So my XML is as below.
<Data>
<Name>John Smith</Name>
<Date>28112012</Date>
<Phone>iphone</Phone>
<Car>BMW</Car>
</Data>
And my incomplete xslt looks like below.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"
exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="{$nodename}"/>
</Output>
</xsl:template>
</xsl:stylesheet>
Ideally I want the out put to be
<Output>John Smith</Output>
Is there any way I can do this using XSLT?
I want to be able to select appropriate node based on a users choice.
Thanks
SK
A wild guess, let me know if it works:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" exclude-result-prefixes="#all">
<xsl:param name="nodename" select="'Name'"/>
<xsl:template match="/Data">
<Output>
<xsl:value-of select="//*[name()=$nodename]" />
</Output>
</xsl:template>
</xsl:stylesheet>

XSLT 1.0 amount fields have to have at least 2 decimals

I have two xmls.There is a amount field which can contains values like 54.2,54.23,54.234,54.234567.
Would someone please tell me how can I make sure that atleast two decimal places will appear in the output xml.Currently 54.2 gets converted to 54,2 , but I want it to be 54,20
You can use the format-number() function to convert a number into a string in a given format.
At least two decimal places will be appeared if you use this "#.00##########" format string.
If you have an xml file which can contains values like 54.2,54.23,54.234,54.234567:
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<price>54.2</price>
<price>54.23</price>
<price>54.234</price>
<price>54.234567</price>
</catalog>
You can convert the numbers with an xslt like this to get at least two decimal places
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="/catalog/price">
<xsl:value-of select='format-number(., "#.00##########")'/><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Then the output:
54.20
54.23
54.234
54.234567