I have input XML like below
<?xml version = "1.0" encoding = "UTF-8"?>
<root>
<param>:22:/ABC/GID:50749612002 BOOK USER REF: 12311111112222 XYZ: DEF BK ID:3333 3333 JKL:MNN - VZXVXHFHF DETA ABC:DEF ORDERID:989796123456789.GKLT C:0006789 FASDFSF.FYRTY 53546475</param>
</root>
Need help to extract ORDERID using XSLT.
Consider extracting the node value to a variable $param:
<xsl:variable name="param">
<xsl:value-of select="/root/param"/>
</xsl:variable>
Now you can use since XSLT 2.0 the function replace to get the number:
<xsl:value-of select="replace($param, '.*?ORDERID:(\d+)\.\w{4} .*', '$1')"/>
The regex .*?ORDERID:(\d+)\.\w{4} .* is demonstrated at Regex101.
Related
I have my string as below
<Text>Pack:NA Lead:20 Dimension:235</Text>
And need to map
NA to outputfield1
20 to outputfield2
235 to outputfield3
How to do this correctly in xslt mapping where the values 'NA,20,235' could be different each time?
I could only see substring component which takes length as second parameter.
That leads requires several steps to achieve this.
Any better solution to just take the value between Lead: and Dimension for outputfield2?
To extract the Pack value, you can use:
<xsl:value-of select="substring-before(substring-after(Text, 'Pack:'), ' ')" />
To extract the Lead value, use:
<xsl:value-of select="substring-before(substring-after(Text, 'Lead:'), ' ')" />
To extract the Dimension:
<xsl:value-of select="substring-after(Text, 'Dimension:')" />
I am new in XSLT and if it is possible to get the position of a specific word? For example, I have a data like this:
<Data>The quick brown fox jumps over the lazy dog!</Data>
I want to get the position of a "brown", "over", "dog" and "!". And, store it in different output name. Like the position of brown is <foo>3</foo>, position of over is <boo>6</boo>, dog <hop>9</hop> and ! <po_df>10</po_df>. Is it possible?
If you were only looking for words you could use tokenize(., '\s+|\p{P}')
<xsl:template match="Data">
<xsl:copy>
<xsl:variable name="words" select="tokenize(., '\s+|\p{P}')"/>
<xsl:for-each select="'brown', 'over', 'dog'">
<matched item="{.}" at-pos="{index-of($words, .)}"/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
which gives
<Data>
<matched item="brown" at-pos="3"/>
<matched item="over" at-pos="6"/>
<matched item="dog" at-pos="9"/>
</Data>
so it has the right positions (I am not sure where the names of the elements you posted (like hop) are to be taken from so I have not tried to implement that.).
As you also want to identify a punctuation character I am not sure tokenize suffices and even with analyze-string it is not straight-forward to match and collect the position. Maybe someone else has a better idea.
<xsl:variable name="groups" as="element(group)*">
<xsl:analyze-string regex="^^([0-9-]+)([A-Z ]*)[ ]*([\(](.*)[\)])*$" select="/fault/informations/restriction1">
<xsl:matching-substring>
<group>
<x><xsl:value-of select="regex-group(1)"/></x>
<y><xsl:value-of select="regex-group(4)"/></y>
</group>
</xsl:matching-substring>
</xsl:analyze-string>
</xsl:variable>
it was allready in
XSL Analyze-String -> Matching-Substring into multiple variables
but my question is how to use it on example to concate
eg regex-group(1) with other element outside regex path "/fault/informations/restriction2"
so simple IMPUT XML:
<fault >
<information>
<reference1>22-00 X (AA - 03 StoLAT)</reference1>
<opr>Sample sam (66-33) Sample</opr>
</information>
</fault>
and OUTPUT I would like to have
in one element
<mytrouble>
<trouble12>AA - 03 StoLAT , Sample sam Sample</trouble12>
</mytrouble>
so the rgex group 4 - text inside ( ) from <reference1> and the whole text from opr
ps
currently by transforming:
Cannot match item type with required type Error location:
xsl:stylesheet / xsl:template / xsl:element / xsl:variable /
#as
Details XTTE0570 : The required item type of variable ' groups ' is
' element(Q{http://xml.event/1.0}group) ' ,
the supplied item type is ' text() '
GREAT THANX for
michael.hor257k
almost there - just the result indicates header
<trouble12 xmlns:fn="http://www.w3.org/2005/xpath-functions">AA - 03 StoLATSample sam Sample</trouble12>
my question is how to use it on example to concate eg regex-group(1)
with other element outside regex path
"/fault/informations/restriction2"
To refer to another node within the xsl:analyze-string instruction, capture it in a variable first - for example:
<xsl:variable name="opr" select="fault/information/opr" />
<xsl:analyze-string regex="^^([0-9-]+)([A-Z ]*)[ ]*([\(](.*)[\)])*$" select="fault/information/reference1">
<xsl:matching-substring>
<trouble12>
<xsl:value-of select="regex-group(4)"/>
<xsl:value-of select="$opr"/>
</trouble12>
</xsl:matching-substring>
</xsl:analyze-string>
I have 2 xml nodes like this, for example:
<Model>GRAND MODUS</Model>
<QualifiedDescription>2008 58 Reg Renault Grand Modus 1.2 TCE Dynamique 5drMetallic Flame Red</QualifiedDescription>
I'm trying to use substring-after to split the QualifiedDescription after the Grand Modus like this:
<xsl:variable name="something"><xsl:value-of select='substring-after(QualifiedDescription, Model)' /></xsl:variable>
But obviously it's not working being of it being case sensitive. Is it possible to get substring-after to work case insensitive, but still return the output with case preserved EG.
1.2 TCE Dynamique 5drMetallic Flame Red
Thanks.
You could convert the two strings to the same case using translate in order to work out the character offset of the first within the second, then take a substring of the original QualifiedDescription from that position.
<xsl:variable name="uc" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:variable name="lc" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="substrStart" select="
string-length(substring-before(translate(QualifiedDescription, $uc, $lc),
translate(Model, $uc, $lc)))
+ string-length(Model)
+ 1" /><!-- +1 because string indexes in XPath are 1-based -->
<xsl:variable name="something"
select="substring(QualifiedDescription, $substrStart)" />
You'd need slightly more complex logic to take account of cases where the QualifiedDescription does not include the Model (since in this case both substring-before and substring-after return the empty string) but you get the idea.
You can do case insensitive if you uppercase all first and substring on uppercase:
substring-after(upper-case(QualifiedDescription), upper-case(Model))
I have a number such as: 457342137 but i want to display it as a 457 342 137.
I have something like this:
<xsl:template match="klient/#numer_telefonu">
<xsl:variable name="numer" select="." />
<xsl:value-of select="format-number($numer, '### ### ###')" />
</xsl:template>
but it does not work.
If you want to use a non-standard 'grouping' separator, you first need define the symbols you are going to use in your format command as follows:
<xsl:decimal-format name="spaces" grouping-separator=" " />
Then, you can reference this format in the command itself as follows:
<xsl:value-of select="format-number($numer, '# ###', 'spaces')" />
Further information about decimal-format can be found at http://www.w3.org/TR/xslt#format-number
A telephone number is a string, not a number and you shouldn't try formatting it as one. Technically, you could do:
<xsl:value-of select="translate(format-number(., '#,###'), ',', ' ' )" />
to achieve the desired result in your example. However, given a "number" such as "057342137" the result will be "57 342 137" (leading zeros stripped). You should be using string functions to manipulate a string.