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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:38:36+00:00 2026-05-30T20:38:36+00:00

I have an XML like this: <?xml version=1.0 encoding=UTF-8?> <Section> <Chapter> <Cell colname=1> <Value>A</Value>

  • 0

I have an XML like this:

<?xml version="1.0" encoding="UTF-8"?>

<Section>
    <Chapter>
        <Cell colname="1">
            <Value>A</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>AAA</MyValue>
            <MyValue>BBB</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Honda</MyCar>
        </Cell>
    </Chapter>
    <Chapter>
        <Cell colname="1">
            <Value>C</Value>
        </Cell>
        <Cell colname="2">
            <MyValue>CCC</MyValue>
        </Cell>
        <Cell colname="3">
            <MyCar>Toyota</MyCar>
        </Cell>
    </Chapter>
</Section>

I have an XSLT like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:output method="xml" version="1.0" encoding="iso-8859-1" indent="yes"/>

    <xsl:template match="/">
        <xsl:apply-templates select="Section/Chapter"/>
    </xsl:template>

    <xsl:template match="Chapter">
        <xsl:apply-templates select="./Cell/MyValue"/>
    </xsl:template>

    <xsl:template match="MyValue">
        <xsl:message>
            <xsl:value-of select="../../Cell/@colname"/>
            <xsl:value-of select="../../Cell[@colname='1']/Value"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
        </xsl:message>
    </xsl:template>

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

</xsl:stylesheet>

Problem is the debug output shows up as 1 2 3 A AAA Honda 1 2 3 A BBB Honda 1 2 3 C CCC Toyota. I’d like to get something like 1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota. Basically getting the value of the attribute colname correctly.

So several questions:

  1. What did I do wrong ?.
  2. There is a sequence 1 2 3 generated why ?.

TIA,

John

  • 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-30T20:38:37+00:00Added an answer on May 30, 2026 at 8:38 pm

    So several questions:

    1. What did I do wrong ?.

    You didn’t specify exactly the necessary expressions.

    1. There is a sequence 1 2 3 generated why ?.

    Because ../../Cell/@colname selects the colname attribute of every Cell child of the current node’s grandparent Chapter.

    Solution:

    Use:

    <xsl:template match="MyValue">
        <xsl:message>
            <xsl:value-of select="../../Cell[1]/@colname"/>
            <xsl:value-of select="../../Cell[@colname='1']/Value"/>
            <xsl:value-of select="../@colname"/>
            <xsl:value-of select="."/>
            <xsl:value-of select="../../Cell[3]/@colname"/>
            <xsl:value-of select="../../Cell[@colname='3']/MyCar"/>
        </xsl:message>
    </xsl:template>
    

    The debug output is exactly the wanted:

    1A2AAA3Honda
    1A2BBB3Honda
    1C2CCC3Toyota
    

    You also can use a slight modification of the answer to your previous question:

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
         <xsl:strip-space elements="*"/>
    
         <xsl:template match="Chapter">
          <xsl:apply-templates select="Cell[1]"/>
         </xsl:template>
    
         <xsl:template match="Cell">
          <xsl:param name="pText" as="xs:string*"/>
    
          <xsl:apply-templates select="*[1]">
           <xsl:with-param name="pText" select="$pText"/>
          </xsl:apply-templates>
         </xsl:template>
    
         <xsl:template match="Cell/*">
          <xsl:param name="pText" as="xs:string*"/>
    
          <xsl:variable name="vText" as="xs:string*"
           select="$pText, ../@colname, string(.)"/>
    
          <xsl:sequence select=
           "$vText
              [not(current()/../following-sibling::Cell)]"/>
          <xsl:apply-templates select="../following-sibling::Cell[1]">
            <xsl:with-param name="pText" select="$vText"/>
          </xsl:apply-templates>
          <xsl:apply-templates select="following-sibling::*">
            <xsl:with-param name="pText" select="$pText"/>
          </xsl:apply-templates>
         </xsl:template>
    </xsl:stylesheet>
    

    When this transformation is applied on the provided XML document:

    <Section>
        <Chapter>
            <Cell colname="1">
                <Value>A</Value>
            </Cell>
            <Cell colname="2">
                <MyValue>AAA</MyValue>
                <MyValue>BBB</MyValue>
            </Cell>
            <Cell colname="3">
                <MyCar>Honda</MyCar>
            </Cell>
        </Chapter>
        <Chapter>
            <Cell colname="1">
                <Value>C</Value>
            </Cell>
            <Cell colname="2">
                <MyValue>CCC</MyValue>
            </Cell>
            <Cell colname="3">
                <MyCar>Toyota</MyCar>
            </Cell>
        </Chapter>
    </Section>
    

    the wanted, correct result is produced:

    1 A 2 AAA 3 Honda 1 A 2 BBB 3 Honda 1 C 2 CCC 3 Toyota
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XML like this: <?xml version=1.0 encoding=UTF-8?> <Section> <Chapter> <Cell colname=1> <Value>A</Value>
I have a XML like this: <?xml version=1.0 encoding=UTF-8?> <Section> <Chapter> <nametable> <namerow> <namecell
I have XML-file with settings like this <?xml version=1.0 encoding=utf-8 ?> <configuration> <configSections> <sectionGroup
I have a xml like this: <?xml version=1.0 encoding=utf-8?> <assessment xmlns=http://xml.thinkcentral.com/pub/xml/hsp/assessment xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xhtml=http://www.w3.org/1999/xhtml xmlns:tia=http://xml.thinkcentral.com/pub/xml/hsp/tia
have an xml file like this. <?xml version =1.0 encoding =utf-8?> <menu> <menuNode title=Register
I have an xml file which looks like this <?xml version=1.0 encoding=UTF-8 ?> <BulkDataExchangeRequests
I have a simple XML file like so: <?xml version=1.0 encoding=UTF-8?> <foo attr=blah &#176;
I would like to have an xml config file as follows: <?xml version=1.0 encoding=utf-8
I have the following sample nested XML <?xml version=1.0 encoding=UTF-8?> <text> <main> <div n=Section
My XML looks like this: <?xml version = 1.0 encoding = utf-8?> <gallery> <name>Rosie'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.