2011-09-25 19 views
5

Sto provando a dividere un testo da un nodo <extra>text1|text2|text3|text4</extra> in quattro parti "|" come delimitatore e ricostruire 4 nuovi nodi come segue.Esiste una funzione di divisione in xpath?

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

Ecco il codice che ho, che ovviamente non funziona, ma dovrebbe spiegare quello che sto cercando di fare.

<% 
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 
%> 
+0

Stai chiedendo come dividere in XPath o perché la tua suddivisione in ASP non funziona? –

+0

Immagino un po 'di entrambi se non ti dispiace spiegarlo ad un novizio. – user357034

risposta

6

non c'è una funzione split() (o equivalente) in XPath 1.0.

C'è una funzione tokenize() in XPath 2.0.

Si può implementare la funzionalità scissione utilizzando XSLT 1.0 - ci sono diverse domande addebbitato good answers nel tag XSLT.

+0

Grazie per quello, ho pensato che saresti venuto in giro e avere una risposta per me :) Domanda stupida, come potrei sapere se è possibile utilizzare XPATH 2.0. Sono davvero perso qui. Ho guardato le pagine ma non ho aiutato molto. Grazie per l'aiuto. – user357034

+0

@ user357034: non esiste un'implementazione nativa .NET di XPath 2.0 (e l'implementazione XQuery di SQL Server è di una bozza di lavoro iniziale). Se stai usando un'implementazione XSLT 2.0 o XQuery compatibile (come Saxon 9.x (ha una versione speciale Saxon.NET) o XQSharp - specialmente scritta per .NET) allora hai anche XPath 2.0 come parte di essi. –

Problemi correlati