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>
开发者_Python百科</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
You can use <xsl:import>
or <xsl:copy>
to achieve this.
精彩评论