2012-05-26 5 views
1

이 문제점에 대한 해결책을 찾고 있는데 awk가 내 서투른 셸 스크립트 대신에 충분히 간단한 솔루션을 제공해야한다고 생각합니다.조건부로 XML 파일의 섹션을 제거하십시오

다음과 같이 여러 섹션으로 구성된 xml 파일이 있습니다. 나는 또한 가치 목록을 가지고있다. value_x 내 목록에서 삭제 각 섹션 <top_tag> ... </top_tag>를 들어

: 섹션 <top_tag> ... </top_tag>

<xml> 
<outer_tag> 
    <top_tag> 
     <tag>value_1</tag> 
     <other_tags></other_tags> 
    </top_tag> 
    <top_tag> 
     <tag>value_2</tag> 
     <other_tags></other_tags> 
    </top_tag> 
    ... 
    <top_tag> 
     <tag>value_n</tag> 
     <other_tags></other_tags> 
    </top_tag> 
</outer_tag> 

귀하의 제안이 가장 감사합니다 (즉, 인쇄되지 않음).

+1

당신은 XML 파싱 모듈을 사용해야합니다이 같은 처리 명령과 XSL 파일을 포함 할 경우 마지막으로, 웹 브라우저는 XSLT를 사용하여 파일을 변환 할 수 있습니다 Python이나 Perl 또는'xmlstarlet'과 같은 명령 행 유틸리티에서. –

+0

섹션별로 무엇을 의미합니까? 'outer_tag','top_tag','tag' 또는 다른 그룹핑? – Edwin

답변

2

여기서 필요한 것은 awk가 아니라이 종류의 작업을 위해 특별히 만들어진 XSLT입니다. 그것은 당신이 다른 XML로 XML 문서를 변형 할 수 있습니다. 많은 당신처럼 입력을 위해

:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="example.xsl"?> 
<outer_tag> 
    <top_tag> 
     <tag>value_1</tag> 
     <other_tags></other_tags> 
    </top_tag> 
    <top_tag> 
     <tag>value_2</tag> 
     <other_tags></other_tags> 
    </top_tag> 
    <top_tag> 
     <tag>value_3</tag> 
     <other_tags></other_tags> 
    </top_tag> 
    <top_tag> 
     <tag>value_n</tag> 
     <other_tags></other_tags> 
    </top_tag> 
</outer_tag> 

다음 XSLT는 단순히 복사하고 그 내용을 무시하지 않음으로써 value_3 모든 top_tag 요소를 제거합니다.

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="*"> 
     <xsl:element name="{name()}">   
      <xsl:apply-templates select="child::node()"></xsl:apply-templates> 
     </xsl:element> 
    </xsl:template> 

    <xsl:template match="top_tag[tag = 'value_3']">  
    </xsl:template> 
</xsl:stylesheet 

모든 주요 프로그래밍 언어에는 XSLT에 따라 XML 입력을 처리 할 수있는 라이브러리가 두 개 이상 있습니다. 명령 줄 도구와 UI 기반 응용 프로그램 (IDE뿐 아니라 그뿐만 아니라)도이를 수행 할 수 있습니다.

<?xml-stylesheet type="text/xsl" href="example.xsl"?> 
2

이 당신을 위해 작동 할 수 있습니다 :

sed -i '/<top_tag>/,/<\/top_tag>/!b;/<top_tag>/{h;d};H;/<\/top_tag/!d;x;/<tag>value.*<\/tag>/d' file 
+0

완벽하게 작동하지만 어떻게 작동하는지 설명 할 수 있다면 좋을 것입니다. – Scadge

관련 문제