移除多个节点并将XML关联化。

huangapple go评论53阅读模式
英文:

Remove multiple nodes and make the XML relational

问题

以下是您的XML输入的翻译结果:

<?xml version="1.0"?>
<HEAD>
 <BODY>
  <IDATA>
   <RDATA>
    <TMSG xmlns:UDF="TUDF">
     <DOC RID="21ca3c51-21b8-2196-8bd6-e046365c19c8-00000020" DOCKEY="16ca3c51-21b8-4496-8bd6-e046365c19c8-0000acfe:00000018" DOCTYPE="R" ACTION="Create" OBJVIEW="Accounting DOC View">
      <IDS.LIST TYPE="Number">
       <IDS>-1</IDS>
      </IDS.LIST>
      <DATE>20210401</DATE>
      <NARRATION>NARR1</NARRATION>
      <PARTYLDGNAME>Party1</PARTYLDGNAME>
      <DOCTYPENAME>R</DOCTYPENAME>
      <DOCNUMBER>1</DOCNUMBER>
      <DOCKEY>1</DOCKEY>
      <DTL.LIST>
       <LDGNAME>Party1</LDGNAME>
       <AMOUNT>12345.00</AMOUNT>
       <VATEXPAMOUNT>100.00</VATEXPAMOUNT>
       <SERVICETAXDETAILS.LIST/>
       <BA.LIST/>
      </DTL.LIST>
      <DTL.LIST>
       <IDS.LIST TYPE="Number">
        <IDS>-1</IDS>
       </IDS.LIST>
       <LDGNAME>Bank1</LDGNAME>
       <AMOUNT>-12345.00</AMOUNT>
       <SERVICETAXDETAILS.LIST/>
       <BA.LIST>
        <DATE>20210401</DATE>
        <INSTRUMENTDATE>20210401</INSTRUMENTDATE>
        <NAME>e0d8881e-f228-4a66-8a00-717e11eee657</NAME>
        <TRANSACTIONTYPE>Inter Bank Transfer</TRANSACTIONTYPE>
        <PAYMENTFAVOURING>PARTY4</PAYMENTFAVOURING>
        <AMOUNT>-12345.00</AMOUNT>
       </BA.LIST>
      </DTL.LIST>
      <PAYROLLMODEOFPAYMENT.LIST/>
      <ATTDRECORDS.LIST/>
      <GSTEWAYCONSIGNORADDRESS.LIST/>
      <GSTEWAYCONSIGNEEADDRESS.LIST/>
      <TEMPGSTRATEDETAILS.LIST/>
     </DOC>
    </TMSG>
    <TMSG xmlns:UDF="TUDF">
     <DOC RID="21ca3c51-21b8-2196-8bd6-e046365c19c8-00000053" DOCKEY="16ca3c51-21b8-4496-8bd6-e046365c19c8-0000acfe:00000020" DOCTYPE="P" ACTION="Create" OBJVIEW="Accounting DOC View">
      <IDS.LIST TYPE="Number">
       <IDS>-1</IDS>
      </IDS.LIST>
      <DATE>20210401</DATE>
      <NARRATION>NARR2</NARRATION>
      <PARTYLDGNAME>Cash</PARTYLDGNAME>
      <DOCTYPENAME>P</DOCTYPENAME>
      <DOCNUMBER>1</DOCNUMBER>
      <DOCKEY>2</DOCKEY>
      <DTL.LIST>
       <LDGNAME>Repair and Maintance</LDGNAME>
       <GSTCLASS/>
       <AMOUNT>-321.00</AMOUNT>
       <SERVICETAXDETAILS.LIST/>
       <BA.LIST/>
      </DTL.LIST>
      <DTL.LIST>
       <IDS.LIST TYPE="Number">
        <IDS>-1</IDS>
       </IDS.LIST>
       <LDGNAME>Cash</LDGNAME>
       <GSTCLASS/>
       <AMOUNT>321.00</AMOUNT>
       <SERVICETAXDETAILS.LIST/>
       <BA.LIST/>
      </DTL.LIST>
      <PAYROLLMODEOFPAYMENT.LIST/>
      <ATTDRECORDS.LIST/>
      <GSTEWAYCONSIGNORADDRESS.LIST/>
      <GSTEWAYCONSIGNEEADDRESS.LIST/>
      <TEMPGSTRATEDETAILS.LIST/>
     </DOC>
    </TMSG>
   </RDATA>
  </IDATA>
 </BODY>
</HEAD>

请注意,上述翻译只是您提供的XML的翻译。如果您需要对XML进行进一步的操作,例如删除"IDS.LIST"元素及其子元素,去除元素名称中的"".""等操作,您可以使用XSLT模板来实现这些操作。您已经提供了一个XSLT模板的示例,但它需要进一步完善以满足您的需求。如果您有关于XSLT模板的具体问题或需要更多帮助,请随时提问。

英文:

Below is my input XML

&lt;?xml version=&quot;1.0&quot;?&gt;
&lt;HEAD&gt;
&lt;BODY&gt;
&lt;IDATA&gt;
&lt;RDATA&gt;
&lt;TMSG xmlns:UDF=&quot;TUDF&quot;&gt;
&lt;DOC RID=&quot;21ca3c51-21b8-2196-8bd6-e046365c19c8-00000020&quot; DOCKEY=&quot;16ca3c51-21b8-4496-8bd6-e046365c19c8-0000acfe:00000018&quot; DOCTYPE=&quot;R&quot; ACTION=&quot;Create&quot; OBJVIEW=&quot;Accounting DOC View&quot;&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;DATE&gt;20210401&lt;/DATE&gt;
&lt;NARRATION&gt;NARR1&lt;/NARRATION&gt;
&lt;PARTYLDGNAME&gt;Party1&lt;/PARTYLDGNAME&gt;
&lt;DOCTYPENAME&gt;R&lt;/DOCTYPENAME&gt;
&lt;DOCNUMBER&gt;1&lt;/DOCNUMBER&gt;
&lt;DOCKEY&gt;1&lt;/DOCKEY&gt;
&lt;DTL.LIST&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;LDGNAME&gt;Party1&lt;/LDGNAME&gt;
&lt;AMOUNT&gt;12345.00&lt;/AMOUNT&gt;
&lt;VATEXPAMOUNT&gt;100.00&lt;/VATEXPAMOUNT&gt;
&lt;SERVICETAXDETAILS.LIST&gt;       &lt;/SERVICETAXDETAILS.LIST&gt;
&lt;BA.LIST&gt;       &lt;/BA.LIST&gt;
&lt;/DTL.LIST&gt;
&lt;DTL.LIST&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;LDGNAME&gt;Bank1&lt;/LDGNAME&gt;
&lt;AMOUNT&gt;-12345.00&lt;/AMOUNT&gt;
&lt;SERVICETAXDETAILS.LIST&gt;       &lt;/SERVICETAXDETAILS.LIST&gt;
&lt;BA.LIST&gt;
&lt;DATE&gt;20210401&lt;/DATE&gt;
&lt;INSTRUMENTDATE&gt;20210401&lt;/INSTRUMENTDATE&gt;
&lt;NAME&gt;e0d8881e-f228-4a66-8a00-717e11eee657&lt;/NAME&gt;
&lt;TRANSACTIONTYPE&gt;Inter Bank Transfer&lt;/TRANSACTIONTYPE&gt;
&lt;PAYMENTFAVOURING&gt;PARTY4&lt;/PAYMENTFAVOURING&gt;
&lt;AMOUNT&gt;-12345.00&lt;/AMOUNT&gt;
&lt;/BA.LIST&gt;
&lt;/DTL.LIST&gt;
&lt;PAYROLLMODEOFPAYMENT.LIST&gt;      &lt;/PAYROLLMODEOFPAYMENT.LIST&gt;
&lt;ATTDRECORDS.LIST&gt;      &lt;/ATTDRECORDS.LIST&gt;
&lt;GSTEWAYCONSIGNORADDRESS.LIST&gt;      &lt;/GSTEWAYCONSIGNORADDRESS.LIST&gt;
&lt;GSTEWAYCONSIGNEEADDRESS.LIST&gt;      &lt;/GSTEWAYCONSIGNEEADDRESS.LIST&gt;
&lt;TEMPGSTRATEDETAILS.LIST&gt;      &lt;/TEMPGSTRATEDETAILS.LIST&gt;
&lt;/DOC&gt;
&lt;/TMSG&gt;
&lt;TMSG xmlns:UDF=&quot;TUDF&quot;&gt;
&lt;DOC RID=&quot;21ca3c51-21b8-2196-8bd6-e046365c19c8-00000053&quot; DOCKEY=&quot;16ca3c51-21b8-4496-8bd6-e046365c19c8-0000acfe:00000020&quot; DOCTYPE=&quot;P&quot; ACTION=&quot;Create&quot; OBJVIEW=&quot;Accounting DOC View&quot;&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;DATE&gt;20210401&lt;/DATE&gt;
&lt;NARRATION&gt;NARR2&lt;/NARRATION&gt;
&lt;PARTYLDGNAME&gt;Cash&lt;/PARTYLDGNAME&gt;
&lt;DOCTYPENAME&gt;P&lt;/DOCTYPENAME&gt;
&lt;DOCNUMBER&gt;1&lt;/DOCNUMBER&gt;
&lt;DOCKEY&gt;2&lt;/DOCKEY&gt;
&lt;DTL.LIST&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;LDGNAME&gt;Repair and Maintance&lt;/LDGNAME&gt;
&lt;GSTCLASS/&gt;
&lt;AMOUNT&gt;-321.00&lt;/AMOUNT&gt;
&lt;SERVICETAXDETAILS.LIST&gt;       &lt;/SERVICETAXDETAILS.LIST&gt;
&lt;BA.LIST&gt;       &lt;/BA.LIST&gt;
&lt;/DTL.LIST&gt;
&lt;DTL.LIST&gt;
&lt;IDS.LIST TYPE=&quot;Number&quot;&gt;
&lt;IDS&gt;-1&lt;/IDS&gt;
&lt;/IDS.LIST&gt;
&lt;LDGNAME&gt;Cash&lt;/LDGNAME&gt;
&lt;GSTCLASS/&gt;
&lt;AMOUNT&gt;321.00&lt;/AMOUNT&gt;
&lt;SERVICETAXDETAILS.LIST&gt;       &lt;/SERVICETAXDETAILS.LIST&gt;
&lt;BA.LIST&gt;       &lt;/BA.LIST&gt;
&lt;/DTL.LIST&gt;
&lt;PAYROLLMODEOFPAYMENT.LIST&gt;      &lt;/PAYROLLMODEOFPAYMENT.LIST&gt;
&lt;ATTDRECORDS.LIST&gt;      &lt;/ATTDRECORDS.LIST&gt;
&lt;GSTEWAYCONSIGNORADDRESS.LIST&gt;      &lt;/GSTEWAYCONSIGNORADDRESS.LIST&gt;
&lt;GSTEWAYCONSIGNEEADDRESS.LIST&gt;      &lt;/GSTEWAYCONSIGNEEADDRESS.LIST&gt;
&lt;TEMPGSTRATEDETAILS.LIST&gt;      &lt;/TEMPGSTRATEDETAILS.LIST&gt;
&lt;/DOC&gt;
&lt;/TMSG&gt;
&lt;/RDATA&gt;
&lt;/IDATA&gt;
&lt;/BODY&gt;
&lt;/HEAD&gt;

I need to accomplish the following

  1. Eliminate all IDS.LIST elements and its child elements.
  2. Remove "." from the names of all other elements.
  3. Need to add DOCKEY element along with its value to DTL.LIST AND BA.LIST (if it has child elements)

I started with below XSL to first remove ALL the IDS.LIST element, but I am stuck as it doesn't remove if I add the 2nd item in the except list

&lt;xsl:stylesheet version=&quot;2.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; indent=&quot;yes&quot;/&gt;
&lt;xsl:strip-space elements=&quot;*&quot;/&gt;
&lt;xsl:template match=&quot;TMSG&quot;&gt;
&lt;xsl:copy&gt;
&lt;xsl:copy-of select=&quot;* except (IDS.LIST,DTL.LIST)&quot;/&gt;
&lt;/xsl:copy&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

Not sure what I am doing wrong. I am new to XSLT.

答案1

得分: 2

你可以尝试类似这样的代码:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  
  <xsl:output method="xml" indent="yes"/>
  
  <!-- 删除所有IDS.LIST元素及其子元素。 -->
  <xsl:template match="IDS.LIST" priority="2"/>
  
  <!-- 从所有其他元素的名称中移除“.”。 -->
  <xsl:template match="*[contains(name(.),'.'))]" priority="1">
    <xsl:element name="{translate(name(.),'.','')}">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>
  
  <!-- 需要将DOCKEY元素及其值添加到DTL.LIST和BA.LIST中(如果它们有子元素)。 -->
  <xsl:template match="DTL.LIST[node()] | BA.LIST[node()]" priority="2">
    <xsl:element name="{translate(name(.),'.','')}">
      <xsl:copy-of select="../DOCKEY | ../../DOCKEY"/>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

  <!-- 原样复制 -->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  
</xsl:stylesheet>

在这里查看它的工作原理:https://xsltfiddle.liberty-development.net/bFD9utw/2

英文:

You could try something like this :

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;xsl:stylesheet xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
version=&quot;1.0&quot;&gt;
&lt;xsl:output method=&quot;xml&quot; indent=&quot;yes&quot;/&gt;
&lt;!-- Eliminate all IDS.LIST elements and its child elements. --&gt;
&lt;xsl:template match=&quot;IDS.LIST&quot; priority=&quot;2&quot;/&gt;
&lt;!-- Remove &quot;.&quot; from the names of all other elements. --&gt;
&lt;xsl:template match=&quot;*[contains(name(.),&#39;.&#39;)]&quot; priority=&quot;1&quot;&gt;
&lt;xsl:element name=&quot;{translate(name(.),&#39;.&#39;,&#39;&#39;)}&quot;&gt;
&lt;xsl:apply-templates/&gt;
&lt;/xsl:element&gt;
&lt;/xsl:template&gt;
&lt;!-- Need to add DOCKEY element along with its value to DTL.LIST AND BA.LIST (if it has child elements) --&gt;
&lt;xsl:template match=&quot;DTL.LIST[node()] | BA.LIST[node()]&quot; priority=&quot;2&quot;&gt;
&lt;xsl:element name=&quot;{translate(name(.),&#39;.&#39;,&#39;&#39;)}&quot;&gt;
&lt;xsl:copy-of select=&quot;../DOCKEY | ../../DOCKEY&quot;/&gt;
&lt;xsl:apply-templates/&gt;
&lt;/xsl:element&gt;
&lt;/xsl:template&gt;
&lt;!-- Identity transform --&gt;
&lt;xsl:template match=&quot;@*|node()&quot;&gt;
&lt;xsl:copy&gt;
&lt;xsl:apply-templates select=&quot;@*|node()&quot;/&gt;
&lt;/xsl:copy&gt;
&lt;/xsl:template&gt;
&lt;/xsl:stylesheet&gt;

See it working here : https://xsltfiddle.liberty-development.net/bFD9utw/2

huangapple
  • 本文由 发表于 2023年6月6日 01:57:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定