2012-02-26 4 views
0

많은 변수를 사용하여 계산 된 테이블이 있습니다. 계산하는 동안, 나는 이러한 계산의 기록을 XML 파일에 보관합니다.매개 변수를 사용하는 노드 형제 템플릿 xsl로드

것은 내가 열 이름부터 송장

  • 다른부터 다른 레이아웃

    • 하나에이 XML 포맷해야합니다.

    각 '송장'에는 송장 금액의 일부가 될 수있는 '비용'이 지정되며 각 '비용'은 테이블의 열에 배치됩니다.

    내 XML은 다음과 같이이다 :

    :

    <table> 
        <invoices> 
         <invoice id="1" value="230" supplier="First supplier"/> 
        </invoices> 
        <costs> 
         <cost id="1" invoice="1" column="2" value="100"> 
          <calculation> 
           <tenant name="Tenant1" cost="30" /> 
           <tenant name="Tenant2" cost="70" /> 
          </calculation> 
         </cost> 
         <cost id="2" invoice="1" column="1" value="130"> 
          <calculation> 
           <tenant name="Tenant1" cost="50" /> 
           <tenant name="Tenant2" cost="50" /> 
          </calculation> 
          <calculation> 
           <tenant name="Tenant1" cost="10" /> 
           <tenant name="Tenant2" cost="20" /> 
          </calculation> 
         </cost> 
        </costs> 
        <columns> 
         <column id="1" name="Column name 1"/> 
         <column id="2" name="Column name 2"/> 
        </columns> 
    </table> 
    

    그래서 내 예상 출력은

    Invoice 
        Spread over a column based on cost 
         Calculations 
    

    내 XSL이 같이 보입니다

    Column name 
        Costs from which invoice 
         Calculations 
    

    또는

    같은 것
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
        <xsl:template match="/"> 
          <xsl:for-each select="table/costs/cost"> 
           <xsl:call-template name="cost"></xsl:call-template> 
           <h1>Mere</h1> 
          </xsl:for-each> 
         </html> 
        </xsl:template> 
        <xsl:template match="cost" name="cost"> 
         <xsl:apply-templates select="invoice"></xsl:apply-templates> 
         <h1>Cheltuiala <xsl:value-of select="@id"></xsl:value-of></h1> 
         <ul> 
          <li> 
          <xsl:call-template name="invoice" > 
           <xsl:with-param name="idInvoice" select="@invoice"></xsl:with-param> 
          </xsl:call-template> 
          </li> 
          <li>Si ceva avcolo</li> 
         </ul> 
        </xsl:template> 
        <xsl:template name="invoice" match="table/invioces/invoice[@[email protected]]"> 
         <xsl:param name="idInvoice"></xsl:param> 
         <p>Found something</p> 
         <h2><xsl:value-of select="@value" /></h2> 
        </xsl:template> 
    </xsl:stylesheet> 
    

    그래서 노드의 속성을 기반으로 템플릿을 호출하고 데이터를 표시 한 후 다음 노드로 계속 진행합니다.

    이것이 가능합니까?

  • +0

    당신은 정확한 변환의 출력을 원하는 제공하는 것을 잊었다. 질문을 편집하고 필요한 정보를 제공해주십시오. –

    답변

    1

    current() 함수를 사용하여 현재 컨텍스트 노드를 참조 할 수 있습니다. 그러나이 작업을 수행하려면 템플릿을 호출하거나 이전 할 때 노드를 선택해야합니다

    <xsl:template name="cost"> 
        ... 
        <xsl:for-each match="/table/invoices/invoice[@id=current()/@invoice]"> 
         <xsl:call-template name="invoice"/> 
        </xsl:for-each> 
        ... 
    </xsl:template> 
    
    • @id

      선택한 <invoice>id= 속성을 참조.
    • current()/@invoice<cost> 컨텍스트의 invoice= 속성을 나타냅니다.

    <xsl:apply-templates> 대신 + <xsl:call-template><xsl:for-each>를 사용하는 것이 더 쉽다,하지만 당신은 대신 name=match=을 사용하는 템플릿을 다시 작성해야합니다. 당신이 태그 이름을 여러 번 사용해야하는 경우

    <xsl:template match="cost"> 
        ... 
        <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]"/> 
        ... 
    </xsl:template> 
    
    <xsl:template match="invoice"> 
        ... 
    </xsl:template> 
    

    , 당신은 모드를 사용할 수 있습니다

    <xsl:template match="cost"> 
        ... 
        <xsl:apply-templates match="/table/invoices/invoice[@id=current()/@invoice]" 
             mode="cost-invoice"/> 
        ... 
    </xsl:template> 
    
    <xsl:template mode="cost-invoice" match="invoice"> 
        ... 
    </xsl:template> 
    
    관련 문제