none
Add Attributes To XML Tag Using XSLT

    Question

  • Hi,

    I am trying to add two attributes to Parameters tag.

    <?xml version="1.0" encoding="utf-8"?>
    <Parameters A="True" B="0.05" C="50" xmlns="clr-namespace:Test.Core.Model.Classes;assembly=Test.Core.Model" xmlns:ocme="clr-namespace:Test.Core.Model.Enums;assembly=Test.Core.Model" xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
      <Parameters.Searches>
        <scg:List x:TypeArguments="ocme:SearchType" Capacity="8">
          <ocme:SearchType>Two</ocme:SearchType>
          <ocme:SearchType>Three</ocme:SearchType>
          <ocme:SearchType>Four</ocme:SearchType>
        </scg:List>
      </Parameters.Searches>
      <Parameters.Mutations>
        <scg:List x:TypeArguments="ocme:SearchType" Capacity="8">
          <ocme:SearchType>Two</ocme:SearchType>
          <ocme:SearchType>Three</ocme:SearchType>
          <ocme:SearchType>Four</ocme:SearchType>
        </scg:List>
      </Parameters.Mutations>
      <Parameters.Searches2>
        <scg:List x:TypeArguments="Search" Capacity="0" />
      </Parameters.Searches2>
    </Parameters>

    I am using this XSLT file.

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="Parameters">
        <xsl:copy>
          <xsl:attribute name="D">0</xsl:attribute>
          <xsl:attribute name="E">100</xsl:attribute>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    But the output xml is same as input xml. Where am i doing wrong?

    Tuesday, September 03, 2013 8:37 AM

Answers

  • When i changed Parameters to /* problem solved. I don't know the reason. Maybe it could't match top level node by name.

    New xslt:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/*">
        <xsl:copy>
          <xsl:attribute name="D">0</xsl:attribute>
          <xsl:attribute name="E">100</xsl:attribute>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>

    • Marked as answer by Tempeck81 Tuesday, September 03, 2013 1:51 PM
    Tuesday, September 03, 2013 1:49 PM

All replies

  • Tuesday, September 03, 2013 11:09 AM
  • When i changed Parameters to /* problem solved. I don't know the reason. Maybe it could't match top level node by name.

    New xslt:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/*">
        <xsl:copy>
          <xsl:attribute name="D">0</xsl:attribute>
          <xsl:attribute name="E">100</xsl:attribute>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>

    • Marked as answer by Tempeck81 Tuesday, September 03, 2013 1:51 PM
    Tuesday, September 03, 2013 1:49 PM