2015-10-08 19 views
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 =「」?

回答

2

使用XSLT 1.0,你可以先創建一個結果樹片段,並在最後添加的值,然後將其轉換爲與設置節點exsl:node-set(或類似的,這取決於你的XSLT處理器),並按照順序的過程吧:

<xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns="http://www.springframework.org/schema/beans" 
       xmlns:util="http://www.springframework.org/schema/util" 
       xmlns:beans="http://www.springframework.org/schema/beans" 
       xmlns:p="http://www.springframework.org/schema/p" 
       xmlns:exsl="http://exslt.org/common" 
       exclude-result-prefixes="exsl beans"> 

    <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">00000003</xsl:param> 

    <xsl:template match="beans:bean[@id='messagePool']/beans:property[@name='queueNames']/util:list"> 
     <xsl:copy> 
      <xsl:copy-of select="@*"/> 
      <xsl:variable name="new-values"> 
       <xsl:copy-of select="beans:value"/> 
       <xsl:if test="not(beans:value[text() = concat('QL.', $ident, '.0')])"> 
        <value>QL.<xsl:value-of select="$ident"/>.0</value> 
       </xsl:if> 
      </xsl:variable> 
      <xsl:apply-templates select="exsl:node-set($new-values)/*"> 
       <xsl:sort select="."/> 
      </xsl:apply-templates> 

     </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

我還增加了xmlns="http://www.springframework.org/schema/beans"樣式表,以確保新的元素在正確的命名空間創建,以便不會發生xmlns=""

+0

謝謝,它的工作!我試圖做類似的事情之前,但得到有關片段的錯誤。訣竅是使用節點集。 – Eduardo

相關問題