2012-04-19 1 views
1

나는이 XML이 :XSLT - 모든 다른 사람을 버리고 주어진 요소를 유지, XSLT를 사용하여 XML 문서를 필터링하는 방법

 


    <?xml version="1.0" encoding="utf-8"?> 
    <xml> 
    <head> 
     <name>This is my XML</name> 
     <version>This is my XML</version> 
    </head> 
    <body> 
     <item id="SH2-99435"> 
      <properties> 
       <property id="69"> 
        <name>This is a property</name> 
        <amount>54.13</amount> 
        <estructure id="IZ4"> 
         <name>caterpillar</name> 
         <location><zipCode>02210</zipCode><street>South Station</street></location> 
        </estructure> 
       </property> 
       <features id="ABC3"> 
        <feature>If it works, a bug is another feature.</feature> 
        <feature>feature!!</feature> 
       </features> 
      </properties> 
      <coding> 
       <codename>Silent Hill 2</codename> 
       <developer>Team Silent</developer> 
       <publisher>Konami</publisher> 
      </coding> 
     </item> 
     <item id="SH3-4498"> 
      <text value="it has values like the other item"/> 
     </item>  
     <item id="MGS-2"> 
      <text value="it has values like the other item"/> 
     </item> 
    </body> 
    </xml> 

 

을 그리고 난이 달성하고자하는 :

 


    <?xml version="1.0" encoding="utf-8"?> 
    <xml> 
    <head> 
     <name>This is my XML</name> 
     <version>This is my XML</version> 
    </head> 
    <body> 
     <item> 
      <properties> 
       <property id="69"> 
        <amount>54.13</amount> 
        <estructure id="IZ4"> 
         <name>caterpillar</name> 
         <location><zipCode>02210</zipCode><street>South Station</street></location> 
        </estructure> 
       </property> 
      </properties> 
     </item> 
     <item id="SH3-4498"> 
      <text value="it should have properties and the selected sons"/> 
     </item>  
     <item id="MGS-2" > 
      <text value="it should have properties and the selected sons"/> 
     </item> 
    </body> 
    </xml> 
 

내가 뭔가를 다음과 같이 정확히 필터링합니다.

 


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

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

     <xsl:template match="xml/body/item/coding" /> 
     <xsl:template match="xml/body/item/properties/features" /> 
     <xsl:template match="xml/body/item/properties/property/name" /> 

    </xsl:stylesheet> 

 

나는 10 개의 파일이 실행 중임을 알게되었습니다. 각각 다른 태그를 가진 필터; 하지만 새로운 태그가 오면 필요한 파일에만 태그를 추가하는 대신 불필요한 태그를 제외하기 위해 10 개의 파일을 변경해야합니다. 예를 들어, 다른 파일에는 코딩 만 포함됩니다.

 
 

    <xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns"> 
     <xsl:output omit-xml-declaration="yes" indent="yes" /> 
     <xsl:strip-space elements="*" /> 
     <ns:WhiteList> 
      <name>amount</name> 
      <name>estructure</name> 
     </ns:WhiteList> 
     <xsl:template match="node()|@*"> 
      <xsl:copy> 
       <xsl:apply-templates select="node()|@*" /> 
      </xsl:copy> 
     </xsl:template> 
     <xsl:template 
      match="*[not(descendant-or-self::*[name()=document('')/*/ns:WhiteList/*])]" /> 
    </xsl:stylesheet> 
 

을하지만 estructure의 자식을 복사하지 않습니다 :

나는이 함께했습니다. 내가 할 수있는 것을 알고 있습니까?

감사합니다.

업데이트.이 방법으로, 그리고 다른 방법으로, 그리고 더 기술적 인 질문을하기 위해 정당성을 추가했습니다.

+0

처음 XSL이 나에게 충분히 명확 해 보입니다! 비즈니스 규칙이 특정 노드를 제외한 모든 것을 복사하는 것이면 그것은 길일 것입니다. 하나의 불일치가 있지만, 초기 XSL에서는 ** estructure ** node를 제외하고 있지만 이것은 예상 출력에 여전히 존재합니다. –

+0

@TimC 죄송합니다, 나는 구조를 포함하도록 첫 번째 xslt를 수정했습니다. – StrayChild01

답변

3

나는이 대답을 받아들이지 않을 것이지만, 귀하의 즉각적인 질문에 대한 답변으로 요소의 요소의 조상 (또는 자기) 인 요소를 테스트하고 있지만 다른 것은 무시합니다. 즉 구조의 자손 인은 무시됩니다. xsl : if 조건을 다음과 같이 변경하십시오.

<xsl:if 
    test="descendant-or-self::*[generate-id(.)=$copyValue2]|ancestor::*[generate-id(.)=$copyValue2]"> 

턱.

솔직히 말해서 첫 번째 XSLT는 훨씬 좋고 깨끗합니다.

변환과 관련된 규칙을 정확히 설명하면 도움이 될 것입니다. 규칙이 (특정 노드가)을 제외한 모든 항목을 복사하는 경우 첫 번째 XSLT가 이상적입니다. 규칙이 명시 적 노드 만 복사하는 경우 정확히 복사해야 할 대상에 대해 좀 더 자세한 정보를 제공 할 수 있습니다. 그러면 훨씬 좋은 것을 부스러 뜨릴 수 있어야합니다.

편집 : 해명에 대응

, 나는 문서 룩업을 보유 할 변수를 사용하여 조금을 정돈하는 것을 시도했다

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns="some:ns"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:strip-space elements="*"/> 

    <ns:WhiteList> 
     <name>amount</name> 
     <name>estructure</name> 
    </ns:WhiteList> 

    <xsl:variable name="whistList" select="document('')/*/ns:WhiteList" /> 

    <xsl:template match="@*"> 
     <xsl:copy/> 
    </xsl:template> 

    <xsl:template match="*"> 
     <xsl:if test="descendant-or-self::*[name()=$whistList/*]|ancestor::*[name()=$whistList/*]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
     </xsl:if> 
    </xsl:template>  
</xsl:stylesheet> 

이 XSLT를 시도합니다. xsl : if을 사용하는 이유는 템플릿 일치에서 변수를 사용할 수 없다는 점에 유의하십시오.

+0

나는 질문을 더 명확하게하기 위해 그것을 갱신했다. 나는 해결책이 매우 복잡 할 수도 있다고들 때까지 더 많은 파일을 생성하는 필터로 사용 될지 몰랐다. – StrayChild01

+0

정말 고마워요. 그것은 완벽하게 작동합니다. 내가 알아 차 렸던 한가지는 어떻게 든 그것은 제외 태그를 나열하는 것보다 느리게 실행된다는 것입니다. 그것은 비교 때문입니다. – StrayChild01

관련 문제