|
| sorting nodes |
 |
Thu, 3 Apr 2008 16:25:11 -0700 |
I can't seem to get my results to be formatted.
I have the following xml:
<?xml version="1.0" encoding="utf-8"?>
<Report>
<data>
<form name="order" primary="false">
<tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
</form>
<form name="title" primary="false">
<tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
85374-2525</tag>
</form>
<form name="1004_05" primary="true">
<section type="options" number="0">
<tag name="OPT_TYPE_OF_APPRAISAL.1" flags="1"
format="12288">Summary
Appraisal Report</tag>
</section>
<section type="subject" number="0">
<tag name="CITY.1" flags="0"
format="0">Surprise</tag>
</section>
<section type="salescomp" number="1">
<tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
</section>
<section type="salescomp" number="2">
<tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
</section>
</form>
</data>
</Report>
I also have the following xslt file:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="xsi">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="data/form/tag |
data/form/section/tag">
<xsl:sort select="form/@primary"
order="ascending"/>
</xsl:apply-templates>/>
</xsl:copy>
</xsl:template>
<xsl:template match="tag">
<form>
<sectionNumber>
<xsl:value-of select="ancestor::section/@number"/>
</sectionNumber>
<primary>
<xsl:value-of select="ancestor::form/@primary"/>
</primary>
<formName>
<xsl:value-of select="ancestor::form/@name"/>
</formName>
<tagName>
<xsl:value-of select="@name"/>
</tagName>
<flags>
<xsl:if test="not(@flags)">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="@flags"/>
</flags>
<format>
<xsl:if test="not(@format)">
<xsl:attribute name="xsi:nil">true</xsl:attribute>
</xsl:if>
<xsl:value-of select="@format"/>
</format>
<value>
<xsl:value-of select="."/>
</value>
</form>
</xsl:template>
</xsl:stylesheet>
My sort doesn't seem to work. Regardless, whether I use ascending or
descending, I get:
<?xml version="1.0" encoding="utf-8"?>
<Report>
<data>
<form name="order" primary="false">
<tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
</form>
<form name="title" primary="false">
<tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
85374-2525</tag>
</form>
<form name="1004_05" primary="true">
<section type="options" number="0">
<tag name="OPT_TYPE_OF_APPRAISAL.1" flags="1"
format="12288">Summary
Appraisal Report</tag>
</section>
<section type="subject" number="0">
<tag name="CITY.1" flags="0"
format="0">Surprise</tag>
</section>
<section type="salescomp" number="1">
<tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
</section>
<section type="salescomp" number="2">
<tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
</section>
</form>
</data>
</Report>
It is in the order of the original file. Why is that?
Thanks,
Tom
|
| Post Reply
|
| Re: sorting nodes |
 |
Fri, 4 Apr 2008 07:46:21 -0700 |
"Anthony Jones" <Ant@yadayadayada.com> wrote in message
news:%23i3k2jilIHA.3512@TK2MSFTNGP03.phx.gbl...
> "tshad" <tshad@dslextreme.com> wrote in message
> news:%23NAEdJelIHA.3940@TK2MSFTNGP05.phx.gbl...
>> I can't seem to get my results to be formatted.
>>
>> I have the following xml:
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <Report>
>> <data>
>> <form name="order" primary="false">
>> <tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
>> </form>
>> <form name="title" primary="false">
>> <tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
>> 85374-2525</tag>
>> </form>
>> <form name="1004_05" primary="true">
>> <section type="options" number="0">
>> <tag name="OPT_TYPE_OF_APPRAISAL.1"
flags="1" format="12288">Summary
>> Appraisal Report</tag>
>> </section>
>> <section type="subject" number="0">
>> <tag name="CITY.1" flags="0"
format="0">Surprise</tag>
>> </section>
>> <section type="salescomp" number="1">
>> <tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
>> </section>
>> <section type="salescomp" number="2">
>> <tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
>> </section>
>> </form>
>> </data>
>> </Report>
>>
>> I also have the following xslt file:
>>
>> <xsl:stylesheet version="1.0"
>> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> exclude-result-prefixes="xsi">
>> <xsl:output method="xml" indent="yes"/>
>> <xsl:template match="/*">
>> <xsl:copy>
>> <xsl:apply-templates select="data/form/tag |
data/form/section/tag">
>> <xsl:sort select="form/@primary"
order="ascending"/>
>> </xsl:apply-templates>/>
>> </xsl:copy>
>> </xsl:template>
>> <xsl:template match="tag">
>> <form>
>> <sectionNumber>
>> <xsl:value-of select="ancestor::section/@number"/>
>> </sectionNumber>
>> <primary>
>> <xsl:value-of select="ancestor::form/@primary"/>
>> </primary>
>> <formName>
>> <xsl:value-of select="ancestor::form/@name"/>
>> </formName>
>> <tagName>
>> <xsl:value-of select="@name"/>
>> </tagName>
>> <flags>
>> <xsl:if test="not(@flags)">
>> <xsl:attribute
name="xsi:nil">true</xsl:attribute>
>> </xsl:if>
>> <xsl:value-of select="@flags"/>
>> </flags>
>> <format>
>> <xsl:if test="not(@format)">
>> <xsl:attribute
name="xsi:nil">true</xsl:attribute>
>> </xsl:if>
>> <xsl:value-of select="@format"/>
>> </format>
>> <value>
>> <xsl:value-of select="."/>
>> </value>
>> </form>
>> </xsl:template>
>> </xsl:stylesheet>
>>
>> My sort doesn't seem to work. Regardless, whether I use ascending or
>> descending, I get:
>>
>> <?xml version="1.0" encoding="utf-8"?>
>> <Report>
>> <data>
>> <form name="order" primary="false">
>> <tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
>> </form>
>> <form name="title" primary="false">
>> <tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
>> 85374-2525</tag>
>> </form>
>> <form name="1004_05" primary="true">
>> <section type="options" number="0">
>> <tag name="OPT_TYPE_OF_APPRAISAL.1"
flags="1" format="12288">Summary
>> Appraisal Report</tag>
>> </section>
>> <section type="subject" number="0">
>> <tag name="CITY.1" flags="0"
format="0">Surprise</tag>
>> </section>
>> <section type="salescomp" number="1">
>> <tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
>> </section>
>> <section type="salescomp" number="2">
>> <tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
>> </section>
>> </form>
>> </data>
>> </Report>
>>
>> It is in the order of the original file. Why is that?
>>
>
> The context nodes for the sort path will be the nodes selected in the
> apply-templates select, IOW they will all be tag elements. You need:-
>
> <xsl:sort path="ancestor::form/@primary"
>
Can you do 2 sorts?
Sort on @primary and on ancestor::section/@number?
Thanks,
Tom
>
> --
> Anthony Jones - MVP ASP/ASP.NET
>
>
|
| Post Reply
|
| Re: sorting nodes |
 |
Fri, 4 Apr 2008 08:53:23 +0100 |
"tshad" <tshad@dslextreme.com> wrote in message
news:%23NAEdJelIHA.3940@TK2MSFTNGP05.phx.gbl...
> I can't seem to get my results to be formatted.
>
> I have the following xml:
>
> <?xml version="1.0" encoding="utf-8"?>
> <Report>
> <data>
> <form name="order" primary="false">
> <tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
> </form>
> <form name="title" primary="false">
> <tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
> 85374-2525</tag>
> </form>
> <form name="1004_05" primary="true">
> <section type="options" number="0">
> <tag name="OPT_TYPE_OF_APPRAISAL.1" flags="1"
format="12288">Summary
> Appraisal Report</tag>
> </section>
> <section type="subject" number="0">
> <tag name="CITY.1" flags="0"
format="0">Surprise</tag>
> </section>
> <section type="salescomp" number="1">
> <tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
> </section>
> <section type="salescomp" number="2">
> <tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
> </section>
> </form>
> </data>
> </Report>
>
> I also have the following xslt file:
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> exclude-result-prefixes="xsi">
> <xsl:output method="xml" indent="yes"/>
> <xsl:template match="/*">
> <xsl:copy>
> <xsl:apply-templates select="data/form/tag |
data/form/section/tag">
> <xsl:sort select="form/@primary"
order="ascending"/>
> </xsl:apply-templates>/>
> </xsl:copy>
> </xsl:template>
> <xsl:template match="tag">
> <form>
> <sectionNumber>
> <xsl:value-of select="ancestor::section/@number"/>
> </sectionNumber>
> <primary>
> <xsl:value-of select="ancestor::form/@primary"/>
> </primary>
> <formName>
> <xsl:value-of select="ancestor::form/@name"/>
> </formName>
> <tagName>
> <xsl:value-of select="@name"/>
> </tagName>
> <flags>
> <xsl:if test="not(@flags)">
> <xsl:attribute
name="xsi:nil">true</xsl:attribute>
> </xsl:if>
> <xsl:value-of select="@flags"/>
> </flags>
> <format>
> <xsl:if test="not(@format)">
> <xsl:attribute
name="xsi:nil">true</xsl:attribute>
> </xsl:if>
> <xsl:value-of select="@format"/>
> </format>
> <value>
> <xsl:value-of select="."/>
> </value>
> </form>
> </xsl:template>
> </xsl:stylesheet>
>
> My sort doesn't seem to work. Regardless, whether I use ascending or
> descending, I get:
>
> <?xml version="1.0" encoding="utf-8"?>
> <Report>
> <data>
> <form name="order" primary="false">
> <tag name="DUEDATE" flags="1"
format="4096">12/01/2007</tag>
> </form>
> <form name="title" primary="false">
> <tag name="CITY_STATE_ZIP.1" flags="0"
format="12288">Surprise, AZ
> 85374-2525</tag>
> </form>
> <form name="1004_05" primary="true">
> <section type="options" number="0">
> <tag name="OPT_TYPE_OF_APPRAISAL.1" flags="1"
format="12288">Summary
> Appraisal Report</tag>
> </section>
> <section type="subject" number="0">
> <tag name="CITY.1" flags="0"
format="0">Surprise</tag>
> </section>
> <section type="salescomp" number="1">
> <tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
> </section>
> <section type="salescomp" number="2">
> <tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
> </section>
> </form>
> </data>
> </Report>
>
> It is in the order of the original file. Why is that?
>
The context nodes for the sort path will be the nodes selected in the
apply-templates select, IOW they will all be tag elements. You need:-
<xsl:sort path="ancestor::form/@primary"
--
Anthony Jones - MVP ASP/ASP.NET
|
| Post Reply
|
| Re: sorting nodes |
 |
Fri, 4 Apr 2008 09:54:13 -0700 |
"Martin Honnen" <mahotrash@yahoo.de> wrote in message
news:%23fldSZmlIHA.1744@TK2MSFTNGP05.phx.gbl...
> tshad wrote:
>
>>>> <xsl:apply-templates select="data/form/tag |
data/form/section/tag">
>>>> <xsl:sort select="form/@primary"
order="ascending"/>
>>>> </xsl:apply-templates>/>
>
>
>> Can you do 2 sorts?
>>
>> Sort on @primary and on ancestor::section/@number?
>
> Yes, you simply need to use two xsl:sort elements e.g.
> <xsl:apply-templates select="...">
> <xsl:sort select="@primary"/>
> <xsl:sort select="ancestor::section/@number"/>
> </xsl:apply-templates>
> that way the first xsl:sort defines the primary sort key, the second
> xsl:sort the secondary sort key.
>
>
Worked great.
I assume that the elements/nodes are selected first based on the
"select=",
then the sort is applied to those, then the templates are applied.
At least that is how it seems to work.
Also, during the sort, all the nodes that don't apply will be blank and are
considered the lowest.
In this example:
<form name="1004_05" primary="true">
<section type="options" number="0">
<tag name="OPT_TYPE_OF_APPRAISAL.1" flags="1"
format="12288">Summary Appraisal Report</tag>
</section>
<tag name="CITY.1" flags="0"
format="0">Surprise</tag>
<-- no section
<tag name="STATE.1" flags="0"
format="0">AZ</tag>
<-- no section
<tag name="COUNTY.1" flags="0"
format="0">Maricopa</tag>
<-- no section
<section type="salescomp" number="1">
<tag name="GS_AGE.1" flags="0"
format="0">9 Yrs.</tag>
<tag name="GS_SITE.1" flags="0"
format="0">6,100 SqFt</tag>
<tag name="GS_VIEW.1" flags="1"
format="0">Average</tag>
</section>
<section type="salescomp" number="2">
<tag name="GS_AGE.1" flags="0"
format="0">12 Yrs.</tag>
<tag name="GS_SITE.1" flags="0"
format="0">4,840 SqFt</tag>
<tag name="GS_VIEW.1" flags="1"
format="0">Good</tag>
</section>
</form>
And applying this:
<xsl:apply-templates select="appraisal/data/form/tag |
appraisal/data/form/section/tag">
<xsl:sort select="ancestor::form/@primary"
order="ascending"/>
<xsl:sort select="ancestor::section/@number"
order="descending"
data-type="number"/>
</xsl:apply-templates>
The 3 nodes that have no section, will be at the bottom and if I change the
order of the @number to ascending, they will be at the top.
Thanks,
Tom
>
> --
>
> Martin Honnen --- MVP XML
> http://JavaScript.FAQTs.com/
|
| Post Reply
|
| Re: sorting nodes |
 |
Fri, 04 Apr 2008 17:12:35 +020 |
tshad wrote:
>>> <xsl:apply-templates select="data/form/tag |
data/form/section/tag">
>>> <xsl:sort select="form/@primary"
order="ascending"/>
>>> </xsl:apply-templates>/>
> Can you do 2 sorts?
>
> Sort on @primary and on ancestor::section/@number?
Yes, you simply need to use two xsl:sort elements e.g.
<xsl:apply-templates select="...">
<xsl:sort select="@primary"/>
<xsl:sort select="ancestor::section/@number"/>
</xsl:apply-templates>
that way the first xsl:sort defines the primary sort key, the second
xsl:sort the secondary sort key.
--
Martin Honnen --- MVP XML
|
| Post Reply
|
|
|
|
|
|
|
|
|
|