
XML 문서인 SVG 파일이 있습니다.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<defs
id="defs2">
<!-- a lot of stuff> </defs>
<!-- more stuff-->
</svg>
다음 출력을 얻기 위해 기본 네임스페이스에 해당하는 모든 태그에 svg: 접두사를 추가하고 싶습니다.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg:svg
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape">
<svg:defs
id="defs2">
<!-- a lot of stuff> </svg:defs>
<!-- more stuff-->
</svg:svg>
나는 이것이 다음을 사용하여 쉘의 하나 또는 몇 개의 명령줄에서 가능하다고 확신합니다.xmllint및/또는xmlstarlet하지만 나는 그것을 관리할 수 없습니다.
답변1
질문하신 지 꽤 시간이 지났습니다. 그럼에도 불구하고 ...
xmlstarlet edit
의 -r
/ --rename
작업에는 새 이름에 대한 리터럴 값이 필요하므로 XPath 기능이 종료됩니다. 그러나 xmlstarlet select
편집 명령을 생성하기 위한 코드 생성기로 사용될 수 있습니다.
xmlstarlet select -t \
--var sq -o "'" -b \
-o 'xmlstarlet edit --pf \' -n \
-m 'set:distinct(//_:*)' \
-o ' -r ' -v 'concat($sq,"//_:",local-name(),$sq)' \
-o ' -v ' -v 'concat($sq,"svg:",local-name(),$sq)' -o ' \' -n \
-b \
-f -n \
file.xml
어디
- 표현식
//_:*
은 기본 네임스페이스의 모든 요소 노드와 일치합니다(_
바로가기는xmlstarlet
사용자 가이드) - EXSLT 기능
set:distinct
중복을 제거합니다 -o
문자열 리터럴,-n
개행 문자,-f
입력 경로 이름/URL을 출력합니다(그러나-
stdin의 경우).-b
현재 컨테이너를 종료합니다(-m
,--var
없이=
, ao)- 생성된 XSLT 코드를 나열하기
-C
전에 옵션 을 추가하면-t
산출:
xmlstarlet edit --pf \
-r '//_:svg' -v 'svg:svg' \
-r '//_:defs' -v 'svg:defs' \
file.xml
어디
-P
/--pf
원래 형식을 유지합니다.- 내부 편집을 지원 한 후
-L
/ 옵션 추가 (사용자 가이드가 아니라--inplace
edit
xmlstarlet.txt)
출력을 쉘 스크립트로 실행하려면 다음을 수행하십시오.
xmlstarlet-select-command | sh -s > result.xml
EXSLT를 피하려는 경우 대신 -m '//_:*' --sort 'A:T:-' .
출력을 를 통해 파이프 uniq
하거나 -m '//_:*'
가능한 중복 항목을 사용하여 파이프하십시오.
답변2
xmlstarlet
특히 잔인한 방식으로 사용 (적절한 xpath를 기다립니다)
for x in $(xmlstarlet sel -t -m "//*" -n -v "name()" file1.xml | sort | uniq); do
xmlstarlet ed -r "//svg:$x" -v "svg:$x" file1.xml > tmp.xml;
mv tmp.xml file1.xml;
done
네임스페이스를 이미 선언했으므로 노드 이름의 리터럴 값을 변경하려면 svg
에서 이를 호출해야 합니다 .xpath