Is there a split function in xpath?

22,993

There isn't a split() (or equivalent) function in XPath 1.0.

There is a tokenize() function in XPath 2.0.

One can implement splitting functionality using XSLT 1.0 -- there are several questions wtih good answers in the xslt tag.

Share:
22,993
user357034
Author by

user357034

Updated on July 20, 2022

Comments

  • user357034
    user357034 almost 2 years

    I am trying to split a text from a node <extra>text1|text2|text3|text4</extra> into four parts "|" as the delimiter and reconstruct 4 new nodes as follows.

    <g:test1>text1</g:test1>
    <g:test2>text2</g:test2>
    <g:test3>text3</g:test3>
    <g:test4>text4</g:test4>
    

    Here is the code I have, which obviously is not working but should explain what I am trying to do.

    <%
    Dim objXML, x
    
    Set objXML = CreateObject("MSXML2.DOMDocument")
    objXML.async = False
    objXML.setProperty "ServerHTTPRequest", True
    objXML.Load "http://www.thesite.com/v/myxml.xml"
    objXML.setProperty "SelectionLanguage", "XPath"
    
    Dim xmldoc: set xmldoc = CreateObject("MSXML2.DomDocument")
    xmldoc.async = false
    
    Dim instruction
    Set instruction = xmldoc.createProcessingInstruction("xml", "version=""1.0""  encoding=""UTF-8"" standalone=""yes""")
    xmldoc.appendChild instruction 
    
    Dim rss: set rss = xmldoc.createElement("rss")
    xmldoc.appendChild rss
    
    Dim itemNode2: Set itemNode2 = xmldoc.selectSingleNode(".//rss")
    Dim name: Set name = xmldoc.createAttribute("xmlns:g") 
    name.Value = "http://base.google.com/ns/1.0" 
    itemNode2.attributes.setNamedItem(name)
    
    Dim itemNode: Set itemNode = xmldoc.selectSingleNode(".//rss")
    Dim version: Set version = xmldoc.createAttribute("version") 
    version.Value = "2.0" 
    itemNode.attributes.setNamedItem(version)
    Dim channel: set channel = xmldoc.createElement("channel")
    rss.appendChild channel
    
    For Each x In objXML.documentElement.selectNodes(".//SAVED_EXPORT")
      Dim item: set item = xmldoc.createElement("item")
      channel.appendChild item
    
      Dim str1: Set str1 = x.selectSingleNode("extra")
      Dim gstrarray
      gstrarray = split(str1.text,"|")
    
      Dim gstr1: set gstr1 = xmldoc.createElement("g:test1")
      gstr1.text =gstrarry(0)
      item.appendChild gstr1
      Dim gstr2: set gstr2 = xmldoc.createElement("g:test2")
      gstr2.text =gstrarry(1)
      item.appendChild gstr2
      Dim gstr3: set gstr3 = xmldoc.createElement("g:test3")
      gstr3.text =gstrarry(2)
      item.appendChild gstr3
      Dim gstr4: set gstr4 = xmldoc.createElement("g:test4")
      gstr4.text =gstrarry(3)
      item.appendChild gstr4
    Next
    Response.Write xmldoc.xml
    %>
    
  • user357034
    user357034 almost 13 years
    Thank for that, I figured you would come around and have an answer for me :) Silly question, how would I know if can use XPATH 2.0. I am really lost here. I looked at the pages but not helping much. Thanks for your help.
  • Dimitre Novatchev
    Dimitre Novatchev almost 13 years
    @user357034: There is no .NET native implementation of XPath 2.0 (and SQL Server's XQuery implementation is of an early working draft). If you are using a compliant XSLT 2.0 or XQuery implementation (such as Saxon 9.x (has a special Saxon.NET version) or XQSharp -- especially written for .NET) then you have XPath 2.0 also as part of them.