I have two different XML documents below:
XML 1
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Match>
<Trades>
<Trade fruitId="apples"/>
</Trades>
</Match>
XML 2
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Match2>
<Properties>
<foobar>123</foobar>
</Properties>
<Trades>
<Trade vegetableId="eggplant"/>
</Trades>
</Match2>
I would like to create one XSLT document which would basically take the value inside of fruitId
, or vegetableId
, depending on which one is available. The XSLT should satisfy both XML1 and XML2.
I am not sure how to approach this, whether or not i should create an if-statement that checks to see if this Trades tag contains a vegetableId, or FruitId. I am a bit lost here. This is what i came up with so far.
MY XSLT ATTEMPT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="Trades/Trade">
<xsl: if test = "(contains(@fruitId, 'somevalue'))">
<xsl:value-of select="@fruitId"/>
</xsl:if>
<xsl: if test = "(contains(@vegetableId, 'somevalue'))">
<xsl:value-of select="@vegetableId"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
I know that my contains statement has 'somevalue' inside, this is not the acutal code i am running, rather its an idea of what im thinking of trying out, although i am not sure how to approach.
Also, <Match>
is different than <Match2>
.
Please advise. Thank you!