2013-05-27 2 views
0

생성 된 출력 및 변수에 설정된 출력에서 ​​빈 노드를 제거하는 데 도움을주십시오. XSLT는 생성 된 출력에서 ​​빈 노드를 제거하고 출력은 변수에 설정합니다.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
       xmlns:user="http://www.contoso.com" 
       version="1.0"> 
    <xsl:variable name="books"> 
    <header> 
    <book author="Michael Howard">Writing Secure Code</book> 
    <book author="Michael Kay">XSLT Reference</book> 
    </header> 
    <item> 
     <item1>item1</item1> 
     <item2></item2>//remove this empty tag 
     <item3></item3>//remove this empty tag 
    </item> 
    <summary> 
     <summary1> 
     <sum1>SUB1</sum1> 
     <sum2></sum2> //remove this empty tag 
     </summary1> 
     <summary2>Summary2</summary2> 
     <summary3>Summ3</summary3> 
    </summary> 
    </xsl:variable> 

    <xsl:template match="/"> 
    <xsl:value-of select="$books"/>  
     <xsl:for-each select="msxsl:node-set($books)/node()">     
      <xsl:apply-templates/> 
      <!--select="ms:node-set($completeDocument[1])/node()"/>-->   
     </xsl:for-each> 
    </xsl:template> 
    <xsl:template match= "*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/> 
</xsl:stylesheet> 

출력

<?xml version="1.0" encoding="utf-8"?>Writing Secure CodeXSLT Referenceitem1item2SUB1sub2Summary2Summ3Writing Secure CodeXSLT Referenceitem1item2SUB1sub2Summary2Summ3 

예상 출력

<header> 
     <book author="Michael Howard">Writing Secure Code</book> 
     <book author="Michael Kay">XSLT Reference</book> 
     </header> 
     <item> 
      <item1>item1</item1> 
     </item> 
     <summary> 
      <summary1> 
      <sum1>SUB1</sum1> 
      </summary1> 
      <summary2>Summary2</summary2> 
      <summary3>Summ3</summary3> 
     </summary> 

예상 출력을 만들어 내고 빈 노드를 제거 보여준다 다음 I는 XSLT를 만들었다.

+0

같은 예상 출력 예를 추가하십시오 할 수 있습니다. 나는 "빈 노드"를 "제거"하고 싶지 않습니다. –

+0

내 질문을 참조하십시오. 내 질문을 업데이트했습니다. 여기 내 xslt, 출력 및 예상 된 출력을 표시합니다. 내가 실제로 하나의 파일을 입력하고 다른 form.Hence로 변환 빈 노드를 제거해야합니다. 내가 변수를 만들었습니다 템플릿을 호출하여 그 변수에서 빈 노드를 제거 – user1453017

답변

1

특정 요소가 복사되지 않도록 보장하는 템플릿을 작성했지만 해당 접근 방식을 사용하려면 복사 할 노드를 복사하고 처리를 계속하는 ID 변환 템플릿을 추가해야합니다. 그렇지 않으면 요소 노드를 복사하지 않고 자식 노드를 처리하고 텍스트 노드 만 출력하는 기본 제공 템플릿 만 있습니다.

그래서 당신은

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
       xmlns:user="http://www.contoso.com" 
       exclude-result-prefixes="xsl msxsl user" 
       version="1.0"> 
    <xsl:variable name="books"> 
    <header> 
    <book author="Michael Howard">Writing Secure Code</book> 
    <book author="Michael Kay">XSLT Reference</book> 
    </header> 
    <item> 
     <item1>item1</item1> 
     <item2></item2>//remove this empty tag 
     <item3></item3>//remove this empty tag 
    </item> 
    <summary> 
     <summary1> 
     <sum1>SUB1</sum1> 
     <sum2></sum2> //remove this empty tag 
     </summary1> 
     <summary2>Summary2</summary2> 
     <summary3>Summ3</summary3> 
    </summary> 
    </xsl:variable> 

    <xsl:template match="/"> 
    <xsl:apply-templates select="msxsl:node-set($books)/node()"/> 
    </xsl:template> 

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

    <xsl:template match= "*[not(@*|*|comment()|processing-instruction()) and normalize-space()='']"/> 
</xsl:stylesheet> 
+0

안녕 마틴, 정말 고마워. ... 고마워 .. – user1453017

+0

안녕하세요 마틴, 한 가지 더 의심 스럽습니다. 하위 요소가 비어있는 빈 태그를 제거 할 수 있습니까? 내가 여기 inputfile의 을 준 //이 출력에서 ​​제거 // 항목 2 는 user1453017

+0

가 물어 고려 출력에서 ​​제거되지 않습니다 당신이 새로운 입력 샘플과 원하는 결과물을 질문 본문에 분명하게 넣는 새로운 질문입니다. 나는 코드 샘플을 교환하기가 어렵다는 의견에 두려워한다. –

관련 문제