0

Friends,

I am new to XSLT. Please help to get the below logic. I am using unix machine

From the below 3 input xml file, need to add 'count' field value by using xslt and need to generate output with added value in count field

Input XML 1

<topic>
<topicName>Billing</topicName>
<property>
<make>HONDA</make>
<Model>ACCORD</Model>
</property>
<count>10</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>TOYOTO</make>
<Model>CAMRY</Model>
</property>
<count>20</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>NISSAN</make>
<Model>ALTIMA</Model>
</property>
<count>30</count>
</topic>

Input XML 2

<topic>
<topicName>Billing</topicName>
<property>
<make>HONDA</make>
<Model>ACCORD</Model>
</property>
<count>100</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>TOYOTO</make>
<Model>CAMRY</Model>
</property>
<count>200</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>NISSAN</make>
<Model>ALTIMA</Model>
</property>
<count>300</count>
</topic>

Input XML 3

<topic>
<topicName>Billing</topicName>
<property>
<make>HONDA</make>
<Model>ACCORD</Model>
</property>
<count>1000</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>TOYOTO</make>
<Model>CAMRY</Model>
</property>
<count>2000</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>NISSAN</make>
<Model>ALTIMA</Model>
</property>
<count>3000</count>
</topic>

From the above 3 input xml file, need to add count value by using xslt and need to generate output with added value for count field

OUTPUT XML:

<topic>
<topicName>Billing</topicName>
<property>
<make>HONDA</make>
<Model>ACCORD</Model>
</property>
<count>1110</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>TOYOTO</make>
<Model>CAMRY</Model>
</property>
<count>2220</count>
</topic>
<topic>
<topicName>Billing</topicName>
<property>
<make>NISSAN</make>
<Model>ALTIMA</Model>
</property>
<count>3330</count>
</topic>

Thanks in Advance

1
  • How do you identify topics in the different files to be grouped together, by make and Model? Commented Apr 28, 2016 at 20:55

2 Answers 2

1

Read up on the document() function, and (for 1.1) the doc() function.

In 1.1, read up on xsl:for-each-group and deep-equal().

In both 1.0 and 1.1, read up on sum().

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

Comments

0

You can read in files using collection (the syntax used in the sample is Saxon 9 specific):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output indent="yes"/>

    <xsl:param name="file-pattern" select="'topic*.xml'"/>

    <xsl:template name="main">
        <xsl:for-each-group select="collection(concat('.?select=', $file-pattern))//topic" group-by="concat(property/make, '|', property/Model)">
            <xsl:copy>
                <xsl:copy-of select="topicName, property"/>
                <count>
                    <xsl:value-of select="sum(current-group()/count)"/>
                </count>
            </xsl:copy>
        </xsl:for-each-group>
    </xsl:template>

</xsl:stylesheet>

Run Saxon 9 using -it:main to start with that template called main.

5 Comments

Thanks for your input. While executing xslt, got this error .... "javax.xml.transform.TransformerException: java.lang.RuntimeException: Unsupported XSL element 'w3.org/1999/XSL/Transform:for-each-group' "
You need need to download and use Saxon 9 to be able to use XSLT 2.0.
Saxon 9 is available in an open source version from saxon.sourceforge.net/#F9.7HE.
Thanks for your information. Currently I am not able to install new tool in development unix machine because of Management policy in my concern. So please guide me how to achieve this (reading data from multiple input) in other way.
Well, the question is tagged as XSLT 2.0 so you should have access to XSLT 2.0. As for reading multiple files in XSLT 1.0, see the other answer with the hint to the document function w3.org/TR/xslt#document. Grouping with multiple files in XSLT 1.0 is more difficult, as the usual Muenchian grouping approach using a key is specific to grouping nodes from one document. So you might need to create a result tree fragment first which merges the nodes from the different documents, then group the result tree fragment converted to a node-set with exsl:node-set.

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.