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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:20:56+00:00 2026-05-15T18:20:56+00:00

I want to output something similar to the following using XSLT XML: <myroot> <node1>

  • 0

I want to output something similar to the following using XSLT

XML:

<myroot>
  <node1>
    <subnode1>somestuff</subnode1>
    <subnode2>otherstuff</subnode2>
  </node1>
  <node2>
    <subnode2></subnode2>
    <subnode3>stuff here</subnode3>
  </node2>
  <node3>
    <subnode>stuff</subnode>
    <subnode>stuff</subnode>
    <subnode>other</subnode>
  </node3>
</myroot>

Where I do not know the node names for a given instance.

I want my output to look like this:

myroot = new jsonObject();
myroot.node1 = new jsonObject();
myroot.node1.subnode1 = "holder";
myroot.node1.subnode2 = "holder";
myroot.node2 = new jsonObject();
myroot.node2.subnode2 = "holder";
myroot.node2.subnode3 = "holder";
myroot.node3 = new jsonObject();
myroot.node3.subnode = new array();
"arraystart"
myroot.node3.subnode[aindex] = new jsonObject();
myroot.node3.subnode[aindex] = "holder";
"endarray"

Important points:

  • = “holder”; can be anything unique as I will change this later
  • “arraystart” and “endarray” can be
    anything unique as I will change this
    later
  • I do NOT know the specific node names
    beyond the root.
  • I do NOT know the depth of the tree
    (some 6-7 deep exist)
  • I do NOT know the numbers or position
    or array elements, but the child
    nodes(elements) are the same name for those groups.
  • Multiple arrays may/do exist, and can
    be at any tree depth.
  • Elements with text do not have child
    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-15T18:20:57+00:00Added an answer on May 15, 2026 at 6:20 pm

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:key name="name" match="*" use="name()"/>
        <xsl:template match="text()"/>
        <xsl:template match="*[*]">
            <xsl:param name="name"/>
            <xsl:value-of select="concat($name,
                                         name(),
                                         ' = new jsonObject();&#xA;')"/>
            <xsl:apply-templates>
                <xsl:with-param name="name" select="concat($name,name(),'.')"/>
            </xsl:apply-templates>
        </xsl:template>
        <xsl:template match="*[not(*)][count(../*|key('name',name()))!=count(key('name',name()))]">
            <xsl:param name="name"/>
            <xsl:value-of select="concat($name,
                                         name(),
                                         ' = &quot;holder&quot;;&#xA;')"/>
        </xsl:template>
        <xsl:template match="*[not(*)][1][count(../*|key('name',name()))=count(key('name',name()))]" priority="1">
            <xsl:param name="name"/>
            <xsl:value-of select="concat($name,
                                         name(),
                                         ' = new array();&#xA;',
                                         '&quot;arraystart&quot;&#xA;')"/>
            <xsl:apply-templates select="following-sibling::*" mode="array">
                <xsl:with-param name="name" select="concat($name,name(),'.')"/>
            </xsl:apply-templates>
            <xsl:text>"endarray"</xsl:text>
        </xsl:template>
        <xsl:template match="*" mode="array">
            <xsl:param name="name"/>
            <xsl:value-of select="concat($name,
                                         '[aindex] = ')"/>
            <xsl:choose>
                <xsl:when test="contains(.,'stuff')">new jsonObject();&#xA;</xsl:when>
                <xsl:otherwise>"holder";&#xA;</xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    </xsl:stylesheet>
    

    Output:

    myroot = new jsonObject();
    myroot.node1 = new jsonObject();
    myroot.node1.subnode1 = "holder";
    myroot.node1.subnode2 = "holder";
    myroot.node2 = new jsonObject();
    myroot.node2.subnode2 = "holder";
    myroot.node2.subnode3 = "holder";
    myroot.node3 = new jsonObject();
    myroot.node3.subnode = new array();
    "arraystart"
    myroot.node3.subnode.[aindex] = new jsonObject();
    myroot.node3.subnode.[aindex] = "holder";
    "endarray"
    

    But I think you should refine your goal.

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

Sidebar

Related Questions

While converting an old code, I encountered the following problem. Given an HTML string,
I am making a small app that deletes log files. I am using an
I have a breadcrumb structure similar to the image above. It displays the progress
Sorry for asking a question about something I don't know much about, but I've
Briefly: I'm making a custom view, I want a field listing the two taxonomy
My objective is to find the process that is consuming the most CPU and
I'd like to inspect and manipulate code of arbitrary Perl procedures (got by coderefs)
Given a table like this: Col 1 | Col 2 1 2 1 3
I'm interested in creating a custom Export to Excel option for my Report in
My Table 'Levels' in a SQL Server 2005 database looks like this: LevelId Description

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.