简体   繁体   中英

Extracting a number from a string in a XSLT file

Is there a way to extract a number from a string after a set of characters.

Our software uses XSLT files to convert emails into XML files. In the Subject line of an email there can be a reference to an already opened Incident/Service Request/Task.

For example - the Subject of an email could be:

RE: SR#51417: D_SATTER-NOV60LKA-I_G-A0201244

I want to extract the Service Request Number 51417 from the Subject. The number will always be after the String "SR#". "SR#" could be written as "sr#", "Sr#" or "sR#".

I was trying to use the RegEx functions in XSLT but can't get it to work.

Do you have any suggestions on how to do this?

Thanks in advance.

Update I am trying the solution provided by cyclexx. This is the Code that I have put in my XSLT File:

<xsl:when test="contains($subject, 'SR#')"> <xsl:element name="Field"> <xsl:attribute name="Name"> <xsl:text>a_eco_parentObjectType</xsl:text> </xsl:attribute> <xsl:value-of select="'ServiceReq'"></xsl:value-of> </xsl:element> <xsl:element name="Field"> <xsl:attribute name="Name"> <xsl:text>a_eco_parentObjectID</xsl:text> </xsl:attribute> <xsl:analyze-string select ="$subject" regex="\s*[Ss][Rr]#([0-9]+)\s*"> <xsl:matching-substring> <SR> <xsl:value-of select="regex-group(1)"></xsl:value-of> </SR> </xsl:matching-substring> </xsl:analyze-string> </xsl:element>

The variable $subject contains the Subject line of the Email file that is being processed. The output file just contains:

ServiceReq

and I have an error message: :Error in loading Hierarchical Object XSLT file(s)

Here a solution :)

 <emails> <email> <subject>RE: SR#51417: D_SATTER-NOV60LKA-I_G-A0201244</subject> </email> <email> <subject>RE: Sr#565465: D_SATTER-NOV60LKA-I_G-A0201244</subject> </email> </emails> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="text" /> <xsl:template match="subject"> <xsl:variable name="v" select="." /> <xsl:analyze-string select="$v" regex="\\s*[Ss][Rr]#([0-9]+)\\s*"> <xsl:matching-substring> <SR> <xsl:value-of select="regex-group(1)" /> </SR> </xsl:matching-substring> <xsl:non-matching-substring> <subject> <xsl:value-of select="$v"/> </subject> </xsl:non-matching-substring> </xsl:analyze-string> </xsl:template> </xsl:stylesheet> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM