ThomasM: Attribute verändern

Beitrag lesen

Hallo tt,

<xsl:template match="@a">
??? wie kann ich hier die Existenz und den Wert von b testen und Veränderungen an dem Attribut a sowie dem Attribut b vornehmen ???
</xsl:template>

Versuch unter Annahme dieser XML-Struktur:

<?xml version="1.0" encoding="ISO-8859-1"?>  
<test>  
  <el a="1" b="2"/>  
  <el a="3" b="4"/>  
  <el a="5"/>  
</test>

Transformation:

<xsl:template match="@*|node()">  
  <xsl:copy>  
    <xsl:apply-templates select="@*|node()"/>  
  </xsl:copy>  
</xsl:template>  
  
<xsl:template match="@a">  
  <xsl:choose>  
    <xsl:when test="../@b">  
      <xsl:attribute name="a">  
        <xsl:value-of select="number(.) + 1"/>  
      </xsl:attribute>  
      <xsl:attribute name="b">  
        <xsl:value-of select="number(../@b) + 2"/>  
      </xsl:attribute>  
    </xsl:when>  
    <xsl:otherwise>  
      <xsl:attribute name="a">  
        <xsl:value-of select="."/>  
      </xsl:attribute>  
    </xsl:otherwise>  
  </xsl:choose>  
</xsl:template>  
  
<xsl:template match="@b"/>

Ergebnis:

<?xml version="1.0" encoding="ISO-8859-1"?>  
<test>  
  <el a="2" b="4"/>  
  <el a="4" b="6"/>  
  <el a="5"/>  
</test>

Das dritte Element ohne b bleibt also bezüglich a unverändert. Bei den Elementen mit a und b werden Werte addiert.

Grüße,
Thomas