SORU
5 Mart 2009, PERŞEMBE


XSLT: öznitelik değeri sırasında değiştirme <xsl:copy>?

Bir XML belgesi var ve öznitelikleri için değerleri değiştirmek istiyorum.

İlk çıkış için giriş gelen her şeyi kullanarak kopyaladım:

<xsl:template match="@*|node()">
  <xsl:copy>
    <xsl:apply-templates select="@*|node()"/>
  </xsl:copy>
</xsl:template>

Ve şimdi herhangi bir öğe "property" adlı özniteliği "type" değerini değiştirmek istiyorum.

CEVAP
6 Mart 2009, Cuma


Bu sorun klasik bir çözüm vardır:the identity template kullanarak bastırıyor en temel ve güçlü XSLT tasarım modellerinden biri:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="pNewType" select="'myNewType'"/>

    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="property/@type">
        <xsl:attribute name="type">
            <xsl:value-of select="$pNewType"/>
        </xsl:attribute>
    </xsl:template>
</xsl:stylesheet>

Bu XML belgesi üzerinde uygulandığında:

<t>
  <property>value1</property>
  <property type="old">value2</property>
</t>

istediği sonucu üretilir:

<t>
  <property>value1</property>
  <property type="myNewType">value2</property>
</t>

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • ArkticPlanet

    ArkticPlanet

    9 ŞUBAT 2010
  • Santozz Yazz

    Santozz Yazz

    23 Mart 2014
  • World Science Festival

    World Scienc

    1 Mayıs 2008