加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 23:54:00 所属栏目:百科 来源:网络整理
导读:我有以下xml文件: courses course nameCourse 1/name code00162/code questions2,2,1,1/questions /course /courses 我需要查询文件(我使用xpath)来拆分’questions’元素,检查每个数字出现的位置,并检查它是否为数字1或2. 基本上我需要在xpath中这样做: D
我有以下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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读