【问题标题】:Transforming Xml using XSLT 1.0使用 XSLT 1.0 转换 Xml
【发布时间】:2020-01-02 15:43:49
【问题描述】:

我需要转换我的 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 xslt xslt-1.0


    【解决方案1】:

    标识您的 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>
    

    正如您现在看到的,根元素 &lt;InvoiceDocument&gt; 没有任何 &lt;Product&gt;&lt;Products&gt; 元素。这意味着&lt;xsl:for-each select="Product"&gt; 将找不到任何东西。

    &lt;Invoice&gt; 元素的匹配项中,添加 XSLT 选择器/运算符以访问 &lt;Products&gt; 元素。

    <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>
    

    【讨论】:

      猜你喜欢
      • 2012-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多