5

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 query the file (I'm using xpath) to split the 'questions' element, check the position in which each number appears and check if it is number 1 or 2.

Basically I need to 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, thank you. I was going to edit the question as it was incorrect. I want to get, for example, all course names and codes whose 'questions' element (after split) has "2" in the second position, as in 1,2,2,1,1,1,2,1 Thanks!

netNewbi3
  • 279
  • 6
  • 20

3 Answers3

3

In XSLT 1.0, you would use a recursive template 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>
Community
  • 1
  • 1
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
1
/courses
   /course[
      substring-before(
         substring-after(
            question,
            ','
         ),
         ','
      ) = 2
   ]/*[self::name|self::code]

Or

/courses
   /course[
      substring(question,3,1) = '2'
   ]/*[self::name|self::code]
0

Well there's tokenize(string, pattern) in XPath, so you could write

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

But I don't get, what you mean by "do something" and "do something else". XPath is for selecting nodes / retrieving information. You need XSLT or XQuery to "do something" else.

Jonas Bötel
  • 4,452
  • 1
  • 19
  • 28
  • Hi, thank you. I was going to edit the question as it was incorrect. I want to get, for example, all course names and codes whose 'questions' element (after split) has "2" in the second position, as in 1,**2**,2,1,1,1,2,1 Thanks! – netNewbi3 Apr 12 '11 at 11:32