同时采用使用XSLT复制的取出空元素(Remove the empty elements while

2019-10-23 17:29发布

埃德:从下面复制并行元素作为另一个元素的子元素

输入XML

<root>
   <order orderId="12345">
      <cartId>12346</cartId>
       <orderPayment paymentId="1234">
      <debitCardPayment>
         <chargeAmount currencyCode="USD">22.20</chargeAmount>
         <debitCard>
            <PIN>1234</PIN>  
            <PIN1></PIN1>               
         </debitCard>
      </debitCardPayment>
   </orderPayment>
   </order>
   <context>
   </context>       
</root>

需要OUTPUT

<order>
  .....
</order>
 <orderPayment>
   .....
       <debitCard>
            <PIN>1234</PIN>  
         </debitCard>
</orderPayment>
<context>
</context>

我有XSLT如下

<xsl:template match="Root">
        <updateOrderCheckoutRequest version="1"> 
            <xsl:apply-templates select="@*|node()"/>
            <xsl:copy-of select="//orderPayment"/>
        </updateOrderCheckoutRequest>
    </xsl:template>      

      <xsl:template match="orderPayment"/>

      <xsl:template match="order"> 
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy> 
      </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:choose>
            <xsl:when test=".='' and count(@*)=0">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy>
                    <xsl:apply-templates select="@*|node()"/>
                </xsl:copy>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

这也是抄袭空元素。 是否有可能删除,而应用复制的空元素。 提前致谢...

Answer 1:

是否有可能删除,而应用复制的空元素。

不,这是不可能使用xsl:copy-of不作为是将所有内容复制。 如果你想申请到节点进一步的变化被复制,你必须采用不同的策略,例如:

XSLT 1.0

<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"/>
<xsl:strip-space elements="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/root">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()[not(self::orderPayment)]"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="order">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
        <xsl:apply-templates select="../orderPayment"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

现在orderPayment被“复制”凭借身份变换模板-你可以添加更多的特定模板覆盖,例如:

<xsl:template match="PIN1[not(string())]"/>

以除去空PIN1元件。



Answer 2:

请尝试以下的样式表:

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/root">
        <updateOrderCheckoutRequest version="1"> 
            <xsl:apply-templates select="@*|node()"/>
        </updateOrderCheckoutRequest>
    </xsl:template>

    <xsl:template match="order">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()[not(self::orderPayment)]"/>
        </xsl:copy>
        <xsl:apply-templates select="orderPayment"/>
    </xsl:template>

    <xsl:template match="*[not(string())]"/>

</xsl:stylesheet>


文章来源: Remove the empty elements while using copy-of using XSLT
标签: xml xslt xpath