2011-09-30 2 views
0

나는 다음과 같은 입력 XML 파일이 있습니다익스트림 XSLT의 XML

OUTPUT 1

<root> 
    <row b="1" e="2" f="3" h="4" m="5" n="6" o="7" /> 
    <row b="1" e="2" f="3" h="4" m="8" n="9" o="0" /> 
<root> 

출력 :

<root> 
<a> 
    <b>1</b> 
</a> 
<c> 
    <d> 
    <e>2</e> 
    <f>3</f> or <e>3</e> 
    </d> 
    <g h="4"/> 
    <i> 
    <j> 
     <k> 
     <l m="5" n="6" o="7" /> 
     <l m="8" n="9" o="0" /> 
     </k> 
    </j> 
    </i> 
</c> 
</root> 

내가 추적 출력으로 변환하는 XSLT를 사용하고 싶습니다를 2

<root> 
    <row b="1" e="2" h="4" m="5" n="6" o="7" /> 
    <row b="1" e="2" h="4" m="8" n="9" o="0" /> 
    <row b="1" e="3" h="4" m="5" n="6" o="7" /> 
    <row b="1" e="3" h="4" m="8" n="9" o="0" /> 
<root> 

누구든지 가능 내 XSLT가 그리 강하지 않은지 도와주세요. 감사.

+0

당신이 즉, 2 개 XSLT 파일이 필요하십니까은이 변환을 수행? –

+0

네, 저는 그들이 매우 유사하다고 생각합니다. – David

+0

그럼 논리를 설명해 주시겠습니까? –

답변

0

<e>의 발생으로 인해 <row>을 구성하는 외부 루프가 결정되고 내부 루프가 모두 <l> 반복되는 경우가 쉬울 것입니다. 이 같은

시도 뭔가 :

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

    <xsl:template match="/"> 
     <xsl:apply-templates /> 
    </xsl:template> 

    <xsl:template match="root"> 
     <root> 
      <xsl:apply-templates /> 
     </root> 
    </xsl:template> 

    <xsl:template match="e"> 
     <!-- store values of 'e' and 'f' in params --> 
     <xsl:param name="value_of_e" select="." /> 
     <xsl:param name="value_of_f" select="ancestor::d[1]/f" /> 
     <!-- iterate over all 'l's --> 
     <xsl:for-each select="//l"> 
      <xsl:element name="row"> 
       <xsl:attribute name="b"> 
        <xsl:value-of select="//b" /> 
       </xsl:attribute> 
       <xsl:attribute name="e"> 
        <xsl:value-of select="$value_of_e" /> 
       </xsl:attribute> 
       <!-- only include 'f' if it has a value --> 
       <xsl:if test="$value_of_f != ''"> 
        <xsl:attribute name="f"> 
         <xsl:value-of select="$value_of_f" /> 
        </xsl:attribute> 
       </xsl:if> 
       <xsl:attribute name="h"> 
        <xsl:value-of select="ancestor::c[1]/g/@h" /> 
       </xsl:attribute> 
       <xsl:attribute name="m"> 
        <xsl:value-of select="./@m" /> 
       </xsl:attribute> 
       <xsl:attribute name="n"> 
        <xsl:value-of select="./@n" /> 
       </xsl:attribute> 
       <xsl:attribute name="o"> 
        <xsl:value-of select="./@o" /> 
       </xsl:attribute> 
      </xsl:element> 
     </xsl:for-each> 
    </xsl:template> 

    <xsl:template match="b" /> 
    <xsl:template match="f" /> 
    <xsl:template match="g" /> 
</xsl:stylesheet>