My XML source looks like:
<span class="char-style-override-6">Breast Problems (Female and Male) 511</span>
and I have a template match for it
<xsl:template match="span" mode="table">
My difficulty now is within this template match, I need to create two tags, the first will contain the string "Breast Problems (Female and Male)", while the second only contain the page number "511".
I just don't know how to do this substring split, in order to differentiate text and numeric value.
Relative XPath It starts with the double forward slash (//), which means it can search the element anywhere at the webpage. You can start from the middle of the HTML DOM structure with no need to write a long XPath. Below is the example of a relative XPath expression of the same element shown in the below screen.
XML DOM splitText() MethodThe splitText() method splits the text node into two nodes at the specified offset. This function returns the node containing the text after the offset. The text before the offset remains in the original text node.
XPath (XML Path Language) is an expression language designed to support the query or transformation of XML documents. It was defined by the World Wide Web Consortium (W3C) and can be used to compute values (e.g., strings, numbers, or Boolean values) from the content of an XML document.
XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.
An 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>
applied 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With