william
william

Reputation: 1

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>

Upvotes: 0

Views: 591

Answers (3)

Flynn1179
Flynn1179

Reputation: 12075

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.

Upvotes: 1

sudmong
sudmong

Reputation: 2036

You can use <xsl:import> or <xsl:copy> to achieve this.

Upvotes: 0

Joe
Joe

Reputation: 1765

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

Upvotes: 0

Related Questions