스타일 시트 및 XSLTProc을 사용하여 XSLT를 사용하여 XML에서 요소를 제거하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/321860

  •  11-07-2019
  •  | 
  •  

문제

양식의 XML 파일이 많이 있습니다.

<Element fruit="apple" animal="cat" />

파일에서 제거하고 싶습니다.

XSLT 스타일 시트와 Linux 명령 줄 유틸리티 XSLTPROC를 사용하면 어떻게 할 수 있습니까?

이 시점에서 스크립트 의이 시점까지 이미 제거하려는 요소가 포함 된 파일 목록이 있으므로 단일 파일을 매개 변수로 사용할 수 있습니다.


편집하다: 문제는 원래 의도가 부족했습니다.

내가 달성하려는 것은 전체 요소 "요소"를 제거하는 것입니다. 같은 문서에는 "요소"라는 요소가 많이 있습니다. 그래서

<Element fruit="orange" animal="dog" />
<Element fruit="apple"  animal="cat" />
<Element fruit="pear"   animal="wild three eyed mongoose of kentucky" />

될 것입니다 :

<Element fruit="orange" animal="dog" />
<Element fruit="pear"   animal="wild three eyed mongoose of kentucky" />
도움이 되었습니까?

해결책

가장 근본적인 XSLT 디자인 패턴 중 하나 사용 : " 신원 변환"하나는 다음을 작성합니다.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes"/>

    <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="node()|@*"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="Element[@fruit='apple' and @animal='cat']"/>
</xsl:stylesheet>

주목하십시오 두 번째 템플릿이 ID (1st) 템플릿을 "Apple"값을 가진 "Fruit"와 "Cat"의 값을 가진 "Animal"속성을 갖는 "요소"라는 요소에 대해서만 ID (1st) 템플릿을 무시하는 방법. 이 템플릿에는 빈 본체가 있습니다. 즉, 일치하는 요소가 단순히 무시됩니다 (일치 할 때는 아무것도 생성되지 않습니다).

이 변환이 다음 소스 XML 문서에 적용될 때 :

<doc>... 
    <Element name="same">foo</Element>...
    <Element fruit="apple" animal="cat" />
    <Element fruit="pear" animal="cat" />
    <Element name="same">baz</Element>...
    <Element name="same">foobar</Element>...
</doc>

원하는 결과가 생성됩니다.

<doc>... 
    <Element name="same">foo</Element>...
    <Element fruit="pear" animal="cat"/>
    <Element name="same">baz</Element>...
    <Element name="same">foobar</Element>...
</doc>

Identity Template를 사용하고 재정의하는 더 많은 코드 스 니펫을 찾을 수 있습니다. 여기.

다른 팁

답변 @dimitre novatchev 확실히 정확하고 우아하지만 일반화가 있습니다 (OP가 묻지 않은 것) : 필터링하려는 요소에 자식 요소 나 텍스트가 있으면 어떻게해야합니다. 유지하다?

이 사소한 변형이 그 경우를 다루고 있다고 생각합니다.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    version="2.0">

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <!-- drop DropMe elements, keeping child text and elements -->
    <xsl:template match="DropMe">
        <xsl:apply-templates/>
    </xsl:template>

</xsl:stylesheet>

일치 조건은 다른 속성 등을 지정하는 데 복잡 할 수 있으며 다른 물건을 떨어 뜨리는 경우 여러 템플릿을 사용할 수 있습니다.

그래서이 입력 :

<?xml version="1.0" encoding="UTF-8"?>
<mydocument>
    <p>Here's text to keep</p>
    <p><DropMe>Keep this text but not the element</DropMe>; and keep what follows.</p>
    <p><DropMe>Also keep this text and <b>this child element</b> too</DropMe>, along with what follows.</p>
</mydocument>

이 출력을 생성합니다.

<?xml version="1.0" encoding="UTF-8"?><mydocument>
    <p>Here's text to keep</p>
    <p>Keep this text but not the element; and keep what follows.</p>
    <p>Also keep this text and <b>this child element</b> too, along with what follows.</p>
</mydocument>

신용 XSLT 요리 책.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top