使用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
从评论更新
在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中,您可以使用 <?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>
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- cocosdx让AppDelegate支持全局更新机制
- sqlite3删除数据,文件不变小问题解决
- VTR-to-Bitstream 2 FPGA Architecture File(.xml)
- 使用Ruby中的SCP / SSH和Amazon EC2实例中的pem文件
- xml – 如何在Xslt中将节点名替换为另一个?
- Cocos2d-x::坐标系、定时器、菜单
- c# – 解释System.Net.IPAddress的实例属性
- XStream 注解处理xml
- ruby-on-rails – 如何将操作路由到Rails 3中的应用程序控制
- ruby-on-rails – Rails 4.2.0.beta2中的奇怪行为 – 这是一
