Changing an attribute of an XML element

Asked

Viewed 457 times

0

I have the following XML:

<?xml version="1.0" encoding="WINDOWS-1252"?>
  -<NewDataSet>
    -<xs:schema xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" id="NewDataSet">
      -<xs:element msdata:Locale="" msdata:MainDataTable="INFO" msdata:IsDataSet="true" name="NewDataSet">
        -<xs:complexType>
          -<xs:choice maxOccurs="unbounded" minOccurs="0">
            -<xs:element msdata:Locale="" name="INFO">

I need to change the last line with XSLT to where it reads name="INFO" stick around name="INFO2". I’ve tried several transformations and none works :(

  • 1

    Show the xslt you are using

  • Which criteria you use to select the element that needs to be changed?

1 answer

0


To replace the value of an attribute you first need to locate your element through an Xpath expression, which will be passed to the stylesheet template.

I will assume that the selection is made by the name of xs:element external that has an attribute namewith the value of NewDataSet. In this case, the Xpath to locate this element is:

//xs:element[@name='NewDataSet']

This can be passed to the attribute match of xsl:template without the // and will be the context used within the template. You can also use a more specific context by continuing the path to the element where the attribute you want to change is:

xs:element[@name='NewDataSet']/xs:complexType/xs:choice/xs:element

Knowing the element you want to change, you then need to define two templates in the stylesheet. One to copy all other elements and attributes, and one to select the above element and swap its attribute. You also need to declare the namespaces you are using, so that the above-qualified Xpath expressions work.

The XSLT below does the substitution using the criteria described above:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
    version="1.0">

    <xsl:strip-space elements="*"/>
    <xsl:output indent="yes"/>

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

    <xsl:template match="xs:element[@name='NewDataSet']/xs:complexType/xs:choice/xs:element">
        <xsl:copy>
            <xsl:attribute name="name">INFO2</xsl:attribute>
            <xsl:apply-templates select="@*[name() != 'name']|*"/>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

The first template copies all elements and attributes to the output. The second supersedes the attribute value and copies the sub-elements and other attributes of xs:element if they exist.

  • Thanks for the help! Is it possible to make a second transformation using the same XSLT file? I also need to make the following change: <xsl:template match="INFO"> <INFO2><xsl:apply-templates select="@*|Node()" /></INFO2> </xsl:template>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.