XSL - Tags uniek maken

Uit De Vliegende Brigade
Naar navigatie springen Naar zoeken springen
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Casus: Description-veld (lente 2018)

Het probleem

Hoe zorg ik ervoor, dat een XML-bestand zoals

  <Items>
    <Item code="00000001" type="S" searchcode="00000001">
      <Description>Widget</Description>

Wordt zoals dit:

  <Items>
    <Item code="00000001" type="S" searchcode="00000001">
      <Item_Description>Widget</Item_Description>

De reden: Verderop in dit XML-bestand komt opnieuw het veld Description voor, maar dan als eindelement bij andere parent-elemtenn.

Aanvullende gegevens

  • De rest van het bronbestand moet intact blijven
  • Attributen mogen niet verloren gaan

Oplossing

<!-- =========================================== -->
<!-- Maak velden uniek, maar alleen als daardoor -->
<!-- geen attributen verloren gaan               -->
<!-- =========================================== -->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


<!-- Identity transformation -->

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


<!-- Item/Description -->

<xsl:template match="Item/Description">
   <item_description>
      <xsl:apply-templates/>
   </item_description>
</xsl:template>


<!-- Assortment/Description -->

<xsl:template match="Assortment/Description">
   <assortment_description2>
      <xsl:apply-templates/>
   </assortment_description2>
</xsl:template>


<!-- Availability/DateStart -->

<xsl:template match="Availability/DateStart">
   <availability_datestart>
      <xsl:apply-templates/>
   </availability_datestart>
</xsl:template>


<!-- Sales//Value -->

<xsl:template match="Sales//Value">
   <sales_value>
      <xsl:apply-templates/>
   </sales_value>
</xsl:template>


<!-- Costs//Values -->

<xsl:template match="Costs//Value"><costs_value><xsl:apply-templates/></costs_value></xsl:template>

</xsl:stylesheet>