Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

To split the string into two in XPath

Tags:

xml

xslt

xpath

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.

like image 919
Kevin Avatar asked Mar 08 '12 19:03

Kevin


People also ask

What is the meaning of '/' in XPath?

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.

How to use split in xml?

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.

What is XPath string?

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.

What is the function of XPath?

XPath is a language for addressing parts of an XML document, designed to be used by both XSLT and XPointer.


1 Answers

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>
like image 76
Maestro13 Avatar answered Nov 15 '22 12:11

Maestro13