Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split xml value with xpath and check string position

Tags:

split

xml

xpath

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!

like image 772
netNewbi3 Avatar asked Oct 11 '22 07:10

netNewbi3


1 Answers

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>
like image 158
Mads Hansen Avatar answered Oct 21 '22 06:10

Mads Hansen