0
我已經做了一個xslt腳本來在列表的末尾添加一個元素,如果它尚不存在,並且它工作正常。但是,我如何保持名單有序?XSLT:如何在一個有序列表中添加一個元素
即腳本:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p">
<xsl:output encoding="UTF-8" omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:param name="ident"></xsl:param>
<xsl:template match="beans:bean[@id='messagePool']/beans:property[@name='queueNames']/util:list">
<xsl:copy>
<xsl:copy-of select="@*|node()"/>
<xsl:if test="not(beans:value[text() = concat('QL.', $ident, '.0')])">
<value>QL.<xsl:value-of select="$ident"/>.0</value>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
這是數據文件:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">
<bean id="messagePool" class="com.example.QueueConfig"
p:recoveryInterval="1">
<property name="queueNames">
<util:list>
<value>QL.00000001.0</value>
<value>QL.00000002.0</value>
<value>QL.00000040.0</value>
<value>QL.00000045.0</value>
</util:list>
</property>
</bean>
</beans>
生成的XML是:
<bean id="messagePool" class="com.example.QueueConfig" p:recoveryInterval="1">
<property name="queueNames">
<util:list>
<value>QL.00000001.0</value>
<value>QL.00000002.0</value>
<value>QL.00000040.0</value>
<value>QL.00000045.0</value>
<value xmlns="">QL.00000003.0</value>
</util:list>
</property>
</bean>
與QL.00000003.0該元件應放置在QL.00000002.0之後。
作爲一個額外的問題:我怎樣才能擺脫虛假的xmlns =「」?
謝謝,它的工作!我試圖做類似的事情之前,但得到有關片段的錯誤。訣竅是使用節點集。 – Eduardo