2016-12-17 2 views
0

Ich bin ein Neuling mit xslt. Ich muss Spring Bean zu Xml hinzufügen, falls es noch nicht existiert. Also versuchte ich nächsten Code (i ant verwenden Sie diesen Code auszuführen):XSLT Hinzufügen von Spring Bean zu beans.xml

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8" /> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
      <xsl:if test="not(bean[@class='com.mysite.MyCustomController'])"> 
       <bean class="com.mysite.MyCustomController"/> 
      </xsl:if> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

Es funktioniert, aber fügt Element mit xmlns-Attribute, so sieht es aus wie diese in der endgültigen XML-Datei:

<bean xmlns="" class="com.mysite.MyCustomController"/> 

I erwarten führen ohne xmlns-Attribut, so dass ich gesucht und meine xsl Code wendet sich an:

... 
<xsl:if test="not(bean[@class='com.mysite.MyCustomController'])"> 
    <bean class="com.mysite.MyCustomController" xmlns="http://www.springframework.org/schema/beans"/> 
</xsl:if> 
... 

und jetzt führen XML sieht gut aus:

<bean class="com.mysite.MyCustomController"/> 

aber! Wenn die Bedingung nicht funktioniert. Es fügt die gleiche Bean jedes Mal hinzu, wenn ich Code ausführe.

Ist meine xsl falsch? Danke.

Antwort

0

Korrekter Code ändern:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="2.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:bn="http://www.springframework.org/schema/beans"> 
    <xsl:output method="xml" indent="yes" encoding="UTF-8" /> 
    <xsl:attribute-set name="bean-attr-list"> 
     <xsl:attribute name="class">com.mysite.MyCustomController</xsl:attribute> 
    </xsl:attribute-set> 
    <xsl:template match="@* | node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <xsl:template match="/*"> 
     <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/> 
      <xsl:if test="not(bn:bean[@class='com.mysite.MyCustomController'])"> 
       <xsl:element name="bean" namespace="http://www.springframework.org/schema/beans" use-attribute-sets="bean-attr-list" /> 
      </xsl:if> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 
1

Ihr XML enthält die Elemente im Namespace http://www.springframework.org/schema/beans. Sie überprüfen und fügen Elemente zum Standardnamespace ("") hinzu. Um die Dinge funktionieren kann, müssen Sie Ihren Code

<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:bn="http://www.springframework.org/schema/beans" 
    exclude-result-prefixes="bn"> 
    .......... 
    <xsl:if test="not(bn:bean[@class='com.mysite.MyCustomController'])"> 
     <bean class="com.mysite.MyCustomController" 
      xmlns="http://www.springframework.org/schema/beans"/> 
    </xsl:if> 
    ............. 
</xsl:stylesheet> 
+0

"IF" funktioniert. Aber diese xsl fügt eine Bean wie folgt hinzu: . Kann ich ohne das Attribut xmlns hinzufügen? – Alexandr83

+0

@ Alexandr83 Zu xsl hinzufügen: Stylesheet-Element 'exclude-result-prefixes =" bn "' –