我需要使用提供的孙子的值。我曾尝试过,但无法获取孙子的价值。
这是提供给我的xml
<InvoiceDocument>
<Invoice>
<d>100</d>
<a>120</a>
<Products>
<Product>
<b>11<b>
<c>12</c>
</Product>
<Product>
<b>13</b>
<c>14</c>
</Product>
</Products>
</Invoice>
</InvoiceDocument>
这是我需要的xml格式
<MessageParts>
<LedgerJournalTable class="entity">
<e>120</e>
<LedgerJournalTrans class="entity'>
<g>11</g>
<h>12</h>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity'>
<g>13</g>
<h>14</h>
</LedgerJournalTrans>
</LedgerJournalTable>
</MessageParts>
这是我尝试获取孙子代价值的代码。
<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"/>
<LedgerJournalTrans class="entity'>
<xsl:for-each select="Product">
<xsl:apply-templates select="Product"/>
</xsl:for-each>
</LedgerJournalTrans>
</LedgerJournalTable>
</MessageParts>
</xsl:template>
<xsl:template match="Invoice">
<e><xsl:value-of select="normalize-space(a/text()[1])"/></e>
</xsl:template>
<xsl:template match="Product">
<g><xsl:value-of select="normalize-space(b/text()[1])"/></g>
<h><xsl:value-of select="normalize-space(c/text()[1])"/></h>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:0)
您非常接近,但是这里有几件事:
xsl-foreach
-apply-templates
是更好的方法。/b
,而您的xsl引号为class="entity'
Products
元素,它包装了Product
这是执行此操作的一种方法:
<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>