How to use value of GrandChild in xslt

后端 未结 1 720
一个人的身影
一个人的身影 2021-01-24 15:14

I need to use the value of the grandchild provided. I tried but was unable to fetch the value of the grandchild.

This is the xml provided to me



        
相关标签:
1条回答
  • 2021-01-24 15:42

    You're pretty close, but there's a couple things here:

    • Nest the Product apply templates inside the Invoice template, in order to apply it recursively
    • You don't need the imperative style xsl-foreach - apply-templates is the better approach.
    • There's a couple typos in the xml with closing elements /b and in your xsl quotes class="entity'
    • There is a Products element which wraps Product

    Here's one way to do this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="InvoiceDocument">
        <MessageParts>
          <LedgerJournalTable class="entity">
            <xsl:apply-templates select="Invoice"/>
          </LedgerJournalTable>
        </MessageParts>
      </xsl:template>
    
      <xsl:template match="Invoice">
           <e><xsl:value-of select="normalize-space(a/text()[1])"/></e>
           <xsl:apply-templates select="Products/Product"/>
      </xsl:template>
    
      <xsl:template match="Product">
          <LedgerJournalTrans class="entity">
             <g><xsl:value-of select="normalize-space(b/text()[1])"/></g>
             <h><xsl:value-of select="normalize-space(c/text()[1])"/></h>
          </LedgerJournalTrans>
      </xsl:template>
    </xsl:stylesheet>
    

    Working fiddle here

    0 讨论(0)
提交回复
热议问题