Multiply ancestors in xsl

  • Thread starter Martin Pettersson
  • Start date
M

Martin Pettersson

Hi all,

I'm trying to multiply parent values in xsl. The thing is that I start
with a value down in the xml-structure. From that value (in my case
'qty' value) I check the parent value and later on I will try to
multiply these values. When I have found the parent I will multiply
with its parent and so on, all the way to the top of the xml-file.

The files are pasted below. First I have tried to find these values,
the next step will be to start the calculation

Has anyone worked with a similar case? How can I find the 'qty' value
for hte parents? Maybe there could be a better way to work with these
kind of calculations in xsl (variables or functions ...).

I will really appreciate all kind of feedback on this.

All the best,
Martin


XML:

<?xml-stylesheet type="text/xsl" href="C:\xml-xsl\realized-by.xsl"?>
<state>
<part name="part_5990157" no="1">
<attributes>
<attribute name="qty">1</attribute>
</attributes>
<subparts>
<part name="support_beam_horizontal_part" no="1">
<attributes>
<attribute name="qty">1</attribute>
</attributes>
<subparts>
<part name="endcap_part" no="1">
<attributes>
<attribute name="qty">4</attribute>
</attributes>
<subparts/>
<realized-by>
<name>a_xcbe_44x88</name>
<desc>XCBE 44X88</desc>
</realized-by>
</part>
<part name="support_bracket_part" no="1">
<attributes>
<attribute name="qty">6</attribute>
</attributes>
<subparts>
<part name="nut_1_part" no="1">
<attributes>
<attribute name="qty">4</attribute>
</attributes>
<subparts/>
<realized-by>
<name>a_xlaq_8</name>
<desc>XLAQ 8</desc>
</realized-by>
</part>
<part name="bolt_2_part" no="1">
<attributes>
<attribute name="qty">3</attribute>
</attributes>
<subparts/>
<realized-by>
<name>a_m6s_8x16</name>
<desc>M6S 8X16</desc>
</realized-by>
</part>
</subparts>
<realized-by>
<name>a_xlct_21x158_r</name>
<desc>XLCT 21X158 R </desc>
</realized-by>
</part>
</subparts>
<realized-by>
<name>a_xcbl_lx88</name>
<desc>XCBL LX88</desc>
</realized-by>
</part>
</subparts>
<realized-by>
<name>_5990133</name>
<desc>XCUF T02X88 A</desc>
</realized-by>
</part>
</state>


XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<html>
<body>
<table border="2" bordercolor="black">
<tr>
<td>Qty</td>
<td>Parent Qty</td>
<td>No of ancestors</td>
</tr>
<xsl:for-each select="//part">
<xsl:for-each select="*/*/realized-by">
<xsl:if test="not(contains(../@name,'dummy'))">
<tr>
<td>
<xsl:value-of select="../*/*[@name='qty']"/>
</td>
<td>
<xsl:value-of select="parent::part/*/*[@name='qty']"/>
</td>
<td>
<xsl:value-of select="count(ancestor::*/@name)"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:for-each>
<!-- Parts - end -->
<tr>
<td height="10" colspan="4"/>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
J

Joris Gillis

I'm trying to multiply parent values in xsl. The thing is that I start
with a value down in the xml-structure. From that value (in my case
'qty' value) I check the parent value and later on I will try to
multiply these values. When I have found the parent I will multiply
with its parent and so on, all the way to the top of the xml-file.
Has anyone worked with a similar case? How can I find the 'qty' value
for hte parents? Maybe there could be a better way to work with these
kind of calculations in xsl (variables or functions ...).

Hi,

The following method uses variables and tree-node variables.

To multiple all ancestor's 'qty' attributes, use:

<xsl:call-template name="multiply">
<xsl:with-param name="values" select="ancestor::part/*/*[@name='qty']"/>
</xsl:call-template>

This template multiplies all values found in the 'values' parameter.
It must be included in the XSL.

<xsl:template name="multiply">
<xsl:param name="values"/>
<xsl:param name="product" select="1"/>
<xsl:if test="count($values) &gt; 0">
<xsl:call-template name="multiply">
<xsl:with-param name="product" select="$product * $values[1]"/>
<xsl:with-param name="values" select="$values[position() &gt; 1]"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="count($values)=0">
<xsl:value-of select="$product"/>
</xsl:if>
</xsl:template>

Is this useful?

Also a little question about your document structure:
Why not use real attributes in stead of nodes named 'attribute'?
I mean something like : <attributes @qty="4"/>


Joris Gillis
 
M

Martin Pettersson

Joris Gillis said:
I'm trying to multiply parent values in xsl. The thing is that I start
with a value down in the xml-structure. From that value (in my case
'qty' value) I check the parent value and later on I will try to
multiply these values. When I have found the parent I will multiply
with its parent and so on, all the way to the top of the xml-file.
Has anyone worked with a similar case? How can I find the 'qty' value
for hte parents? Maybe there could be a better way to work with these
kind of calculations in xsl (variables or functions ...).

Hi,

The following method uses variables and tree-node variables.

To multiple all ancestor's 'qty' attributes, use:

<xsl:call-template name="multiply">
<xsl:with-param name="values" select="ancestor::part/*/*[@name='qty']"/>
</xsl:call-template>

This template multiplies all values found in the 'values' parameter.
It must be included in the XSL.

<xsl:template name="multiply">
<xsl:param name="values"/>
<xsl:param name="product" select="1"/>
<xsl:if test="count($values) &gt; 0">
<xsl:call-template name="multiply">
<xsl:with-param name="product" select="$product * $values[1]"/>
<xsl:with-param name="values" select="$values[position() &gt; 1]"/>
</xsl:call-template>
</xsl:if>
<xsl:if test="count($values)=0">
<xsl:value-of select="$product"/>
</xsl:if>
</xsl:template>

Is this useful?

Also a little question about your document structure:
Why not use real attributes in stead of nodes named 'attribute'?
I mean something like : <attributes @qty="4"/>


Joris Gillis

Hi Joris,

Finally I tried to incorporate your template with my code. Perfect, it
works fine. Thank you.

I share your view of how the document is structured. Not much I can do
about it =).

Martin Pettersson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top