使用 XSLT 1.0 转换 Xml

Transforming Xml using XSLT 1.0

我有一个要求,我需要转换我的 xml。

这是给我的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>
<z>50</z>
<LedgerJournalTrans class="entity'>
<e>120</e>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity'>
<g>11</g>
<h>12</h>
</LedgerJournalTrans>
<LedgerJournalTrans class="entity'>
<g>13</g>
<h>14</h>
</LedgerJournalTrans>
</LedgerJournalTable>
</MessageParts>

我尝试使用此代码来转换我的 xml 但无法转换它

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="InvoiceDocument">
    <MessageParts>
    <LedgerJournalTable class="entity">
    <z>50</z>
    <LedgerJournalTrans class="entity'>
    <xsl:apply-templates select="Invoice"/>
    </LedgerJournalTrans>
    <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>

而且我之前发布了一个错误的问题,对此深表歉意

识别您的 XML 文档以查看哪个元素嵌套在哪个其他元素中。当您缩进 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>

如您现在所见,根元素 <InvoiceDocument> 没有任何 <Product><Products> 元素。这意味着 <xsl:for-each select="Product"> 将找不到任何东西。

<Invoice> 元素的匹配项中,添加 XSLT selectors/operators 以访问 <Products> 元素。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="InvoiceDocument">
    <MessageParts>
        <LedgerJournalTable class="entity">
            <z>50</z>
            <xsl:apply-templates select="Invoice"/>
        </LedgerJournalTable>
    </MessageParts>
  </xsl:template>

  <xsl:template match="Invoice">
      <LedgerJournalTrans class="entity">
          <e><xsl:value-of select="normalize-space(a/text()[1])"/></e>
      </LedgerJournalTrans>
      <xsl:apply-templates select="Products" /> <!-- handle products here -->
  </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>

这将生成以下 XML 文档:

<MessageParts>
    <LedgerJournalTable class="entity">
        <z>50</z>
        <LedgerJournalTrans class="entity">
            <e>120</e>
        </LedgerJournalTrans>
        <LedgerJournalTrans class="entity">
            <g>11</g>
            <h>12</h>
        </LedgerJournalTrans>
        <LedgerJournalTrans class="entity">
            <g>13</g>
            <h>14</h>
        </LedgerJournalTrans>
    </LedgerJournalTable>
</MessageParts>