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
  • 1 View
  • 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

I'm assuming this is not possible because I got the following error: error C3533:
I just got bit by assuming the following: foreach ($arr as $key => $value)
Assuming: std::string ToShow,NumStr; The following displays This is 19 ch00: ToShow = This is
assuming I have the following array: views = [ { :user_id => 1, :viewed_at
Say I've got the following: <?php abstract class MyParent { public static $table_name; public
Assuming the following sample inline comment: /* function newMethodName (int bar, String s) {
I've got marshaled CDR data all by itself in the form of a file
A bit of information Parent Class: Vehicle Child Class: Car & Motorcycle I got
So I've got the following: NodeList nodeList = element.getElementsByTagName(rowset); if (nodeList.getLength() > 0) {
Assuming I have the following Projection Matrix (its computed using gluPerspective(40, 1.0, 0.2, 200.0);

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.