Question

I have the following XML file:

<?xml version="1.0" encoding="UTF-8"?>
<gbXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.gbxml.org/schema xsi.xsd"xmlns="http://www.gbxml.org/schema"
 temperatureUnit="C" lengthUnit="Meters" areaUnit="SquareMeters"volumeUnit="CubicMeters"
useSIUnitsForResults="true" version="0.37">

<Pared id="Pa-1" paredType="Shade">     
<Name>P-S-1</Name>
<Order>
    <Direction>0.000000</Direction>
    <Angle>90.000000</Angle>
    <Height>3.657818</Height>
    <Width>15.200000</Width>
</Order>
</Pared>
<Pared id="Pa-2" paredType="Shade">     
    <Name>P-S-2</Name>
    <Order>
        <Direction>90.000000</Direction>
        <Angle>90.000000</Angle>
        <Height>2.598076</Height>
        <Width>14.200000</Width>
    </Order>
</Pared>
</gbXML>

I want to extract the "Height" and "Width" values of each "Pared id" and sort them out according to the "Direction" and "Angle" values. For example, in "Pared id= "Pa-1", if the "Direction"= 0.000000 AND the "Angle"= 90.000000, then the order of the extracted values of Width and Height should be:

(Width, 0.00)
(Width, Height)
(0.00, Height)
(0.00, 0.00)

I am new to XSLT and I know how to select the values of Width and Height with:

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

<xsl:template match= "gb:Pared">
<xsl:value-of select="gb:Order/gb:Height/>

But I do not know how to sort them manually like I mentioned before. I will really appreciate it if someone could help me with this.

I want the following Output (applying the conditions I mentioned before):

From Pared id="Pa-1:

(15.200000, 0.000000)
(15.200000, 3.657818)
(0.000000, 3.657818)
(0.000000, 0.000000)

From Pared id="Pa-2:

(0.000000, 0.000000)
(14.200000, 0.000000)
(14.200000, 2.598076)
(0.000000, 2.598076)

Think of it as wall coordinates X,Y. The two walls cannot have the same coordinates and the values of height and width act like the coordinate values. According to the "Direction" and the "Angle" of the wall is how those coordinates are paired. You have 0.000 coordinate values because of the 4 X, Y points that describe that wall. Therefore, Pa-1 and Pa-2 cannot have the same coordinates but they might be joined at one coordinate. Like if have two walls than form a L shape, 0.00, 0.00 can be the right bottom coordinate of Pa-1, but if the case of Pa-2, 0.00, 0.00 is the left bottom coordinate.

Was it helpful?

Solution

Not sure whether I understand correctly. The following XSL, outputs the width and height values, grouped by the Pared section, which is ordered by Direction then Angle (numeric, ascending).

Just be careful, in your XML example, I had to separate some of the gbXML's attributes with a space (otherwise it's not valid XML).

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
 xmlns:ns0="http://www.gbxml.org/schema">
    <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <xsl:for-each select='//ns0:Pared' >
            <xsl:sort select=".//ns0:Direction" data-type='number' order="ascending"/>
            <xsl:sort select=".//ns0:Angle"     data-type='number' order="ascending"/>
-- Pared: <xsl:value-of select="@id" />
(<xsl:value-of select=".//ns0:Width" />,0.00)
(<xsl:value-of select=".//ns0:Width" />,<xsl:value-of select=".//ns0:Height" />)
(0.00,<xsl:value-of select=".//ns0:Height" />)
(0.00,0.00)
</xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

Gives

-- Pared: Pa-1
(15.200000,0.00)
(15.200000,3.657818)
(0.00,3.657818)
(0.00,0.00)

-- Pared: Pa-2
(14.200000,0.00)
(14.200000,2.598076)
(0.00,2.598076)
(0.00,0.00)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top