2017-12-07 10 views
0

私は、セミコロンで区切られた(キーと値のペアのような)多数の値を持つ入力タグ名のプロパティを持っています。特定の文字列を見つけて特定の出力ノードに値を代入する必要がありますxslt 1.0を使用したXMLの分割要素の値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;[email protected]; 
    </properties> 
    </mail-session> 

私のOutput.xmlは、XSLTの場合、この

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

答えて

0

私は以下の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="[email protected]"> 
    <smtp-server outbound-socket-binding-ref="" tls="" ssl=""> 
     <login name="weblogic" password=""/> 
    </smtp-server> 
</mail-session> 
<subsystem/> 

希望すると便利です。

1

ようにする必要がありますが、トークン化機能(http://www.xsltfunctions.com/xsl/fn_tokenize.html

を見てみる必要があります

2.0

tokenize('mail.debug=false;mail.smtp.user=weblogic;[email protected];', '[;\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> 
関連する問題