Split xml value with xpath and check string position

I have the following xml file:

<courses> <course> <name>Course 1</name> <code>00162</code> <questions>2,2,1,1,2,1,1,1</questions> </course> </courses> 

I need to request a file (I use xpath) to separate the β€œquestions” element, check the position in which each number is displayed, and check if it is number 1 or 2.

Basically I need this in xpath:

 Dim ints As String() = QuestionsString.ToString.Split(",") Dim i As Integer For i = 0 To UBound(ints) If ints(i) = "2" Then 'do something Else 'do something else End If Next 

Update from comments

Hi thanks I was going to edit the question as it was wrong. I want to get, for example, all course names and codes, whose question item (after the split) has "2" in the second position, as in 1, 2 , 2,1,1,1,1,2,1 Thank you!

+4
split xml xpath
Apr 12 '11 at 10:45
source share
3 answers

In XSLT 1.0, you must use a recursive pattern to split the string.

Borrowing from @Tomalak's answer to a similar question is an example:

 <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <!--Call the recursive template to split the string--> <xsl:call-template name="split"> <xsl:with-param name="list" select="/courses/course/questions" /> </xsl:call-template> </xsl:template> <xsl:template name="split"> <xsl:param name="list" select="''" /> <xsl:param name="separator" select="','" /> <xsl:if test="not($list = '' or $separator = '')"> <xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)" /> <xsl:variable name="tail" select="substring-after($list, $separator)" /> <!--Use the parsed value to do something--> <xsl:call-template name="handleQuestion"> <xsl:with-param name="value" select="$head"/> </xsl:call-template> <xsl:call-template name="split"> <xsl:with-param name="list" select="$tail" /> <xsl:with-param name="separator" select="$separator" /> </xsl:call-template> </xsl:if> </xsl:template> <xsl:template name="handleQuestion"> <xsl:param name="value" /> <xsl:choose> <xsl:when test="$value=2"> <!--Do something--> </xsl:when> <xsl:otherwise> <!--Do something else--> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet> 

In XSLT 2.0, you can use the tokenize() function:

 <?xml version="1.0"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xsl:template match="/"> <xsl:for-each select="tokenize(/courses/course/questions,',')"> <xsl:choose> <xsl:when test="number(.)=2"> <!--Do something--> </xsl:when> <xsl:otherwise> <!--Do something else--> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> </xsl:stylesheet> 
+2
Apr 12 2018-11-11T00:
source share
 /courses /course[ substring-before( substring-after( question, ',' ), ',' ) = 2 ]/*[self::name|self::code] 

or

 /courses /course[ substring(question,3,1) = '2' ]/*[self::name|self::code] 
+1
Apr 12 '11 at 17:33
source share

Ok there tokenize(string, pattern) in XPath, so you could write

 /courses/course/questions[(tokenize(text(), "[0-9]+")) ] put something clever here -------^ 

But I don’t understand what you mean by "doing something" and "doing something else." XPath is designed to select nodes / retrieve information. You need XSLT or XQuery to do something.

0
Apr 12 '11 at 11:25
source share



All Articles