使用 xpath 拆分 xml 值并检查字符串位置 [英] split xml value with xpath and check string position

查看:29
本文介绍了使用 xpath 拆分 xml 值并检查字符串位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 xml 文件:

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>

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

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.

基本上我需要在 xpath 中这样做:

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

评论更新

您好,谢谢.我要编辑问题,因为它是不正确的.我想要例如,获取所有课程名称和代码的问题"元素(拆分后)在第二个中有2"位置,如 1,2,2,1,1,1,2,1谢谢!

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!

推荐答案

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

In XSLT 1.0, you would use a recursive template to split the string.

借用 @Tomalak 对类似问题的回答,是一个例子:

<?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() 函数:

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>

这篇关于使用 xpath 拆分 xml 值并检查字符串位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆