0
votes

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>
3
Please show the xslt you tryied.Jean-François Savard
@Jean-FrançoisSavard I was actually not find any usable solution to my question, just this: //SHOPITEM[YEAR=2015]" -> I think this is the way to find nodes what I need.Adrian
Look up the "identity transformation" - that is the standard way to approach XSLT problems where you want to leave most of the XML alone and just change or remove specific parts of it.Ian Roberts

3 Answers

2
votes

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>
2
votes

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>
0
votes

You can nest predicates - try //SHOPITEM[YEAR[text() = 2015]]