复制具有属性但没有子元素的元素

时间:2018-09-22 18:02:59

标签: xml xslt copy

使用以下xml文档

   <div>
    <select id="orderStatus" name="orderStatus">
    <option value="0"></option>
    <option value="Available">Available</option>
    <option value="Arranging Product">Arranging Product</option>
    <option value="Awaiting Fulfillment">Awaiting Fulfillment</option>
    <option value="Awaiting Payment">Awaiting Payment</option>
    <option value="Awaiting Pickup">Awaiting Pickup</option>
    <option value="Awaiting Shipment">Awaiting Shipment</option>
    </select>

     // I think here is the problem when I click button it forgets selected index value    
      <a href="order.php" onmousedown="this.href=this.href+'?orderStatus='+document.getElementById('orderStatus').value" class="btn btn-large">Update</a>

   </div>

和以下xslt

<?xml version="1.0" encoding="UTF-8"?>

<a name="john">
   <b/>
</a>

输出为

<?xml version="1.0" encoding="utf-8"?>

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

    <xsl:template match="a">
        <xsl:copy/>
    </xsl:template>

</xsl:stylesheet>

我想要的是<?xml version="1.0" encoding="UTF-8"?><a/> 。如何获取元素<a name="John"/>及其属性a 而没有其子元素name

1 个答案:

答案 0 :(得分:2)

也复制属性:

<xsl:template match="a">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
    </xsl:copy>
</xsl:template>