简单节点从XML复制到XML

时间:2014-09-18 22:26:55

标签: xml xslt-2.0

我有一个XML片段:

<?xml version="1.0" encoding="UTF-8"?>
        <POSLog xmlns="http://www.nrf-arts.org/IXRetail/namespace/"
        xmlns:dtv="http://www.datavantagecorp.com/xstore/"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.nrf-arts.org/IXRetail/namespace/POSLog.xsd">
<dtv:PosTransactionProperties>
<dtv:PosTransactionPropertyCode><![CDATA[RECEIPT_EMAIL_ADDRESS]]></dtv:PosTransactionPropertyCode>
<dtv:PosTransactionPropertyValue><![CDATA[qwerty@yahoo.com]]></dtv:PosTransactionPropertyValue>
</dtv:PosTransactionProperties>
</POSLog>

以下XSLT代码,现在只复制来自特定选定元素的数据:

<xsl:stylesheet version="2.0"
                 xmlns:dtv="http://www.datavantagecorp.com/xstore/"
                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        >

<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="//POSLog">
<POSLog>
                <xsl:for-each select = "../dtv:PosTransactionProperties[dtv:PosTransactionPropertyCode='RECEIPT_EMAIL_ADDRESS']/dtv:PosTransactionPropertyValue[string-length() &gt; 0]" >
                <xsl:copy-of select = "." />
            </xsl:for-each>
</POSLog>
</xsl:template>


</xsl:stylesheet>      

但是,根据我的要求,我需要将整个节点“dtv:PosTransactionProperties”复制到输出XML,请你提出如何进行复制的建议。

1 个答案:

答案 0 :(得分:0)

这个怎么样?

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:dtv="http://www.datavantagecorp.com/xstore/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs dtv"
version="2.0">  

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="POSLog" xpath-default-namespace="http://www.nrf-arts.org/IXRetail/namespace/">
    <xsl:element name="{name(.)}">
        <xsl:apply-templates select="dtv:PosTransactionProperties[dtv:PosTransactionPropertyCode='RECEIPT_EMAIL_ADDRESS']/dtv:PosTransactionPropertyValue[string-length() &gt; 0]"/>
    </xsl:element>
</xsl:template>

<xsl:template match="dtv:PosTransactionProperties[dtv:PosTransactionPropertyCode='RECEIPT_EMAIL_ADDRESS']/dtv:PosTransactionPropertyValue[string-length() &gt; 0]">
    <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template> 

</xsl:stylesheet> 

Markup_Fanatic,

感谢您的帮助,您的代码产生了以下内容:


<POSLog>
   <dtv:PosTransactionPropertyValue xmlns="http://www.nrf-arts.org/IXRetail/namespace/"
                                    xmlns:dtv="http://www.datavantagecorp.com/xstore/"
                                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">qwerty@yahoo.com</dtv:PosTransactionPropertyValue>
</POSLog>

但这不是我需要的,我能够修改它:


<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
xmlns:dtv="http://www.datavantagecorp.com/xstore/"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs dtv"
version="2.0">  

<xsl:output omit-xml-declaration="yes" indent="yes"/>

<xsl:template match="dtv:PosTransactionProperties[dtv:PosTransactionPropertyCode='RECEIPT_EMAIL_ADDRESS']/dtv:PosTransactionPropertyValue[string-length() &gt; 0]">
        <xsl:copy-of select=".."/>
</xsl:template> 

</xsl:stylesheet> 

它产生了以下内容:


RECEIPT_EMAIL_ADDRESS
<dtv:PosTransactionProperties xmlns="http://www.nrf-arts.org/IXRetail/namespace/"
                              xmlns:dtv="http://www.datavantagecorp.com/xstore/"
                              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <dtv:PosTransactionPropertyCode>RECEIPT_EMAIL_ADDRESS</dtv:PosTransactionPropertyCode>
   <dtv:PosTransactionPropertyValue>qwerty@yahoo.com</dtv:PosTransactionPropertyValue>
</dtv:PosTransactionProperties>

这是非常接近目标,只有一个问题,如何摆脱&#34; RECEIPT_EMAIL_ADDRESS&#34;在顶部,这是元素的值

<dtv:PosTransactionPropertyCode>RECEIPT_EMAIL_ADDRESS</dtv:PosTransactionPropertyCode>.