我想使用 XSLT 将一些元素包装在以下 XML 中,但我无法使其按预期工作:
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2017-04-10T18:28:30">
<qryXMLOrders>
<IteNumGDS7>1</IteNumGDS7>
<GooDesGDS23>Data</GooDesGDS23>
<GroMasGDS46>0.046</GroMasGDS46>
<NetMasGDS48>0.01</NetMasGDS48>
<ProReqGDI1>10</ProReqGDI1>
<PreProGDI1>00</PreProGDI1>
<ComNatProGIM1>000</ComNatProGIM1>
<StaValAmoGDI1>13.87</StaValAmoGDI1>
<PreDocTypAR21>380</PreDocTypAR21>
<PreDocRefAR26>6471</PreDocRefAR26>
<PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
<DocTypDC21>N380</DocTypDC21>
<DocRefDC23>6471</DocRefDC23>
<DocTypDC21A>Y900</DocTypDC21A>
<DocRefDC23A>-</DocRefDC23A>
<DocTypDC21B>Y922</DocTypDC21B>
<DocRefDC23B>-</DocRefDC23B>
<ComNomCMD1>71171900</ComNomCMD1>
<TARCodCMD1>00</TARCodCMD1>
<TARFirAddCodCMD1>0000</TARFirAddCodCMD1>
<TARSecAddCodCMD1>0000</TARSecAddCodCMD1>
<NAtAddCodCMD1>0000</NAtAddCodCMD1>
<NamCE27>Ruthh</NamCE27>
<StrAndNumCE222>9738 SW 94 Terr</StrAndNumCE222>
<PosCodCE223>33176</PosCodCE223>
<CitCE224>Miami</CitCE224>
<CouCE225>US</CouCE225>
<NADLNGGICE>EN</NADLNGGICE>
<MarNumOfPacGS21>RE11628425179GR</MarNumOfPacGS21>
<KinOfPacGS23>EN</KinOfPacGS23>
<NumOfPacGS24>1</NumOfPacGS24>
<InvoiceDate>2017-04-07T00:00:00</InvoiceDate>
<Order_x0020_ID>1184077028</Order_x0020_ID>
</qryXMLOrders>
</dataroot>
所以最终的XML看起来像这样:
<dataroot xmlns:od="urn:schemas-microsoft-com:officedata" generated="2017-04-10T18:28:30">
<qryXMLOrders>
<IteNumGDS7>1</IteNumGDS7>
<GooDesGDS23>Data</GooDesGDS23>
<GroMasGDS46>0.046</GroMasGDS46>
<NetMasGDS48>0.01</NetMasGDS48>
<ProReqGDI1>10</ProReqGDI1>
<PreProGDI1>00</PreProGDI1>
<ComNatProGIM1>000</ComNatProGIM1>
<StaValAmoGDI1>13.87</StaValAmoGDI1>
<PREADMREFAR2>
<PreDocTypAR21>380</PreDocTypAR21>
<PreDocRefAR26>6471</PreDocRefAR26>
<PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
</PREADMREFAR2>
<DocTypDC21>N380</DocTypDC21>
<DocRefDC23>6471</DocRefDC23>
<DocTypDC21A>Y900</DocTypDC21A>
<DocRefDC23A>-</DocRefDC23A>
<DocTypDC21B>Y922</DocTypDC21B>
<DocRefDC23B>-</DocRefDC23B>
<COMCODGODITM>
<ComNomCMD1>71171900</ComNomCMD1>
<TARCodCMD1>00</TARCodCMD1>
<TARFirAddCodCMD1>0000</TARFirAddCodCMD1>
<TARSecAddCodCMD1>0000</TARSecAddCodCMD1>
<NAtAddCodCMD1>0000</NAtAddCodCMD1>
</COMCODGODITM>
<TRACONCE2>
<NamCE27>Ruthh</NamCE27>
<StrAndNumCE222>9738 SW 94 Terr</StrAndNumCE222>
<PosCodCE223>33176</PosCodCE223>
<CitCE224>Miami</CitCE224>
<CouCE225>US</CouCE225>
<NADLNGGICE>EN</NADLNGGICE>
</TRACONCE2>
<PACGS2>
<MarNumOfPacGS21>RE11628425179GR</MarNumOfPacGS21>
<KinOfPacGS23>EN</KinOfPacGS23>
<NumOfPacGS24>1</NumOfPacGS24>
</PACGS2>
<InvoiceDate>2017-04-07T00:00:00</InvoiceDate>
<Order_x0020_ID>1184077028</Order_x0020_ID>
</qryXMLOrders>
</dataroot>
我使用以下 XSLT,但这样我让元素在 XML 中出现两次,一次解开包装,然后按预期包装:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<!-- identity rule Copy all elements and attributes from the source to the target-->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Wrap Element -->
<xsl:template match="/dataroot/qryXMLOrders">
<xsl:copy>
<xsl:apply-templates select="PreDocTypAR21|ComNomCMD1|NamCE27|MarNumOfPacGS21/preceding-sibling::*"/>
<PREADMREFAR2>
<xsl:apply-templates select="PreDocTypAR21|PreDocRefAR26|PreDocCatPREADMREF21"/>
</PREADMREFAR2>
<COMCODGODITM>
<xsl:apply-templates select="ComNomCMD1|TARCodCMD1|TARFirAddCodCMD1|TARSecAddCodCMD1|NAtAddCodCMD1"/>
</COMCODGODITM>
<TRACONCE2>
<xsl:apply-templates select="NamCE27|StrAndNumCE222|PosCodCE223|CitCE224|CouCE225|NADLNGGICE"/>
</TRACONCE2>
<PACGS2>
<xsl:apply-templates select="MarNumOfPacGS21|KinOfPacGS23|NumOfPacGS24"/>
</PACGS2>
<xsl:apply-templates select="PreDocCatPREADMREF21|NAtAddCodCMD1|NADLNGGICE|NumOfPacGS24/following-sibling::*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
您在 MarNumOfPacGS21/preceding-sibling::*
上apply-templates
,其中包含您的基本节点,在xsl:copy
末尾相同。
默认模板是复制节点,因此,如果您不想浪费时间更改 XSL 逻辑,只需创建一个模板即可跳过不需要的节点:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*"/>
<!-- identity rule Copy all elements and attributes from the source to the target-->
<xsl:template match="@*|node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- Wrap Element -->
<xsl:template match="/dataroot/qryXMLOrders">
<xsl:copy>
<xsl:apply-templates select="PreDocTypAR21|ComNomCMD1|NamCE27|MarNumOfPacGS21/preceding-sibling::*" mode="copy-if-needed"/>
<PREADMREFAR2>
<xsl:apply-templates select="PreDocTypAR21|PreDocRefAR26|PreDocCatPREADMREF21"/>
</PREADMREFAR2>
<COMCODGODITM>
<xsl:apply-templates select="ComNomCMD1|TARCodCMD1|TARFirAddCodCMD1|TARSecAddCodCMD1|NAtAddCodCMD1"/>
</COMCODGODITM>
<TRACONCE2>
<xsl:apply-templates select="NamCE27|StrAndNumCE222|PosCodCE223|CitCE224|CouCE225|NADLNGGICE"/>
</TRACONCE2>
<PACGS2>
<xsl:apply-templates select="MarNumOfPacGS21|KinOfPacGS23|NumOfPacGS24"/>
</PACGS2>
<xsl:apply-templates select="PreDocCatPREADMREF21|NAtAddCodCMD1|NADLNGGICE|NumOfPacGS24/following-sibling::*" mode="copy-if-needed"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PreDocTypAR21|PreDocRefAR26|PreDocCatPREADMREF21|ComNomCMD1|TARCodCMD1|TARFirAddCodCMD1|TARSecAddCodCMD1|NAtAddCodCMD1|NamCE27|StrAndNumCE222|PosCodCE223|CitCE224|CouCE225|NADLNGGICE|MarNumOfPacGS21|KinOfPacGS23|NumOfPacGS24" mode="copy-if-needed">
<!-- Do nothing, skip these nodes -->
</xsl:template>
<xsl:template match="*" mode="copy-if-needed">
<!-- Copy (apply default template) -->
<xsl:apply-templates select="current()"/>
</xsl:template>
</xsl:stylesheet>
未经测试,但你应该明白这个想法。