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 1004963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:12:57+00:00 2026-05-16T08:12:57+00:00

I have an XML document which I’m subjected to an XSLT. The structure is

  • 0

I have an XML document which I’m subjected to an XSLT. The structure is similar to:

<root>
  <item value="1">
     <object/>
  </item>
  <item value="2" />
     <object/>
  </item>
</root>

My goal is to end up with a transformed XML similar to:

<root>
 <parent>
  <object-one value-one="1"/>
 </parent>
 <parent>
  <object-two value-two="2"/>
 </parent>
</root>

My XSLT is similar to:

<xsl:apply-templates select="object" />


<xsl:template match="object">
    <xsl:call-template name="1" />
    <xsl:call-template name="2" />
</xsl:template>

<xsl:template name="1" match="object[item/@value = '1'">
  <xsl:element name="object-one" namespace="http://something.org">
    <xsl:attribute name="_Description">
      <xsl:value-of select="@_Type"/>
    </xsl:attribute>
    <xsl:attribute name="_Type">
      <xsl:value-of select="@_Amount"/>
   </xsl:attribute>
  </xsl:element>
</xsl:template>

 <xsl:template name="2" match="object[item/@value = '2'">
  <xsl:element name="object-two" namespace="http://something.org">
    <xsl:attribute name="OriginalAmount">
      <xsl:value-of select="@_Amount"/>
    </xsl:attribute>
  </xsl:element>
</xsl:template>

The problem is the all item nodes are having the same template applied. How can I apply a template to only specific nodes?

  • 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-16T08:12:58+00:00Added an answer on May 16, 2026 at 8:12 am

    EDIT: Now for different input sample (corrected for well-formed):

    <root>
        <item value="1">
            <object/>
        </item>
        <item value="2" >
            <object/>
        </item>
    </root>
    

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:num="number" extension-element-prefixes="num">
        <num:num>one</num:num>
        <num:num>two</num:num>
        <xsl:template match="root">
            <xsl:copy>
                <xsl:apply-templates/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="item">
            <parent>
                <xsl:apply-templates/>
            </parent>
        </xsl:template>
        <xsl:template match="object">
            <xsl:variable name="vTextNumber" select="document('')/*/num:*[number(current()/../@value)]"/>
            <xsl:element name="object-{$vTextNumber}">
                <xsl:attribute name="value-{$vTextNumber}">
                    <xsl:value-of select="../@value"/>
                </xsl:attribute>
            </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    <root>
        <parent>
            <object-one value-one="1" />
        </parent>
        <parent>
            <object-two value-two="2" />
        </parent>
    </root>
    

    EDIT 2: Now, what is wrong within your stylesheet fragment? Well, it looks like you don’t know how the processor resolves template rules applying, also XPath navegation.

    First, this object[item/@value = '1'] will match only this kind of input

    <object>
        <item value="1"/>
    </object>
    

    Second, consider this three rules

    1 –

    <xsl:template match="object">
    </xsl:template> 
    

    2 –

    <xsl:template name="1" match="object[../@value = '1']"> 
    </xsl:template> 
    

    3 –

    <xsl:template name="2" match="object[../@value = '2']"> 
    </xsl:template> 
    

    With your last provided input, first object element (in document order) will match rules 1 and 2, and then the processor would resolve to apply rule 2. Why? From http://www.w3.org/TR/xslt#conflict

    Next, all matching template rules that
    have lower priority than the matching
    template rule or rules with the
    highest priority are eliminated from
    consideration. The priority of a
    template rule is specified by the
    priority attribute on the template
    rule. The value of this must be a real
    number (positive or negative),
    matching the production Number with an
    optional leading minus sign (-). The
    default priority is computed as
    follows:

    • If the pattern contains multiple alternatives separated by |, then it
      is treated equivalently to a set of template rules, one for each
      alternative.
    • If the pattern has the form of a QName preceded by a
      ChildOrAttributeAxisSpecifier or has the form processing-instruction(Literal)
      preceded by a ChildOrAttributeAxisSpecifier, then the priority is 0.
    • If the pattern has the form NCName:* preceded by a
      ChildOrAttributeAxisSpecifier, then the priority is -0.25.
    • Otherwise, if the pattern consists of just a NodeTest
      preceded by a ChildOrAttributeAxisSpecifier, then the priority is -0.5.
    • Otherwise, the priority is 0.5.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML document looking similar to this: <items> <item cat=1 owner=14>bla</item> <item
I have a JSP which attaches a XSL to an XML document pulled from
I have an XML document with un-namespaced elements, and I want to use XSLT
I have an xml document object that I need to convert into a string.
I have XML document which is something like <X><Y><Values><double>1.0</double><double>2.0</double></Values>... I am trying to get
I have an XML document which contains an element that is over 90,000 characters
I have an XML document which is confounding me. I'd like to (to start)
I have an XML document which contains some invalid characters (é for example). Unfortunalty
I have one XML document which I want to store it inside ViewState so
I have an xml document which consists of a number of the following: -

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.