Source

LIME / lib / xmlnorm.awk

#!/usr/bin/awk -f
##################################################################
# Normalize XML, promote attributes to tags, keep some as attributes
# (id, xlink: stuff).
#
#Copyright (C) 2008 NXP Semiconductors B.V.
#
#This file is part of LIME.
#
#LIME is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 2
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#LIME is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with LIME.  If not, see <http://www.gnu.org/licenses/>.
##################################################################
BEGIN {
	if (!passthrough) passthrough=1
}

func handle(t,b) {
    	if (DEBUG) print "NORM: handling tag","<" t ">","body","'" b "'","in",state >STDERR
	# remove history
	delete id[lev+1]
	if (t=="@id") if (!no_propid) {
		for (j=lev-1;j>0;j--) if (Tag[j]!=RootTag)
		{
			if (!id[j]) id[j]="_" rand()*1000000
			b=unquote(id[j]) ":" b
		}
		return toid(b)
	}
	# remove history
	delete label[lev+1]
	if (t=="@xlink:label" && Tag[lev-1]!=RootTag) {
		b=append(unquote(label[lev-1]),b,":")
		return label[lev]=b
	}
	if (t=="id") return id[lev]=b
	return b 
}

func toid(n) {
	if (n ~ /^[0-9]/) n="_" n
	return gensub(/[[:space:]]/,"_","g",n)
}

@include xml-parser.awk