Reputation: 2656
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>
Upvotes: 0
Views: 3264
Reputation: 116959
Would this work for you?
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/SHOP">
<xsl:copy>
<xsl:copy-of select="SHOPITEM[YEAR=2015]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2
Reputation: 101652
As Ian Roberts says, you should start with the identity transform, and customize from there. This is all you need:
<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="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Omit any shop item without YEAR = 2015 -->
<xsl:template match="SHOPITEM[not(YEAR = 2015)]" />
</xsl:stylesheet>
When run on your sample input, the result is:
<SHOP>
<SHOPITEM>
<unknown_element1/>
<unknown_element2/> ...
<YEAR>2015</YEAR>
<unknown_elementxy/> ...
</SHOPITEM>
<SHOPITEM>
<unknown_element1/>
<unknown_element2/> ...
<YEAR>2015</YEAR>
<unknown_elementxy/> ...
</SHOPITEM>
</SHOP>
Upvotes: 2