I am trying to process an XML file with XSLT, where I need to select nodes by child value. I tried to simple filter out results by regex, but maybe it is not the most elegant way to do this. I think XSLT is an universal solution to do this transform, if not please suggest me a better solution. The XML file actually look like this:
<?xml version="1.0" encoding="windows-1250"?>
<SHOP>
<SHOPITEM>
<unknown_element1></unknown_element1>
<unknown_element2></unknown_element2>
...
<YEAR>2015</YEAR>
<unknown_elementxy></unknown_elementxy>
...
</SHOPITEM>
<SHOPITEM>
<unknown_element1></unknown_element1>
<unknown_element2></unknown_element2>
...
<YEAR>2014</YEAR>
<unknown_elementxy></unknown_elementxy>
...
</SHOPITEM>
<SHOPITEM>
<unknown_element1></unknown_element1>
<unknown_element2></unknown_element2>
...
<YEAR>2015</YEAR>
<unknown_elementxy></unknown_elementxy>
...
</SHOPITEM>
</SHOP>
I want to keep current XML structure, but keep only nodes where = 2015. The desidered output:
<?xml version="1.0" encoding="windows-1250"?>
<SHOP>
<SHOPITEM>
<unknown_element1></unknown_element1>
<unknown_element2></unknown_element2>
...
<YEAR>2015</YEAR>
<unknown_elementxy></unknown_elementxy>
...
</SHOPITEM>
<SHOPITEM>
<unknown_element1></unknown_element1>
<unknown_element2></unknown_element2>
...
<YEAR>2015</YEAR>
<unknown_elementxy></unknown_elementxy>
...
</SHOPITEM>
</SHOP>
<unknown_element>
= there are many different elements in the node, the XSLT should keep current structure of the actual node. Just need to filter out nodes by <YEAR>
.
I am trying something like this:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes" method="xml"/>
<xsl:template match="//SHOPITEM[YEAR=2015]">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>