首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在xslt中使用GrandChild的值

如何在xslt中使用GrandChild的值
EN

Stack Overflow用户
提问于 2019-12-31 14:39:19
回答 1查看 62关注 0票数 1

我需要使用提供的孙子对象的值。我试过了,但没能得到孙子孙女的价值。

这是提供给我的xml

代码语言:javascript
复制
<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格式

代码语言:javascript
复制
<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>

这是我尝试获取grandchild的值的代码。

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2019-12-31 14:55:17

你已经很接近了,但这里有几件事:

将产品应用模板嵌套在发票模板内,为了应用它,recursively

  • You不需要命令式样式-
  • 更好的做法是在带有结束元素的xml中使用几个拼写错误,并且在您的xsl引号中recursively
  • You是包装的Products元素Products

这里有一种方法:

代码语言:javascript
复制
<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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59540533

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档