简体   繁体   中英

copying data from on XML file to another

I want to copy the content from one XML file( items.xml) into another xml file(product.xml) where available='yes", by using xsl. How can I do that? I have the follwoing XML file

item.xml:

<items>
    <item available="yes" >
        <name> laptop  </name>
        <quantity>  2 </quantity>
    </item>
    <item available="yes" >
        <name> mouse </name>
        <quantity> 1 </quantity>
    </item>
    <item available="no" >
        <name> keyboad </name>
        <quantity> 0</quantity>
    </item>
</items>

output:

<items>
  <item>
    <name> laptop </name>
    <quantity> 2 </quantity>
  </item>
  <item>
    <name> mouse </name>
    <quantity> 1 </quantity>
  </item>
  <item available="no">
    <name> keyboad </name>
    <quantity>0</quantity>
  </item>
</items>

The XSLT you need is simply the identity rule with an additional template for the ones you want to remove, that generates no output.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="item/@available[.='yes']" />

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

The first template is more specific, and will override the more general template below.

What do you mean by saying "by using xsl if available" ?
However the only way to acoomplish your goal is to parse it. It depends by the technology you use, in Java for example I there are several way to parse an XML file: DOM, SAX, STAX.
Good luck

您可以使用<xsl:import><xsl:copy>来实现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM