为什么xsl:copy-of对我不起作用?

时间:2017-03-11 12:27:16

标签: xml xslt xslt-1.0

我有以下XSLT,它包含三个特定元素,然后重命名更多节点。问题是它只包装了初始元素,并且XSLT的下一部分没有运行。如果我注释掉XSLT的包装部分,重命名就会没有错误。

<?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:copy-of select="PreDocTypAR21/preceding-sibling::*"/>
            <PREADMREFAR2>
                <xsl:copy-of select="PreDocTypAR21|PreDocRefAR26|PreDocCatPREADMREF21"/>
            </PREADMREFAR2>

            <xsl:copy-of select="PreDocCatPREADMREF21/following-sibling::*"/>
        </xsl:copy>

    </xsl:template>


    <xsl:template match="/dataroot/qryXMLOrders/DocTypDC21A">
        <DocTypDC21>
            <xsl:apply-templates select="@* | node()"/>
        </DocTypDC21>
    </xsl:template>

    <xsl:template match="/dataroot/qryXMLOrders/DocRefDC23A">
        <DocRefDC23>
            <xsl:apply-templates select="@*|node()" />
        </DocRefDC23>
    </xsl:template>

    <xsl:template match="/dataroot/qryXMLOrders/DocTypDC21B">
        <DocTypDC21>
            <xsl:apply-templates select="@*|node()" />
        </DocTypDC21>
    </xsl:template>

    <xsl:template match="/dataroot/qryXMLOrders/DocRefDC23B">
        <DocRefDC23>
            <xsl:apply-templates select="@*|node()" />
        </DocRefDC23>
    </xsl:template>

</xsl:stylesheet>

根元素是&#34; dataroot&#34;所有其他元素都在&#34; qryXMLOrders&#34;重复

因此,我想要转换的XML的一部分就像这样

<dataroot generated="2017-03-11T14:06:08" xmlns:od="urn:schemas-microsoft-com:officedata">
    <qryXMLOrders>
        <IteNumGDS7>1</IteNumGDS7>
        <GroMasGDS46>0.046</GroMasGDS46>
        <NetMasGDS48>0.01</NetMasGDS48>
        <ProReqGDI1>10</ProReqGDI1>
        <PreProGDI1>00</PreProGDI1>
        <ComNatProGIM1>000</ComNatProGIM1>
        <StaValAmoGDI1>17.04</StaValAmoGDI1>
        <PreDocTypAR21>380</PreDocTypAR21>
        <PreDocRefAR26>6376</PreDocRefAR26>
        <PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
        <DocTypDC21>N380</DocTypDC21>
        <DocRefDC23>6376</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>Martha</NamCE27>
        <StrAndNumCE222>apt 5F</StrAndNumCE222>
        <PosCodCE223>10034</PosCodCE223>
        <CitCE224>New York</CitCE224>
        <CouCE225>US</CouCE225>
        <NADLNGGICE>EN</NADLNGGICE>
        <MarNumOfPacGS21>RE5935</MarNumOfPacGS21>
        <KinOfPacGS23>EN</KinOfPacGS23>
        <NumOfPacGS24>1</NumOfPacGS24>
        <InvoiceDate>2017-03-10T00:00:00</InvoiceDate>
    </qryXMLOrders>
    <qryXMLOrders>
        <IteNumGDS7>1</IteNumGDS7>
        <GroMasGDS46>0.046</GroMasGDS46>
        <NetMasGDS48>0.01</NetMasGDS48>
        <ProReqGDI1>10</ProReqGDI1>
        <PreProGDI1>00</PreProGDI1>
        <ComNatProGIM1>000</ComNatProGIM1>
        <StaValAmoGDI1>18.85</StaValAmoGDI1>
        <PreDocTypAR21>380</PreDocTypAR21>
        <PreDocRefAR26>6379</PreDocRefAR26>
        <PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
        <DocTypDC21>N380</DocTypDC21>
        <DocRefDC23>6379</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>fgfgfgf</NamCE27>
        <StrAndNumCE222>507 E Water St</StrAndNumCE222>
        <PosCodCE223>77901-9257</PosCodCE223>
        <CitCE224>Victoria</CitCE224>
        <CouCE225>US</CouCE225>
        <NADLNGGICE>EN</NADLNGGICE>
        <MarNumOfPacGS21>RE59357</MarNumOfPacGS21>
        <KinOfPacGS23>EN</KinOfPacGS23>
        <NumOfPacGS24>1</NumOfPacGS24>
        <InvoiceDate>2017-03-10T00:00:00</InvoiceDate>
    </qryXMLOrders>
</dataroot>

以及XSLT之后的预期结果:

<dataroot generated="2017-03-11T14:06:08" xmlns:od="urn:schemas-microsoft-com:officedata">
    <qryXMLOrders>
        <IteNumGDS7>1</IteNumGDS7>
        <GroMasGDS46>0.046</GroMasGDS46>
        <NetMasGDS48>0.01</NetMasGDS48>
        <ProReqGDI1>10</ProReqGDI1>
        <PreProGDI1>00</PreProGDI1>
        <ComNatProGIM1>000</ComNatProGIM1>
        <StaValAmoGDI1>17.04</StaValAmoGDI1>
        <PREADMREFAR2>
            <PreDocTypAR21>380</PreDocTypAR21>
            <PreDocRefAR26>6376</PreDocRefAR26>
            <PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
        </PREADMREFAR2>
        <DocTypDC21>N380</DocTypDC21>
        <DocRefDC23>6376</DocRefDC23>
        <DocTypDC21>Y900</DocTypDC21>
        <DocRefDC23>-</DocRefDC23>
        <DocTypDC21>Y922</DocTypDC21>
        <DocRefDC23>-</DocRefDC23>
        <ComNomCMD1>71171900</ComNomCMD1>
        <TARCodCMD1>00</TARCodCMD1>
        <TARFirAddCodCMD1>0000</TARFirAddCodCMD1>
        <TARSecAddCodCMD1>0000</TARSecAddCodCMD1>
        <NAtAddCodCMD1>0000</NAtAddCodCMD1>
        <NamCE27>Martha</NamCE27>
        <StrAndNumCE222>apt 5F</StrAndNumCE222>
        <PosCodCE223>10034</PosCodCE223>
        <CitCE224>New York</CitCE224>
        <CouCE225>US</CouCE225>
        <NADLNGGICE>EN</NADLNGGICE>
        <MarNumOfPacGS21>RE5935</MarNumOfPacGS21>
        <KinOfPacGS23>EN</KinOfPacGS23>
        <NumOfPacGS24>1</NumOfPacGS24>
        <InvoiceDate>2017-03-10T00:00:00</InvoiceDate>
    </qryXMLOrders>
    <qryXMLOrders>
        <IteNumGDS7>1</IteNumGDS7>
        <GroMasGDS46>0.046</GroMasGDS46>
        <NetMasGDS48>0.01</NetMasGDS48>
        <ProReqGDI1>10</ProReqGDI1>
        <PreProGDI1>00</PreProGDI1>
        <ComNatProGIM1>000</ComNatProGIM1>
        <StaValAmoGDI1>18.85</StaValAmoGDI1>
        <PREADMREFAR2>
            <PreDocTypAR21>380</PreDocTypAR21>
            <PreDocRefAR26>6379</PreDocRefAR26>
            <PreDocCatPREADMREF21>Z</PreDocCatPREADMREF21>
        </PREADMREFAR2>
        <DocTypDC21>N380</DocTypDC21>
        <DocRefDC23>6379</DocRefDC23>
        <DocTypDC21>Y900</DocTypDC21>
        <DocRefDC23>-</DocRefDC23>
        <DocTypDC21>Y922</DocTypDC21>
        <DocRefDC23>-</DocRefDC23>
        <ComNomCMD1>71171900</ComNomCMD1>
        <TARCodCMD1>00</TARCodCMD1>
        <TARFirAddCodCMD1>0000</TARFirAddCodCMD1>
        <TARSecAddCodCMD1>0000</TARSecAddCodCMD1>
        <NAtAddCodCMD1>0000</NAtAddCodCMD1>
        <NamCE27>fgfgfgf</NamCE27>
        <StrAndNumCE222>507 E Water St</StrAndNumCE222>
        <PosCodCE223>77901-9257</PosCodCE223>
        <CitCE224>Victoria</CitCE224>
        <CouCE225>US</CouCE225>
        <NADLNGGICE>EN</NADLNGGICE>
        <MarNumOfPacGS21>RE59357</MarNumOfPacGS21>
        <KinOfPacGS23>EN</KinOfPacGS23>
        <NumOfPacGS24>1</NumOfPacGS24>
        <InvoiceDate>2017-03-10T00:00:00</InvoiceDate>
    </qryXMLOrders>
</dataroot>

非常感谢, 尼科斯

1 个答案:

答案 0 :(得分:2)

由于您拥有与包装节点匹配的模板,因此您需要在执行包装的模板中使用xsl:apply-templates而不是xsl:copy-of