0

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.

12
  • So, given an XSLT "of this form", you need to deduce what XML it's expecting to run against? Programmatically? Commented Dec 28, 2018 at 23:59
  • I don't know what problem this will solve (and I'd be curious to know), but this doesn't sound like a trivial task. What language do you plan to write the XML-deducer in? Commented Dec 29, 2018 at 0:00
  • I can tell you a few things about the XSLT, if it'll help. It's calling check-library recursively. Anything it tries to match that isn't also defined within the templates, it would be expecting to find in the XML.So cupboard, state, and an element called something are part of the XML. (You've got most of those, but not state). Commented Dec 29, 2018 at 0:03
  • Is there a schema available for the XML? That will help you a LOT more than the XSLT run against it. Commented Dec 29, 2018 at 0:03
  • @Ann.L. Yes, you are correct. And I am using xalan to input my XML and run it against the XSLT file. Also, xalan has some useful features like "-ttc" command line option which allows me to trace the processing of the XSLT file. You are correct about the recursive call. I have updated the XML now. And yes, I need to deduce the XML. It doesn't have to be deduced programmatically right now. I want to understand it better and then I can generate it accordingly. Commented Dec 29, 2018 at 0:22

1 Answer 1

2

This is a completely hopeless task, especially if you are concealing parts of the XSLT code from us for privacy reasons, and if the XSLT code is clearly incorrect (an xsl:when element immediately after the closing </xsl:stylesheet>???)

It's certainly not a task that could ever be automated.

The best you can hope to do is to work out some of the structure: the stylesheet is expecting a root element called library, descendant elements called cupboard, and child elements called val_c and state, and other element called something and something2. If the names were more helpful (e.g. if it were "city" and "state" rather than "val_c" and "state") then you might be able to make some guesses based on the choice of names. Those guesses might of course be completely wrong.

Because the stylesheet doesn't use xsl:apply-templates, it's possible to do a bit of data-flow analysis and work out, for example, that the random values assigned in the root template are never actually used (at least not, in the part of the code you've chosen to show us). But that kind of analysis is not usually going to be possible with XSLT, because most stylesheets do use xsl:apply-templates, and you can't do any static analysis of template rule invocation without knowing a lot about the source document structure.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.