2009-06-03 3 views
0

은과 같이 소스 문서를 감안할 때 :XSLT를 사용하면 일련의 요소를 단일 목록으로 변환 할 수 있습니까?

<A> 
    <B> 
    <C>item1</C> 
    </B> 
    <B> 
    <C>item2</C> 
    </B> 
    <B> 
    <C>item3</C> 
    </B> 
</A> 

이 같은 제품 일에 사용할 수 있습니다 무엇 XSLT : 내가 함께 시트를 시도

<A> 
    <B> 
    <C>item1</C> 
    <C>item2</C> 
    <C>item3</C> 
    </B> 
</A> 

...

<xsl:template match="B"> 
    <xsl:choose> 
     <xsl:when test="count(preceding-sibling::B)=0"> 
     <B> 
      <xsl:apply-templates select="./C"/> 
      <xsl:apply-templates select="following-sibling::B"/> 
     </B> 
     </xsl:when> 

     <xsl:otherwise> 
      <xsl:apply-templates select="./C"/> 
     </xsl:otherwise> 

    </xsl:choose> 
    </xsl:template> 

그러나 나는 점점 ...

<A> 
    <B> 
    <C>item1</C> 
    <C>item2</C> 
    <C>item3</C> 
    </B> 
    <C>item2</C> 
    <C>item3</C> 
</A> 

두 번째 질문 : XSLT를 디버깅하는 데 어려움이 있습니다. 힌트?

답변

3

간단한 방법 :

<xsl:template match="/A"> 
    <A> 
    <B> 
     <xsl:copy-of select=".//C" /> 
    </B> 
    </A> 
</xsl:template> 

당신은 당신이 당신의 XSLT와 함께 볼 수있는 출력을 볼 이유 질문에 대답하려면 :

나는 당신이 그 자리에

<xsl:apply-templates select="B" /> 

을 가지고 aussume. 이는 :

<xsl:template match="B">
  • 한 번 각 <B>를 들어, 세 번이라고합니다.
  • 첫 번째 <B>의 경우 의도 한대로 수행하고 다른 한 번은 <xsl:otherwise>으로, 을 <xsl:template match="C">으로 복사합니다. 이것은 귀하의 여분 <C>가 어디서 왔는지입니다.
  • 는 ... (아니 내가 당신의 접근 방식을 추천하는) 당신이 같은 변경해야

그것을 해결하려면 ...이 :

<xsl:apply-templates select="B[1]" /> 
+0

안녕하세요 저는 이번에 추가 템플릿을 가져 왔습니다 ... 마지막 게시물에 D : –

2

당신은 Altova XMLSPY 또는를위한 비주얼 스튜디오를 사용할 수 있습니다 디버깅. 다음 xslt는 원하는 o/p를 줄 것입니다. 위의 SOLN가 (난 당신이 더 복잡한 XML을 다루는 것으로 의심되는) 당신을 위해 작동하지 않는 경우

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> 
    <xsl:template match="A"> 
     <A> 
      <B> 
       <xsl:apply-templates/> 
      </B> 
     </A> 
    </xsl:template> 
    <xsl:template match="B"> 
     <xsl:copy-of select="C"/> 
    </xsl:template> 
</xsl:stylesheet> 

, 당신은 당신의 XML에 대한 좀 더 많은 정보를 게시 할 수 있습니다.

또한 B 및 C 템플릿을 사용하면 reqd 인 경우 템플릿 o/p를 추가로 확장 할 수 있습니다.

1

두 번째 질문에 대답하려면 VS 2008 debugger을 사용하고 매력처럼 작동합니다.

1

이 스타일 시트는 스타일 시트의 위치 또는 다른 요소 이름과 관계없이 형제 <B> 요소를 병합합니다.

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

    <!-- By default, copy all nodes unchanged --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Only copy the first B element --> 
    <xsl:template match="B[1]"> 
    <xsl:copy> 
     <!-- Merge the attributes and content of all sibling B elements --> 
     <xsl:apply-templates select="../B/@* | 
            ../B/node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- Don't copy the rest of the B elements --> 
    <xsl:template match="B"/> 

</xsl:stylesheet> 

UPDATE :

당신은 결과가 꽤 - 인쇄하고, 공백 전용 텍스트 노드가 사용자의 입력 문서에 중요하지 않은 경우, 당신은 (스타일 시트의 상단에이를 추가 할 수 원하는 경우 <xsl:stylesheet>의 자녀) :

<xsl:strip-space elements="*"/> 
<xsl:output indent="yes"/> 
관련 문제