Domanda

Voglio analizzare il seguente codice XML:

(cxml:parse "<BEGIN><URL>www.some.de/url?some=data&bad=stuff</URL></BEGIN>" (stp:make-builder))
.

Questo risulta in

 #<CXML:WELL-FORMEDNESS-VIOLATION "~A" {1003C5E163}>
.

come '&' è un carattere speciale XML.Ma se utilizzo &amp;? invece il risultato è:

(cxml:parse "<BEGIN><URL>www.some.de/url?some=data&amp;bad=stuff</URL></BEGIN>" (stp:make-builder))
=>#.(CXML-STP-IMPL::DOCUMENT
   :CHILDREN '(#.(CXML-STP:ELEMENT
                  #| :PARENT of type DOCUMENT |#
                  :CHILDREN '(#.(CXML-STP:ELEMENT
                                 #| :PARENT of type ELEMENT |#
                                 :CHILDREN '(#.(CXML-STP:TEXT
                                                #| :PARENT of type ELEMENT |#
                                                :DATA "www.some.de/url?some=data")
                                             #.(CXML-STP:TEXT
                                                #| :PARENT of type ELEMENT |#
                                                :DATA "&")
                                             #.(CXML-STP:TEXT
                                                #| :PARENT of type ELEMENT |#
                                                :DATA "bad=stuff"))
                                 :LOCAL-NAME "URL"))
                  :LOCAL-NAME "BEGIN")))
.

Che non è esattamente quello che mi aspettavo come dovrebbe essere solo un cxml-stp: Bambino di testo con dati "www.some.de/url?some=Data&bad=Stuff"

Come posso risolvere questo comportamento sbagliato (?)?

È stato utile?

Soluzione

Questo comportamento, anche se, non molto conveniente, è in realtà, presente anche in molti altri parser XML.Probabilmente il motivo per cui è essere in grado di analizzare entità XML arbitrarie e applicare alcune regole definite dall'utente a loro.Sebbene, potrebbe essere solo un sottoprodotto dell'implementazione del parser.Non ho ancora scoperto.

Per la variante del sax del parser sono arrivato al seguente approccio:

(defclass my-sax (sax:sax-parser-mixin)
  ((title :accessor title :initform nil)
   (tag :accessor tag :initform nil)
   (text :accessor text :initform "")))

(defmethod sax:start-element ((sax my-sax) namespace-uri local-name
                              qname attributes)
  (with-slots (tag tagcount text) sax
              (setf tag local-name
                    text "")))

(defmethod sax:characters ((sax my-sax) data)
  (with-slots (title tag text) sax
    (switch (tag :test 'string=)
      ("text"  (setf text (conatenate 'string text data)))
      ("title" (setf title data)))))

(defmethod sax:end-element ((sax my-sax) namespace-uri local-name qname)
  (with-slots (title tag text) sax
    (when (string= "text" local-name)
      ;; process (text sax)
    )))
.

I.e.Raccolgo il testo in sax:characters ed elaborarlo in sax:end-element.In STP, probabilmente, puoi allontanarti ancora più semplice semplicemente concatenando gli elementi text vicini.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top