I have an XSLT file and I need to generate the corresponding XML file. I do not know exactly how the XML file is supposed to look like however based on the XSLT, we can guess how it should be.
I need help to understand XSLT properly because I am new to it.
How would the XML file for the below XSLT look like?
<xsl:stylesheet version="1.0" exclude-result-prefixes="xsl math exsl">
<xsl:template match="/library">
<all>
<xsl:if test="count(//cupboards) > 100">
<xsl:message terminate="yes">Too many cupboards</xsl:message>
</xsl:if>
<xsl:variable name="val_a">
<value>
<xsl:value-of select="round(math:random() * 10000)"/>
</value>
<value>
<xsl:value-of select="round(math:random() * 10000)"/>
</value>
</value>
</xsl:variable>
<xsl:call-template name="check-library">
<xsl:with-param name="val_a" select="exsl:node-set($val_a)//value"/>
<xsl:with-param name="val_b" select="1"/>
<xsl:with-param name="val_c" select="val_c[position() = 1]/cupboards"/>
<xsl:with-param name="val_d" select="state/val_d"/>
</xsl:call-template>
</all>
</xsl:template>
<xsl:template name="check-library">
<xsl:param name="val_a"/>
<xsl:param name="val_b"/>
<xsl:param name="val_c"/>
<xsl:param name="val_d"/>
<xsl:if test="$val_b > 20000">
<xsl:message terminate="yes">Check 1</xsl:message>
</xsl:if>
<xsl:if test="count($val_d) > 200">
<xsl:message terminate="yes">
Check 2
</xsl:message>
</xsl:if>
<xsl:if test="count($val_c) > 0">
<xsl:variable name="c" select="$val_c[1]"/>
<xsl:variable name="r" select="$val_c[position()>1]"/>
<xsl:choose>
<xsl:when test="count($c/something) = 1">
<xsl:message>
<val_a>
<xsl:copy-of select="$val_a"/>
</val_a>
</xsl:message>
<xsl:message>
<val_d>
<xsl:copy-of select="$val_d"/>
</val_d>
</xsl:message>
<xsl:call-template name="check-library">
<xsl:with-param name="val_a" select="$val_a"/>
<xsl:with-param name="val_b" select="$val_b + 1"/>
<xsl:with-param name="val_c" select="$r"/>
<xsl:with-param name="val_d" select="$val_d"/>
</xsl:call-template>
<xsl:when test="count($c/something2) = 1">
<xsl:variable name="val_e">
<value>
<xsl:value-of select="$c/something2 + 0"/>
</value><xsl:copy-of select="$val_d"/>
</xsl:variable>
<xsl:call-template name="check-library">
<xsl:with-param name="val_a" select="$val_a"/>
<xsl:with-param name="val_b" select="$val_b + 1"/>
<xsl:with-param name="val_c" select="$r"/>
<xsl:with-param name="val_d" select="exsl:node-set($val_e)//value"/>
</xsl:call-template>
</xsl:when>
I tried to make an XML as shown below:
<?xml version="1.0" encoding="UTF-8"?>
<library>
<val_c>
<cupboards>
<something>
</something>
</cupboards>
</val_c>
</library>
<state>
<val_d>
something
</val_d>
<state>
However, it is not the correct XML.
If I put more than 100 cupboard child nodes for library tree node, I get the message: "Too many cupboards". So, that part is correct.
But I don't understand how the checks are being performed in the check-library template.
Could someone help me understand this XSLT and also show how the corresponding XML should look like?
Then, I can write code to automatically generate the XML for a given XSLT of this form.
Thanks.
check-libraryrecursively. Anything it tries to match that isn't also defined within the templates, it would be expecting to find in the XML.Socupboard,state, and an element calledsomethingare part of the XML. (You've got most of those, but notstate).