Гость
Форумы / XML, XSL, XPath, XQuery [игнор отключен] [закрыт для гостей] / XSL: Как отдельно обработать часть child узлов / 4 сообщений из 4, страница 1 из 1
22.11.2010, 12:50
    #36969428
svenom
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
XSL: Как отдельно обработать часть child узлов
Коллеги, возник следующий вопрос.

Есть документ вида
Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
<parent>
    <notToSort1> 
        ...
    </notToSort1>
    ...
    <notToSortN> 
        ...
    </notToSortN>
    <toSort>
        ...
        <property> 2 </property>
    <toSort>
    <toSort>
        ...
        <property> 3 </property>
    <toSort>
    <toSort>
        ...
        <property> 1 </property>
    <toSort>
</parent>

Мне нужно преобразовать его к виду:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
<parent>
    <notToSort1> 
        ...
    </notToSort1>
    ...
    <notToSortN> 
        ...
    </notToSortN>
    <toSort>
        ...
        <property> 1 </property>
    <toSort>
    <toSort>
        ...
        <property> 2 </property>
    <toSort>
    <toSort>
        ...
        <property> 3 </property>
    <toSort>
</parent>

То есть вся структура отсается такй же, мне нужно только отсортирвоать часть дочерних объектов по определенному свйоству. Не могу понять как это сделать.

Пока что у меня есть такой скрипт:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes" />

	<xsl:template match="@*">
		<xsl:attribute name="{local-name()}">
			<xsl:value-of select="." />
		</xsl:attribute>
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="*">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="@* | node()" />
			<xsl:apply-templates select="toSort">
				<xsl:sort select="property" order="ascending"/>
			</xsl:apply-templates>
		</xsl:element>
	</xsl:template>

	<xsl:template match="produktdaten">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="node()|@*" />
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>

Понятное дело, он выдает неправильный результат, так как сортируемые узлы дублируются:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
<parent>
    <notToSort1> 
        ...
    </notToSort1>
    ...
    <notToSortN> 
        ...
    </notToSortN>
    <toSort>
        ...
        <property> 2 </property>
    <toSort>
    <toSort>
        ...
        <property> 3 </property>
    <toSort>
    <toSort>
        ...
        <property> 1 </property>
    <toSort>
    <toSort>
        ...
        <property> 1 </property>
    <toSort>
    <toSort>
        ...
        <property> 2 </property>
    <toSort>
    <toSort>
        ...
        <property> 3 </property>
    <toSort>
</parent>

Каким образом можно подправить скрипт, что бы не появлялось дубликатов, то есть то, что =toSort обрабатывается одним образом, а то, что !=toSort - другим?

Спасибо.
...
Рейтинг: 0 / 0
22.11.2010, 12:51
    #36969437
svenom
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
XSL: Как отдельно обработать часть child узлов
Прошу прощения, текущий xsl скрипт таков:

Код: plaintext
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
<xsl:stylesheet version="1.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

	<xsl:output method="xml" indent="yes" />

	<xsl:template match="@*">
		<xsl:attribute name="{local-name()}">
			<xsl:value-of select="." />
		</xsl:attribute>
		<xsl:apply-templates />
	</xsl:template>

	<xsl:template match="*">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="@* | node()" />
			<xsl:apply-templates select="toSort">
				<xsl:sort select="property" order="ascending"/>
			</xsl:apply-templates>
		</xsl:element>
	</xsl:template>

	<xsl:template match="property">
		<xsl:element name="{local-name()}">
			<xsl:apply-templates select="node()|@*" />
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>
...
Рейтинг: 0 / 0
22.11.2010, 13:25
    #36969539
refreg
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
XSL: Как отдельно обработать часть child узлов
svenom,

Код: plaintext
1.
<xsl:apply-templates select="@* | node()[not(local-name()='toSort')]" />
<xsl:apply-templates select="node()[local-name()='toSort']">
...
Рейтинг: 0 / 0
22.11.2010, 13:43
    #36969596
svenom
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
XSL: Как отдельно обработать часть child узлов
Благодарю, именно то, что нужно.
...
Рейтинг: 0 / 0
Форумы / XML, XSL, XPath, XQuery [игнор отключен] [закрыт для гостей] / XSL: Как отдельно обработать часть child узлов / 4 сообщений из 4, страница 1 из 1
Целевая тема:
Создать новую тему:
Автор:
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]