簡體   English   中英

使用xslt 1.0分割xml的元素值

[英]Split element values of xml using xslt 1.0

我有一個輸入標簽名稱屬性,其中有許多值(例如鍵值對),以分號分隔。 我需要找到特定的字符串並將值(在=之后出現)分配給特定的輸出節點

例如,在下面的input.xml中,我想使用mail.debug = false並將值“ false”分配給輸出xml調試節點。

Input.xml文件

<mail-session>
    <name>MailSession-1</name>
    <target>AdminServer</target>
    <jndi-name>MailNotification2</jndi-name>
    <properties>
    mail.debug=false;mail.smtp.user=weblogic;mail.from=master@mydom.com;
   </properties>
  </mail-session>

我的output.xml應該是這樣的

<?xml version="1.0" encoding="UTF-8"?>
<mail-session jndi-name="java:jboss/mail/mailservice" name="MailSession-1" debug="false" from="master@mydom.com">
<smtp-server outbound-socket-binding-ref="" tls="" ssl="">
<login name="weblogic" password=""/>
</smtp-server>
</mail-session><subsystem>
</subsystem>

對於XSLT 2.0

您應該看一下Tokenize函數( http://www.xsltfunctions.com/xsl/fn_tokenize.html

tokenize('mail.debug=false;mail.smtp.user=weblogic;mail.from=master@mydom.com;', '[;\s]+')

較舊的XSLT

如果您使用的是較舊的XSLT版本,請創建自己的函數:

<xsl:template name="tokenize-string">
    <xsl:param name="list" /> 
    <xsl:variable name="newlist" select="concat(normalize-space($list), ' ')" /> 
    <xsl:variable name="first" select="substring-before($newlist, ' ')" /> 
    <xsl:variable name="remaining" select="substring-after($newlist, ' ')" /> 
    <id>
        <xsl:value-of select="$first" /> 
    </id>
    <xsl:if test="$remaining">
        <xsl:call-template name="tokenize-string">
            <xsl:with-param name="list" select="$remaining" /> 
        </xsl:call-template>
    </xsl:if>
</xsl:template>

為了更清晰,動態地執行更改和需求,我建議使用以下XSL:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">    
    <xsl:template name="get_value_from_str">
        <xsl:param name="in.str"/>
        <xsl:param name="in.val"/>
        <xsl:param name="in.sep"/>
        <xsl:choose>
            <xsl:when test="string-length($in.val) &gt;0 and string-length($in.sep) &gt;0">
                <xsl:value-of select="substring-before(substring-after(normalize-space($in.str), concat($in.val, '=')), $in.sep)"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="normalize-space($in.str)"/>
            </xsl:otherwise>
        </xsl:choose>        
    </xsl:template>

    <xsl:attribute-set name="mail-session-attr">
        <xsl:attribute name="jndi-name">
            <xsl:call-template name="get_value_from_str">
                <xsl:with-param name="in.str" select="'java:jboss/mail/mailservice'" />
            </xsl:call-template>
        </xsl:attribute>    
        <xsl:attribute name="name">
            <xsl:call-template name="get_value_from_str">
                <xsl:with-param name="in.str" select="/mail-session/name" />
            </xsl:call-template>            
        </xsl:attribute>    
        <xsl:attribute name="debug">
            <xsl:call-template name="get_value_from_str">
                <xsl:with-param name="in.str" select="/mail-session/properties" />
                <xsl:with-param name="in.val" select="'mail.debug'" />
                <xsl:with-param name="in.sep" select="';'" />                
            </xsl:call-template>
        </xsl:attribute>    
        <xsl:attribute name="from">
            <xsl:call-template name="get_value_from_str">
                <xsl:with-param name="in.str" select="/mail-session/properties" />
                <xsl:with-param name="in.val" select="'mail.from'" />
                <xsl:with-param name="in.sep" select="';'" />                
            </xsl:call-template>            
        </xsl:attribute>    
    </xsl:attribute-set>    
    <xsl:attribute-set name="smtp-server-attr">
        <xsl:attribute name="outbound-socket-binding-ref" select="''"/>
        <xsl:attribute name="tls" select="''"/>
        <xsl:attribute name="ssl" select="''"/>    
    </xsl:attribute-set> 
    <xsl:attribute-set name="login-attr">
        <xsl:attribute name="name">
            <xsl:call-template name="get_value_from_str">
                <xsl:with-param name="in.str" select="/mail-session/properties" />
                <xsl:with-param name="in.val" select="'mail.smtp.user'" />
                <xsl:with-param name="in.sep" select="';'" />                
            </xsl:call-template>            
        </xsl:attribute>    
        <xsl:attribute name="password" select="''"/>
    </xsl:attribute-set>   

    <xsl:template match="/">        
        <xsl:element name="mail-session" use-attribute-sets="mail-session-attr">
            <xsl:element name="smtp-server" use-attribute-sets="smtp-server-attr">
                <xsl:element name="login" use-attribute-sets="login-attr"/>
            </xsl:element>    
        </xsl:element>    
        <xsl:element name="subsystem" />        
    </xsl:template>
</xsl:stylesheet>

然后結果將如預期的那樣:

<?xml version="1.0" encoding="UTF-8"?>
<mail-session jndi-name="java:jboss/mail/mailservice" name="MailSession-1" debug="false" from="master@mydom.com">
    <smtp-server outbound-socket-binding-ref="" tls="" ssl="">
        <login name="weblogic" password=""/>
    </smtp-server>
</mail-session>
<subsystem/>

希望對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM