Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
707 views
in Technique[技术] by (71.8m points)

xslt - Need help to create an XSL file that will transform the input XML file input.xml to the format of output.xml

I need to:

  1. Transform the input.xml (file below) to the format of output.xml
  2. Create another XLS that will display per each amount: KG, PAL and M3 the total values;
  3. Replace the tag <quantity> with the tag <amount> in the same XSL from point 2;
  4. create another XSL which is just adding an extra tag <hour> for each <order> and display inside this tag the hour of loading (hh:MM)

But at first I need to convert this file (input.xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
<orders>
    <order>
        <id>1</id>
        <number>10002</number>
        <type>Loading</type>
        <date>2013-01-01T02:30:00</date>
    </order>
    <order>
        <id>2</id>
        <number>10003</number>
        <type>Loading</type>
        <date>2013-01-01T010:30:00</date>
    </order>
    <order>
        <id>3</id>
        <number>10004</number>
        <type>Loaded</type>
        <date>2013-01-01T12:30:00</date>
    </order>
</orders>
<quantities>
    <quantity>
        <id_order>1</id_order>
        <unit>KG</unit>
        <value>1000</value>
    </quantity>
    <quantity>
        <id_order>1</id_order>
        <unit>PAL</unit>
        <value>3</value>
    </quantity>
    <quantity>
        <id_order>1</id_order>
        <unit>M3</unit>
        <value>1.5</value>
    </quantity>
    <quantity>
        <id_order>2</id_order>
        <unit>KG</unit>
        <value>2000</value>
    </quantity>
    <quantity>
        <id_order>2</id_order>
        <unit>PAL</unit>
        <value>4</value>
    </quantity>
    <quantity>
        <id_order>3</id_order>
        <unit>KG</unit>
        <value>5000</value> 
    </quantity>
</quantities>
</output>

To this file (output.xml):

<?xml version="1.0" encoding="ISO-8859-1"?>
<output>
<orders>
    <order>
        <id>1</id>
        <number>10002</number>
        <type>Loading</type>
        <KG>1000</KG>
        <PAL>3</PAL>
        <M3>1.5</M3>
    </order>
    <order>
        <id>2</id>
        <number>10003</number>
        <type>Loading</type>
        <KG>2000</KG>
        <PAL>4</PAL>
    </order>
    <order>
        <id>3</id>
        <number>10004</number>
        <type>Loaded</type>
        <KG>5000</KG>
    </order>
</orders>
</output>

I tried this one but it is not complete, I don't know how to convert values (like: PAL in input file) to atributes (like <PAL>...</PAL> in output file). I have looked around and tried few things but it is not working. Can someone help me with this. Below is my XSLT. Thanks in advance.

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>

<xsl: template match = "/">
    <output>
            <xsl: for-each select = "orders/order[id = 1]">
            <order>
                    <xsl: value-of select = "id"/>
                    <xsl: value-of select = "number"/>
                    <xsl: value-of select = "type"/>
            </order>
            </xsl: for-each>
            <xsl: for-each select = "quantities/quantity">
            <order>
                    <xsl: value-of select = "id"/>
                    <xsl: value-of select = "number"/>
                    <xsl: value-of select = "type"/>
            </order>
            </xsl: for-each>
    </output>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to link the quantities to their respective orders by matching the order id. The best way to do this is by using a key:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<xsl:key name="qty" match="quantity" use="id_order"/>  

<xsl:template match="/">
<output>
    <orders>
        <xsl:for-each select="output/orders/order"> 
            <order>
                <xsl:copy-of select="id|number|type"/>
                <xsl:for-each select="key('qty', id)">
                    <xsl:element name="{unit}">
                        <xsl:value-of select="value"/>  
                    </xsl:element>
                </xsl:for-each>
            </order>    
        </xsl:for-each>
    </orders>
</output>
</xsl:template>

</xsl:stylesheet>

Note that we are assuming that each order has no more than one quantity of each unit (i.e no summing is required). Also it seems to me that the target structure is less useful than what you're starting with.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...