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.4 2002/10/10 16:23:48 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 1) (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 (defun parse-xml-no-ws (str)
33 "Return list structure of XML string with removing whitespace strings"
34 (remove-tree-if #'string-ws? (parse-xml str)))
36 (defun positions-xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
37 "Returns three values: the start and end positions of contents between
38 the xml tags and the position following the close of the end tag."
46 (setq end-xmlstr (length xmlstr)))
48 (let ((bracketpos (position #\< xmlstr :start pos :end end-xmlstr)))
50 (let* ((starttag (1+ bracketpos))
51 (endtag (+ starttag taglen)))
52 (if (string= tag xmlstr :start2 starttag :end2 endtag)
53 (let* ((char-after-tag (char xmlstr endtag)))
54 (declare (character char-after-tag))
55 (if (or (char= #\> char-after-tag) (char= #\space char-after-tag))
57 (if (char= #\> char-after-tag)
58 (setq startpos (1+ endtag))
59 (setq startpos (1+ (position #\> xmlstr :start (1+ endtag)))))
60 (setq endpos (search (format nil "</~a>" tag) xmlstr
61 :start2 startpos :end2 end-xmlstr))
63 (if (and startpos endpos)
65 (setq nextpos (+ endpos taglen 3))
69 (setq pos (1+ endtag))))
70 (setq pos (1+ starttag)))
71 (when (> pos end-xmlstr)
74 (values startpos endpos nextpos)))
77 (defun xml-tag-contents (tag xmlstr &optional (start-xmlstr 0) (end-xmlstr nil))
78 "Returns two values: the string between XML start and end tag
79 and position of character following end tag."
81 (startpos endpos nextpos)
82 (positions-xml-tag-contents tag xmlstr start-xmlstr end-xmlstr)
83 (if (and startpos endpos)
84 (values (subseq xmlstr startpos endpos) nextpos)
87 (defun xml-cdata (str)
88 (concatenate 'string "<![CDATA[" str "]]>"))