1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: xml-utils.lisp
6 ;;;; Purpose: XML utilities
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: xml-utils.lisp,v 1.6 2002/12/04 16:49:23 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
20 (declaim (optimize (speed 3) (safety 2) (compilation-speed 0) (debug 3)))
23 (defun wrap-with-xml (str entity)
24 "Returns string of xml header along with entity tag start/end with str contents"
25 (format nil "<?xml version=\"1.0\" standalone=\"yes\"?>~%~%<~a>~%~a~%</~a>~%"
29 ;;; XML Extraction Functions
32 #+allegro (require :pxml)
34 (defun parse-xml-no-ws (str)
35 "Return list structure of XML string with removing whitespace strings"
36 (remove-tree-if #'string-ws? (parse-xml str)))
39 (defun positions-xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
40 "Returns three values: the start and end positions of contents between
41 the xml tags and the position following the close of the end tag."
49 (setq end-xmlstr (length xmlstr)))
51 (let ((bracketpos (position #\< xmlstr :start pos :end end-xmlstr)))
53 (let* ((starttag (1+ bracketpos))
54 (endtag (+ starttag taglen)))
55 (if (and (< endtag end-xmlstr)
56 (string= tag xmlstr :start2 starttag :end2 endtag))
57 (let* ((char-after-tag (char xmlstr endtag)))
58 (declare (character char-after-tag))
59 (if (or (char= #\> char-after-tag) (char= #\space char-after-tag))
61 (if (char= #\> char-after-tag)
62 (setq startpos (1+ endtag))
63 (setq startpos (1+ (position #\> xmlstr :start (1+ endtag)))))
64 (setq endpos (search (format nil "</~a>" tag) xmlstr
65 :start2 startpos :end2 end-xmlstr))
67 (if (and startpos endpos)
69 (setq nextpos (+ endpos taglen 3))
73 (setq pos (1+ endtag))))
74 (setq pos (1+ starttag)))
75 (when (> pos end-xmlstr)
78 (values startpos endpos nextpos)))
81 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
82 "Returns two values: the string between XML start and end tag
83 and position of character following end tag."
85 (startpos endpos nextpos)
86 (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
87 (if (and startpos endpos)
88 (values (subseq xmlstr startpos endpos) nextpos)
91 (defun xml-cdata (str)
92 (concatenate 'string "<![CDATA[" str "]]>"))