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

  • Home
  • SEARCH
  • 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 3795112
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:02:02+00:00 2026-05-19T13:02:02+00:00

I have two XMLs, one for defining the templates for the objects of my

  • 0

I have two XMLs, one for defining the templates for the objects of my app. and another with the actual objects. Basically in each object only a few values are changed, that’s why I wanted to provide some kind of template mechanism and apply XSL to transform them into the final one.

This is a sample object:

<config>
<objects>
    <object code="1000" name="object1">
        <template name="decoration" buyCoins="60" />
    </object>
</objects>

And this is a sample template for that object:

<config xmlns:template="object-template">
<templates>
  <template name="decoration">
<connection type="make" />
<placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
  <requirement template:coins="buyCoins"/>
  <reward xp="1" />
</buyable>
<sellable>
  <reward coins="1"/>
</sellable></template></templates></config>

This is my current XSL:

<xsl:variable name="templates" select="document('../templates.xml')/config/templates//template" />

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

<xsl:template match="//template">
    <xsl:variable name="itemTemplate" select="."/>
    <xsl:variable name="templateName" select="@name"/>
    <xsl:variable name="selectedTemplate" select="$templates[@name = $templateName]/*" />

    <xsl:for-each select="$selectedTemplate">

            <!-- This part is only a test to get the values that I need -->
        <xsl:for-each select=".//@*[namespace-uri() = 'object-template']">
            <xsl:variable name="attributeName" select="name()"/>
            <xsl:variable name="attributeValue" select="."/>
            <xsl:variable name="finalValue" select="$itemTemplate/@*[local-name() = $attributeValue]"/>

        </xsl:for-each> 
    </xsl:for-each>
</xsl:template>

<xsl:template match="comment()"/>

I just need to do the following:

  1. Iterate every item that contains a template tag
  2. For each one get the associated template of the templates document
  3. Copy everything inside the template into the object (*)
  4. Replace the values of the attributes inside the item (now with the template pasted) with the real value in the original template (**)

Explanation:

(*) Apply

 <template name="test"><node></template> 

to

<object><template name="test"></object> 

becomes

<object><node></object>

(**) In the original sample above, the value of buyCoins of the item’s template tag should replace the template’s value of the text “buyCoins” before sending it into the output. For easy lookup and to avoid reg. exp. I am using namespaces. So what I do in the XSL is to search for all the attributes inside the template with the right namespace and search for the values.
The value “60” should be put instead of “buyCoins” inside the coins attribute.

My problem is that I don’t understand how to copy everything (I believe that is called an identity copy) but replace the value that I need.

Any help will be appreciated, thanks!!!

UPDATE:

The current output is:

<config xmlns:template="item-template">
<objects>   
    <object code="1000" name="object1" type="decorations">
    </object>
</object>

If I put:

<xsl:copy-of select="."/>

Below:

<xsl:for-each select="$selectedTemplate">

Then I get:

<objects>
<object code="1000" name="object1">
    <connection type="make" /><placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
    <requirement template:coins="buyCoins"/>
    <reward xp="1" />
</buyable>
<sellable>
    <reward coins="1"/>
</sellable>
</object></objects>

That is the first part of what I need, to put on each output item the content of the template associated to it. I am having problems replacing the values now.

These tree lines in the XSL represents the data that I need:

        <xsl:variable name="attributeName" select="name()"/>
        <xsl:variable name="attributeValue" select="."/>
        <xsl:variable name="finalValue" select="$itemTemplate/@*[local-name() = $attributeValue]"/>

For the only item in this example this is the content of each variable:
attributeName will contain “template:coins”
attributeValue will contain “buyCoins”
finalValue will contain “60”

I need to put finalValue instead of attributeValue in the tag of that attributeName.

At that point I am stuck 🙁

Thanks!

Update 2:

To avoid misunderstandings, here is the EXACT output that I need:

    <objects>
<object code="1000" name="object1">
    <connection type="make" /><placeable width="1" length="1" moveable="true" collision="D" />
<buyable>
    <requirement coins="60"/>
    <reward xp="1" />
</buyable>
<sellable>
    <reward coins="1"/>
</sellable>
</object></objects>

Instead of “buyCoins” in the attribute coins I need it to be “60” (The value in the input objects file).
Also the best possible output should remove the namespace templates of the attributes too, because its only a flag for the XSL to know which attributes to modify.

  • 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-19T13:02:03+00:00Added an answer on May 19, 2026 at 1:02 pm

    Just for fun, this XSLT 1.0 stylesheet:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:template="object-template"
     exclude-result-prefixes="template">
        <xsl:key name="kTemplateByName" match="templates/template" use="@name"/>
        <xsl:template match="*">
            <xsl:param name="pContext"/>
            <xsl:element name="{name()}">
                <xsl:copy-of select="namespace::*[.!='object-template']"/>
                <xsl:apply-templates select="node()|@*">
                    <xsl:with-param name="pContext" select="$pContext"/>
                </xsl:apply-templates>
            </xsl:element>
        </xsl:template>
        <xsl:template match="@*">
            <xsl:copy-of select="."/>
        </xsl:template>
        <xsl:template match="templates"/>
        <xsl:template match="config">
            <xsl:apply-templates/>
        </xsl:template>
        <xsl:template match="template">
            <xsl:apply-templates select="key('kTemplateByName',@name)/node()">
                <xsl:with-param name="pContext" select="."/>
            </xsl:apply-templates>
        </xsl:template>
        <xsl:template match="@template:*">
            <xsl:param name="pContext"/>
            <xsl:attribute name="{local-name()}">
                <xsl:value-of select="$pContext//@*[name()=current()]"/>
            </xsl:attribute>
        </xsl:template>
    </xsl:stylesheet>
    

    With this input:

    <config xmlns:template="object-template">
        <objects>
            <object code="1000" name="object1">
                <template name="decoration" buyCoins="60" />
            </object>
        </objects>
        <templates>
            <template name="decoration">
                <connection type="make" />
                <placeable width="1" length="1" moveable="true" collision="D" />
                <buyable>
                    <requirement template:coins="buyCoins"/>
                    <reward xp="1" />
                </buyable>
                <sellable>
                    <reward coins="1"/>
                </sellable>
            </template>
        </templates>
    </config>
    

    Output:

    <objects>
        <object code="1000" name="object1">
            <connection type="make"></connection>
            <placeable width="1" length="1" moveable="true" collision="D"></placeable>
            <buyable>
                <requirement coins="60"></requirement>
                <reward xp="1"></reward>
            </buyable>
            <sellable>
                <reward coins="1"></reward>
            </sellable>
        </object>
    </objects>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have two events: $('body').mouseup(function(e){} and $('.toggle').click(function(e){} I only want one of these to trigger.
Have two actionsheet buttons and one modalviewcontroller on mainviewcontroller in application. Now for two
I have two DropDownListBoxes one is called ddlDay and the other is ddlMonth. As
I have two application that need to talk to each other. App1 needs to
I have two controls on a page, one is a search entry and submission
I have two tables inside a database. One stores unique userNames and a unique
I have two Hibernate data object. The first is a User (with unique id,
I have two structs (part of the assignment). A list of one -- Activity,
I have two xmls.There is a amount field which can contains values like 54.2,54.23,54.234,54.234567.
Have two sets of data (two tables) for patient records, one 1999-2003, the other

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.