英文:
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
<?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>
<IDS.LIST TYPE="Number">
<IDS>-1</IDS>
</IDS.LIST>
<LDGNAME>Party1</LDGNAME>
<AMOUNT>12345.00</AMOUNT>
<VATEXPAMOUNT>100.00</VATEXPAMOUNT>
<SERVICETAXDETAILS.LIST> </SERVICETAXDETAILS.LIST>
<BA.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> </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> </PAYROLLMODEOFPAYMENT.LIST>
<ATTDRECORDS.LIST> </ATTDRECORDS.LIST>
<GSTEWAYCONSIGNORADDRESS.LIST> </GSTEWAYCONSIGNORADDRESS.LIST>
<GSTEWAYCONSIGNEEADDRESS.LIST> </GSTEWAYCONSIGNEEADDRESS.LIST>
<TEMPGSTRATEDETAILS.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>
<IDS.LIST TYPE="Number">
<IDS>-1</IDS>
</IDS.LIST>
<LDGNAME>Repair and Maintance</LDGNAME>
<GSTCLASS/>
<AMOUNT>-321.00</AMOUNT>
<SERVICETAXDETAILS.LIST> </SERVICETAXDETAILS.LIST>
<BA.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> </SERVICETAXDETAILS.LIST>
<BA.LIST> </BA.LIST>
</DTL.LIST>
<PAYROLLMODEOFPAYMENT.LIST> </PAYROLLMODEOFPAYMENT.LIST>
<ATTDRECORDS.LIST> </ATTDRECORDS.LIST>
<GSTEWAYCONSIGNORADDRESS.LIST> </GSTEWAYCONSIGNORADDRESS.LIST>
<GSTEWAYCONSIGNEEADDRESS.LIST> </GSTEWAYCONSIGNEEADDRESS.LIST>
<TEMPGSTRATEDETAILS.LIST> </TEMPGSTRATEDETAILS.LIST>
</DOC>
</TMSG>
</RDATA>
</IDATA>
</BODY>
</HEAD>
I need to accomplish the following
- Eliminate all IDS.LIST elements and its child elements.
- Remove "." from the names of all other elements.
- 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
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="TMSG">
<xsl:copy>
<xsl:copy-of select="* except (IDS.LIST,DTL.LIST)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
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 :
<?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"/>
<!-- Eliminate all IDS.LIST elements and its child elements. -->
<xsl:template match="IDS.LIST" priority="2"/>
<!-- Remove "." from the names of all other elements. -->
<xsl:template match="*[contains(name(.),'.')]" priority="1">
<xsl:element name="{translate(name(.),'.','')}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<!-- Need to add DOCKEY element along with its value to DTL.LIST AND BA.LIST (if it has child elements) -->
<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>
<!-- Identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/bFD9utw/2
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论