I have a long web url in my xsl variable.
eg.
#url = "http://stackoverflow.com/questions/ask/2434/35454"
I need a substring from this based on 3rd index of "/". i.e I want to display only
http://stackoverflow.com
There is a substring(string, start, length) function in xsl but how do i find the length portion. I could not find any indexof function.
<xsl:value-of select="substring(url,1,length)"/>
My url is suppose - "http://stackoverflow.com/questions/ask/2434/35454"
Output I want is http://stackoverflow.com
Please suggest some solutions.
Use:
concat(substring-before(.,'//'),
'//',
substring-before(substring-after(., '//'),
'/'
)
)
Complete code example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="/*">
<xsl:value-of select=
"concat(substring-before(.,'//'),
'//',
substring-before(substring-after(., '//'),
'/'
)
)
"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on this XML document:
<t>http://stackoverflow.com/questions/ask/2434/35454</t>
the wanted, correct result is produced:
http://stackoverflow.com
Related
I have an XML which has Decimal field which need to formatted to European Currency format.
The Current its having US format.
-<Envelope>
-<Body>
-<getPriceRecommendationResponse>
-<status>
<statusCode>Success</statusCode>
</status>
-<priceRecommendation>
<tssArticleNumber>Item Number1234</tssArticleNumber>
<compoundCode>N123</compoundCode>
<compoundGroupCodeBucket>A</compoundGroupCodeBucket>
<compoundCodeBucket>N123 & others</compoundCodeBucket>
<qualityIndexCode>-</qualityIndexCode>
<qualityIndexBucket>Std Quality</qualityIndexBucket>
<weight>66.0341</weight>
<weightGroupBucket>BT 123.1234 and 12345.1234</weightGroupBucket>
<weightIsValidBucket>YES</weightIsValidBucket>
<subGroupCode>PT</subGroupCode>
<subGroupCodeBucket>7:B03</subGroupCodeBucket>
<stockDistinction>MTS</stockDistinction>
<productIdBucket>PT0401450-T46N</productIdBucket>
<referencePrice>42.076</referencePrice>
<averageQuantity>9</averageQuantity>
<quantityAdjustments>0.96</quantityAdjustments>
<highDV>2.123789</highDV>
<averageDV>1.25141</averageDV>
<lowDV>0.79983</lowDV>
<additionalAdjustmentsTotal>1</additionalAdjustmentsTotal>
<highPrice>19876.9124796544</highPrice>
<averagePrice>12345.5481540736</averagePrice>
<lowPrice>123344567.3075011968</lowPrice>
</priceRecommendation>
</getPriceRecommendationResponse>
</Body>
</Envelope>
Please help me with an xslt which can format all the Decimal Nodes in the XML.
The below is an xslt i am already using. I am expecting a similar one. Thanks
//this has been taken from a Microsoft knowledgebase aricle and strips out the
//namespaces from an XML message using a style sheet
XslTransform := XslTransform.XslTransform;
XMLStyleSheet := XMLStyleSheet.XmlDocument;
XMLStyleSheet.InnerXml(
'<?xml version="1.0" encoding="UTF-8"?>'+
'<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:msxml="urn:schemas-microsoft-com:xslt">'+
'<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="UTF-8" indent="yes"/>' +
'<xsl:template match="#*|node()">'+
'<xsl:copy>'+
'<xsl:apply-templates select="#*|node()"/>'+
'</xsl:copy>'+
'</xsl:template>'+
'<xsl:template match="'+OldNode+'">'+
'<xsl:variable name="oldNode" select="'+OldNode+'"/>' +
'<xsl:variable name="newNodeXml">' +
'<xsl:element name="'+NewNode+'">' +
'<xsl:copy-of select="$oldNode/#*|node()"/>' +
'<xsl:copy-of select="$oldNode/child::*"/>' +
'<xsl:copy-of select="$oldNode/#*"/>' +
'</xsl:element>' +
'</xsl:variable>' +
'<xsl:copy-of select="msxml:node-set($newNodeXml)"/>' +
'</xsl:template>' +
'</xsl:stylesheet>'
);
XslTransform.Load(XMLStyleSheet);
writer := writer.StringWriter();
XslTransform.Transform(Source, nullXsltArgumentList, writer);
Destination := Destination.XmlDocument;
Destination.InnerXml(writer.ToString());
Thank you guys for your response. I hope i can make the things more clear to you. The European format is "," for Decimal separator and "." for Digit Grouping. Whereas US have the reverse. For example 1,000.156 in US and 1.000,156 for Europe. Yes Rnet i have tried the decimal-format its working fine, but the problem here is i need to use it multiple times for the Fields. I want an XSLT which change the format for all the decimal fields at once. I hope you got my point
Thank you Michael, I have tried your code but getting an error.
A call to System.Xml.Xsl.XslTransform.Load failed with this message: Expression must evaluate to a node-set.
I have changed the code little bit given by Michael, but still getting the same error.
'<?xml-stylesheet type="text/xsl" href="decimalformat.xsl"?>'+
'<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:decimal-format name="eu" decimal-separator="," grouping-separator="." />'+
'<!-- identity transform -->'+
'<xsl:template match="#*|node()">'+
' <xsl:copy>'+
' <xsl:apply-templates select="#*|node()"/>'+
' </xsl:copy>'+
'</xsl:template>'+
'<xsl:template match="*[number()=number()]">'+
' <xsl:copy>'+
' <xsl:value-of select=translate("format-number(., '#.##0,##########', 'eu'), ',', '.')" />'+
' </xsl:copy>'+
'</xsl:template>'+
'</xsl:stylesheet>'
In my application its not working. I have found a similar post here http://mikeschinkel.com/blog/gettingpastthexslterrorexpressionmustevaluatetoanodeset/#comment-484235
<xsl:value-of select="format-number(., '#.##0,##########', 'eu')" />
I am getting the error if i include the above code. i have tried it this way as well.
<xsl:value-of select=translate("format-number(., '#.##0,##########', 'eu'), ',', '.')" />
the problem here is i need to use it multiple times for the Fields. I
want an XSLT which change the format for all the decimal fields at
once.
This could be accomplished by a template that matches only elements containing strictly numeric values, for example:
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:decimal-format name="eu" decimal-separator=',' grouping-separator='.' />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[number()=number()]">
<xsl:copy>
<xsl:value-of select="format-number(., '#.##0,##########', 'eu')" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note:
This will not work with your input, because it contains an unescaped ampersand character;
This will not process elements that contain numbers as part of a string, e.g:
<weightGroupBucket>BT 123.1234 and 12345.1234</weightGroupBucket>
This has nothing to do with currency.
I have a xslt showing no whitespace as characters.
In this case show only %.
URL:
http://localhost:8888/tire/details/Bridgestone/ECOPIA%EP001S/Bridgestone,ECOPIA%EP001S,195--65%R15%91H,TL,ECO,0
XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:x="http://www.w3.org/1999/xhtml" version="1.0">
<xsl:param name="extractorHost" />
<xsl:template match="/">
<links>
<xsl:apply-templates />
</links>
</xsl:template>
<xsl:template match="//x:form/x:a[#class='arrow-link forward']">
<xsl:variable name="url" select="translate(#href, ' ', '%20')"/>
<link href="{concat($extractorHost, $url)}" />
</xsl:template>
<xsl:template match="text()" />
</xsl:stylesheet>
The correct URL should be:
http://localhost:8888/tire/details/Bridgestone/ECOPIA%20EP001S/Bridgestone,ECOPIA%20EP001S,195--65%20R15%2091H,TL,ECO,0
Is it wrong XSLT formed?. Thanks.
The XPath translate function doesn't work the way you think it does. That is, it is not a replace-string function.
It maps individual characters from one list to the corresponding characters in the other list.
So this,
translate(#href, ' ', '%20')
means, translate a space into %. The 20 part of the third argument is ignored.
Take a look here: XSLT string replace
You can use already existing templates that will let you use "replace" function.
I have a XSL file in which I am creating a field like this:
<ServiceText>
<xsl:value-of select="concat(Yrs,'-',Mos,'-',Days,'-',Hrs)" />
</ServiceText>
The values of 'Yrs,'-',Mos,'-',Days,'-',Hrs , I am receiving from a Web service response and assiging it to the XSL directly. I cannot do any modification to the data in code for these fields, because that is how the ocde is. All data manipulation is on the xslt.
I want to do a data filtering on xslt as follows:
if value of yrs =-1 then yrs=""
if value of mos =-1 then mos=""
if value of Days =-1 then Days=""
if value of Hrs =-1 then Hrs=""
How can I do it on the XSL file?
XSLT 2.0:
<xsl:template match="/root">
<ServiceText>
<xsl:value-of select="string-join((Yrs, Mos, Days, Hrs)[.!=-1],'-')" />
</ServiceText>
</xsl:template>
See link for a Working example
In XSLT 1.0 use something like this:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*/*[not(.= -1)]">
<xsl:value-of select="concat(., substring('-', 2 - not(self::Hrs)))"/>
</xsl:template>
<xsl:template match="*/*[. = -1]"/>
</xsl:stylesheet>
When this transformation is applied on the following XML document:
<t>
<Yrs>-1</Yrs>
<Mos>7</Mos>
<Days>15</Days>
<Hrs>3</Hrs>
</t>
the wanted result is produced:
7-15-3
Do Note:
It seems that there is an assumption that the "-1" values form a contiguous group (right to left in the sequence Yrs, Mos,Days).
If this assumption is violated, it would be impossible to understand what is the missing part in 2013-10-8 -- is it the months or the days ?
I am using XSLT to create XML file. A Date-time has milliseconds. I need to have the output XML without milliseconds.
Format needs to be YYYY-MM-DDTHH:MM:SS
For example:
XML shows date as: 2012-12-341T09:26:53.132-0500
But this needs to be: 2012-12-341T09:26:53
If all of the values are dateTime and have a ., you could use substring-before():
substring-before('2012-12-341T09:26:53.132-0500', '.')
Of you could use substring() to select the first 20 characters:
substring('2012-12-341T09:26:53.132-0500', 0, 21)
If you are using XSLT2, see this function: http://www.w3.org/TR/xslt20/#function-format-dateTime. This picture string should give you what you want:
format-dateTime($dateTime,'[Y0001]-[M01]-[D01]T[H01]:[m01]:[s01]')
This XPath expression produces the wanted result regardless whether the string contains a dot or a hyphen or both dot and hyphen, or none, and doesn't rely on the number of digits used for year, month, day:
substring-before(concat(substring-before(concat(substring-after(.,'T'),
'.'),
'.'),
'-'),
'-')
Here is a simple XSLT transformation that uses this XPath expression:
<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="dt/text()">
<xsl:value-of select="substring-before(., 'T')"/>
<xsl:text>T</xsl:text>
<xsl:value-of select=
"substring-before(concat(substring-before(concat(substring-after(.,'T'),
'.'),
'.'),
'-'),
'-')
"/>
</xsl:template>
</xsl:stylesheet>
When this transformation is applied on this test XML document:
<t>
<dt>2012-12-341T09:26:53.132-0500</dt>
<dt>2012-12-355T09:34:56</dt>
<dt>2012-12-355T09:34:56-0500</dt>
<dt>2012-12-13T9:34:5-0500</dt>
<dt>2012-12-344T09:12:34.378-0500</dt>
</t>
the wanted, correct result is produced:
<t>
<dt>2012-12-341T09:26:53</dt>
<dt>2012-12-355T09:34:56</dt>
<dt>2012-12-355T09:34:56</dt>
<dt>2012-12-13T9:34:5</dt>
<dt>2012-12-344T09:12:34</dt>
</t>
Explanation:
Proper application of sentinels.
I have a block as below.
<rightOperand>.*ifIndedx.*</rightOperand>
But i need to change the above snippet to the below one
<rightOperand>(?i)(?s).*ifIndex.*</rightOperand>
This translation needs to be done only when the right operand starts and ends with the string .*
please provide me some pointers .
You can do this my overriding the identity transform with an extra template just to match the text within rightOperand that matches your criteria
<xsl:template match="rightOperand/text()
[starts-with(., '.*')]
[substring(., string-length(.) - 1, 2) = '.*']">
Note that XSLT 1.0 does not have the ends-with function, which is why there is the extra work to check the ending with substring. If you were using XSLT 2.0 you could simplify this with ends_with though.
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="rightOperand/text()
[starts-with(., '.*')]
[substring(., string-length(.) - 1, 2) = '.*']">
<xsl:text>(?i)(?s)</xsl:text><xsl:copy />
</xsl:template>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your sample XML, the following is output:
<rightOperand>(?i)(?s).*ifIndedx.*</rightOperand>