Breast Problems (Female and Male) 511

To split a string into two in XPath

My XML source looks like this:

<span class="char-style-override-6">Breast Problems (Female and Male) 511</span> 

and I have a pattern match for it

 <xsl:template match="span" mode="table"> 

Now my difficulty is to match the template, I need to create two tags, the first will contain the line "Breast problems (woman and man)", and the second only contains the page number "511".

I just don’t know how to do this to separate the text and numerical value.

+4
source share
3 answers

XSLT 2.0 Solution:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <output> <xsl:apply-templates mode="table"/> </output> </xsl:template> <xsl:template match="span" mode="table"> <xsl:variable name="split" select="replace(., '.*\s(\d+)$', '$1')"/> <string><xsl:value-of select="normalize-space(substring-before(., $split))"/></string> <number><xsl:value-of select="$split" /></number> </xsl:template> </xsl:stylesheet> 

It applies to

 <?xml version="1.0" encoding="UTF-8"?> <root> <span class="char-style-override-6">Breast Problems (Female and Male) 511</span> </root> 

gives

 <?xml version="1.0" encoding="UTF-8"?> <output> <string>Breast Problems (Female and Male)</string> <number>511</number> </output> 
+3
source

In XSLT 1.0 use :

 <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="span"> <xsl:variable name="vNumeric" select= "translate(., translate(., '0123456789', ''), '')"/> <xsl:variable name="vNonNumeric" select= "normalize-space(substring-before(., $vNumeric))"/> <nonNumeric><xsl:value-of select="$vNonNumeric"/></nonNumeric> <numeric><xsl:value-of select="$vNumeric"/></numeric> </xsl:template> </xsl:stylesheet> 

When this conversion is applied to the provided XML document :

 <span class="char-style-override-6">Breast Problems (Female and Male) 511</span> 

required, the correct result is obtained :

 <nonNumeric>511</nonNumeric> <numeric>Breast Problems (Female and Male)</numeric> 

Explanation

  • Double translation method.

  • Appropriate use of substring-before() and normalize-space() .

+2
source

You should be able tokenize: http://www.w3schools.com/xpath/xpath_functions.asp

If you know that all your nodes are limited to three spaces, this can be a good way, or you can use a regular expression and work back from the end of the node content.

+1
source

Source: https://habr.com/ru/post/1400447/


All Articles