微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用xpath拆分xml值并检查字符串位置

我有以下xml文件
<courses>
   <course>
    <name>Course 1</name>
    <code>00162</code>
    <questions>2,2,1,1</questions>
   </course>
   </courses>

我需要查询文件(我使用xpath)来拆分’questions’元素,检查每个数字出现的位置,并检查它是否为数字1或2.

基本上我需要在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

评论更新

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,1
Thanks!

在XSLT 1.0中,您将使用递归模板来拆分字符串.

借用@Tomalak’s answer to a similar question,就是一个例子:

<?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>

在XSLT 2.0中,您可以使用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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。