<Description>this is my value 822880494 this is my value</Description>
I'm quite new to xpath, xml and stylevision so this might be a basic problem.
I am using stylevision 2010 and xpath to create an sps/xslt for an schema.
In the above node you can see there is a numeric value inside the node and I want to extract that value and turn it into a link in my pdf/html. The problem is that i cant seem to extract it. Substring is not an option since the length of the value and the position of the numeric value inside it varies.
Some will probably think that the schema is badly composed and that the numeric value should be in a seperate node/attribute/... There is nothing I can do about that since this schema is provided by another company.
Thanks in advance!
hi this will produce the results you requre! it checks each character, and then makes sure that it is a number.
XSLT 1 Solution
A fragile but possible solution in plain XSLT 1.0 would be to use a composition of
translate
(to make all non-numeric values to empty strings or spaces) andnormalize-space
(to trim the rest of spaces, thoughtranslate
might suffice). This will certainly work only if there are no other numeric values within the string. And, I can't currently check,translate
might work only if your string contains ascii characters.XSLT 2.0 has several regexp functions. If you xslt processor allows using EXSLT extentions, it as well contains regexp functions, or you can tokenize your string by spaces and provide non-empty template to the numeric token only.
p.s. I am sorry, that I do not provide any links, it's hard to to from the device.
The following is a derivation of the above XSLT v1 solution, however, this is specifically for a leading number, as versus embedded in the middle of the string. It also allows for floating point or integer parsing. (I personally find this useful for breaking apart units from values, such as "80 mg" or "128.4 mm2", where the unit is "mm2", and the value "128.4" , and NOT "128.42".
The following are some unit-test cases with comparative results:
StyleVision 2010 seems to support XSLT 2.0, so you could use a 2.0 stylesheet and do something like
Or whatever you want to do with the number; the string with the number is the context element inside the
<xsl:matching-substring>
element.Newtover's
translate
idea (for XSLT 1.0) would look like this:But if your input contains multiple numbers, that will simply concatenate them.
Use this simple XPath 1.0 expression:
Here is a complete XSLT 1.0 solution:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
This is known as the Double Translate Method first proposed by Michael Kay. It consists of two nested calls to the
translate()
function:Inner
translate()
. This produces all characters of the string, except digits.Outer
translate()
. This deletes from the string all characters produced by the innertranslate()
. What remains is just the wanted characters (the digits).