Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 767595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:02:59+00:00 2026-05-14T18:02:59+00:00

assuming I got following XML file : <?xml version=1.0 encoding=ISO-8859-1 ?> <MyCarShop> <Car gender=Boy>

  • 0

assuming I got following XML file :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<MyCarShop>
    <Car gender="Boy">
        <Door>Lamborghini</Door>
        <Key>Skull</Key>
    </Car>
    <Car gender="Girl">
        <Door>Normal</Door>
        <Key>Princess</Key>
    </Car>
</MyCarShop>

I want to perform a transformation so the xml looks like this :

<?xml version="1.0" encoding="ISO-8859-1" ?>
<MyCarShop>
    <Car gender="Boy">
        <Door color="blue">Lamborghini</Door>
        <Key color="blue">Skull</Key>
    </Car>
    <Car gender="Girl">
        <Door color="red">Normal</Door>
        <Key color="red">Princess</Key>
    </Car>
</MyCarShop>

So I want to add a color attribut to each subelement of Car depending on the gender information.

I came up with this XSLT but it doesnt work :

<?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:element name="MyCarShop">
      <xsl:attribute name="version">1.0</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

    <xsl:template match="Car">
    <xsl:element name="Car">
      <xsl:apply-templates/>
    </xsl:element>
  </xsl:template>

    <xsl:template match="Door">
    <xsl:element name="Door">
          <xsl:attribute name="ViewSideIndicator">
        <xsl:choose>
            <xsl:when test="gender = 'Boy' ">Front</xsl:when>
            <xsl:when test="gender = 'Girl ">Front</xsl:when>
        </xsl:choose>
    </xsl:attribute>
       </xsl:element>
  </xsl:template>

      <xsl:template match="Key">
    <xsl:element name="Key">
      <xsl:apply-templates/>
          </xsl:element>
  </xsl:template>

  </xsl:stylesheet>

Does anybody know what might be wrong ?

Thanks again!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:03:01+00:00Added an answer on May 14, 2026 at 6:03 pm

    I changed value in test to ../@gender and now this template adds color attribute to the ‘Door’ node depending on value of ‘gender’ attribute of the Car.
    .. means ‘get parent node’.
    @ means ‘get value of the attribute’.

    <xsl:template match="Door">
    <xsl:element name="Door">
      <xsl:attribute name="color">
        <xsl:choose>
          <xsl:when test="../@gender = 'Boy' ">Red</xsl:when>
          <xsl:when test="../@gender = 'Girl' ">Green</xsl:when>
        </xsl:choose>
      </xsl:attribute>
    </xsl:element>
    

    You should do the same for ‘Key’ template (or better reuse the ‘choose’ code by extracting it into separate named template).

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Assuming there are 5 items in the settings file ( MySetting1 to MySetting5 ),
Assuming the following data: Column1 (data type: varchar(50)) -------- 11.6 -1 1,000 10 Non-Numeric
I've got marshaled CDR data all by itself in the form of a file
I've got the following entities on my EDMX :- These two entites were generated
assuming i got this main window xmal: <Window x:Class=MVVMTUTRIALS.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml xmlns:views=clr-namespace:TestMvvm444.Views Title=Window1 Height=300
Assuming such a query exists, I would greatly appreciate the help. I'm trying to
Assuming you can't use LINQ for whatever reason, is it a better practice to
Assuming network access is sporadic with no central server, what would be the best
Assuming I have only the class name of a generic as a string in
Assuming I have an open source web server or proxy I can enhance, let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.