2017-12-16 24 views
-1

모두 잘하고있는 것 같아요 !! 나는 아래는 XML 가지 내용이요소를 검색하고 조치를 취하십시오

:

은 내가 다음을 수행해야하는 시나리오가 있습니다.

`<?xml version="1.0" encoding="UTF-8" ?> 
<a> 
    <b> 
     <c> 
      <key>One</key> 
      <value>Value1</value> 
     </c> 
     <c> 
      <key>Two</key> 
      <value>Value2</value> 
     </c> 
     <c> 
      <key>Three</key> 
      <value>Value3</value> 
     </c> 
     <c> 
      <key>Four</key> 
      <value>Value4</value> 
     </c> 
     <c> 
      <key>Five</key> 
      <value>Value5</value> 
     </c> 
    </b> 
    <d> 
     <e> 
      <Five>Check this feild</Five> 
      <Seven>It is extra</Seven> 
      <tewnty>extra</tewnty> 
     </e> 
     <f> 
      <Three>it is present</Three> 
      <Five>Came again</Five> 
     </f> 
     <g> 
      <Four>It is here</Four> 
     </g> 
    </d> 
    <n> 
     <Five>Dont check under n</Five> 
     <Six>Extra under n</Six> 
     <Three>Dont check under n</Three> 
    </n> 
</a>` 

는 지금은 /a/b/c/key/text()이 요소 이름 /a/d

true의 경우로 존재하는 경우, 현재의 키/값 쌍을 복사 확인합니다. 출력 할 키/값 쌍을 무시하지 않을 경우.

`<?xml version="1.0" encoding="UTF-8" ?> 
<a> 
    <b> 
     <c> 
      <key>Three</key> 
      <value>Value3</value> 
     </c> 
     <c> 
      <key>Four</key> 
      <value>Value4</value> 
     </c> 
     <c> 
      <key>Five</key> 
      <value>Value5</value> 
     </c> 
    </b> 
    <d> 
     <e> 
      <Five>Check this feild</Five> 
      <Seven>It is extra</Seven> 
      <tewnty>extra</tewnty> 
     </e> 
     <f> 
      <Three>it is present</Three> 
      <Five>Came again</Five> 
     </f> 
     <g> 
      <Four>It is here</Four> 
     </g> 
    </d> 
    <n> 
     <Five>Dont check under n</Five> 
     <Six>Extra under n</Six> 
     <Three>Dont check under n</Three> 
    </n> 
</a>` 

당신이 XSLT 좀 도와 주시겠습니까 :

그래서 내 출력과 같아야합니다.

나는 노드를 가지고 local-name()으로 확인하고 있습니다.

XSLT에서 어떻게 구현할 수 있습니까?

내 질문에, 요소 이름 = (c/key/text())에 따라 노드를 제거 할 수 있습니까 (내 경우 somevaluesomevalue).

제 질문이 구체적이기를 바랍니다.

감사 & 감사 샨 드루는

+1

은 읽어 보시기 바랍니다 [** 왜 "누군가가 나를 도와 드릴까요?"입니다하지 실제 질문을? **] (https://meta.stackoverflow.com/q/284236/290085) – kjhughes

답변

1

다음 스타일 시트는 local-name()를 사용 /a/d/*/* 아래의 요소에 대한 xsl:key을 사용하여 다음 기준입니다 /a/b/*에서 요소를 제외하는대로 text() 값이 xsl:key에있는 모든 항목을 찾을 수없는 것을 사용한다.

이것은 identity transformation으로 수정되었습니다. 기본 처리는 모든 것을 보존하는 것입니다. xsl:key을 사용하는 두 x 째 빈 템플리트와 일치하는 요소는 해당 요소가 제거되도록합니다. 에 대한

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" encoding="UTF-8" /> 

    <xsl:key name="filter" match="https://stackoverflow.com/a/d/*/*" use="local-name()"/> 

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

    <xsl:template match="https://stackoverflow.com/a/b/c[not(key('filter', key/text()))]"/> 

</xsl:stylesheet> 
+0

감사 MADS 댓글. 나는 이것을 시도했다. 입력을 출력으로 생성 중입니다. – user2071463

+1

@ user2071463, http://xsltfiddle.liberty-development.net/jyyiVhe (XSLT 3 버전) 및 http://xsltfiddle.liberty-development.net/jyyiVhe/1 (XSLT 2 버전)에서 Mads 접근 방식을 볼 수 있습니다. 핵심 작업. –