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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:04:45+00:00 2026-06-07T08:04:45+00:00

I want to be able to generate a complete XML file, given a set

  • 0

I want to be able to generate a complete XML file, given a set of XPath mappings.

The input could specified in two mappings: (1) One which lists the XPath expressions and values; and (2) the other which defines the appropriate namespaces.

/create/article[1]/id                 => 1
/create/article[1]/description        => bar
/create/article[1]/name[1]            => foo
/create/article[1]/price[1]/amount    => 00.00
/create/article[1]/price[1]/currency  => USD
/create/article[2]/id                 => 2
/create/article[2]/description        => some name
/create/article[2]/name[1]            => some description
/create/article[2]/price[1]/amount    => 00.01
/create/article[2]/price[1]/currency  => USD

For namespaces:

/create               => xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/
/create/article       => xmlns:ns1='http://predic8.com/material/1/‘
/create/article/price => xmlns:ns1='http://predic8.com/common/1/‘
/create/article/id    => xmlns:ns1='http://predic8.com/material/1/'

Note also, that it is important that I also deal with XPath Attributes expressions as well. For example: I should also be able to handle attributes, such as:

/create/article/@type => richtext

The final output should then look something like:

<ns1:create xmlns:ns1='http://predic8.com/wsdl/material/ArticleService/1/'>
    <ns1:article xmlns:ns1='http://predic8.com/material/1/‘ type='richtext'>
        <name>foo</name>
        <description>bar</description>
        <ns1:price xmlns:ns1='http://predic8.com/common/1/'>
            <amount>00.00</amount>
            <currency>USD</currency>
        </ns1:price>
        <ns1:id xmlns:ns1='http://predic8.com/material/1/'>1</ns1:id>
    </ns1:article>
    <ns1:article xmlns:ns1='http://predic8.com/material/2/‘ type='richtext'>
        <name>some name</name>
        <description>some description</description>
        <ns1:price xmlns:ns1='http://predic8.com/common/2/'>
            <amount>00.01</amount>
            <currency>USD</currency>
        </ns1:price>
        <ns1:id xmlns:ns1='http://predic8.com/material/2/'>2</ns1:id>
    </ns1:article>
</ns1:create>

PS: This is a more detailed question to a previous question asked, although due to a series of further requirements and clarifications, I was recommended to ask a more broader question in order to address my needs.

Note also, I am implementing this in Java. So either a Java-based or XSLT-based solution would both be perfectly acceptable. thnx.

Further note: I am really looking for a generic solution. The XML shown above is just an example.

  • 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-06-07T08:04:46+00:00Added an answer on June 7, 2026 at 8:04 am

    This problem has an easy solution if one builds upon the solution of the previous problem:

    <xsl:stylesheet version="2.0"
         xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
         xmlns:xs="http://www.w3.org/2001/XMLSchema"
         xmlns:my="my:my">
         <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
         <xsl:key name="kNSFor" match="namespace" use="@of"/>
         <xsl:variable name="vStylesheet" select="document('')"/>
    
         <xsl:variable name="vPop" as="element()*">
            <item path="/create/article/@type">richtext</item>
            <item path="/create/article/@lang">en-us</item>
            <item path="/create/article[1]/id">1</item>
            <item path="/create/article[1]/description">bar</item>
            <item path="/create/article[1]/name[1]">foo</item>
            <item path="/create/article[1]/price[1]/amount">00.00</item>
            <item path="/create/article[1]/price[1]/currency">USD</item>
            <item path="/create/article[1]/price[2]/amount">11.11</item>
            <item path="/create/article[1]/price[2]/currency">AUD</item>
            <item path="/create/article[2]/id">2</item>
            <item path="/create/article[2]/description">some name</item>
            <item path="/create/article[2]/name[1]">some description</item>
            <item path="/create/article[2]/price[1]/amount">00.01</item>
            <item path="/create/article[2]/price[1]/currency">USD</item>
    
            <namespace of="create" prefix="ns1:"
                       url="http://predic8.com/wsdl/material/ArticleService/1/"/>
            <namespace of="article" prefix="ns1:"
                       url="xmlns:ns1='http://predic8.com/material/1/"/>
            <namespace of="@lang" prefix="xml:"
                       url="http://www.w3.org/XML/1998/namespace"/>
            <namespace of="price" prefix="ns1:"
                       url="xmlns:ns1='http://predic8.com/material/1/"/>
            <namespace of="id" prefix="ns1:"
                       url="xmlns:ns1='http://predic8.com/material/1/"/>
         </xsl:variable>
    
         <xsl:template match="/">
          <xsl:sequence select="my:subTree($vPop/@path/concat(.,'/',string(..)))"/>
         </xsl:template>
    
         <xsl:function name="my:subTree" as="node()*">
          <xsl:param name="pPaths" as="xs:string*"/>
    
          <xsl:for-each-group select="$pPaths" group-adjacent=
                "substring-before(substring-after(concat(., '/'), '/'), '/')">
            <xsl:if test="current-grouping-key()">
             <xsl:choose>
               <xsl:when test=
                  "substring-after(current-group()[1], current-grouping-key())">
    
                 <xsl:variable name="vLocal-name" select=
                  "substring-before(concat(current-grouping-key(), '['), '[')"/>
    
                 <xsl:variable name="vNamespace"
                               select="key('kNSFor', $vLocal-name, $vStylesheet)"/>
    
    
                 <xsl:choose>
                  <xsl:when test="starts-with($vLocal-name, '@')">
                   <xsl:attribute name=
                     "{$vNamespace/@prefix}{substring($vLocal-name,2)}"
                        namespace="{$vNamespace/@url}">
                     <xsl:value-of select=
                      "substring(
                           substring-after(current-group(), current-grouping-key()),
                           2
                                 )"/>
                   </xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                   <xsl:element name="{$vNamespace/@prefix}{$vLocal-name}"
                              namespace="{$vNamespace/@url}">
    
                        <xsl:sequence select=
                         "my:subTree(for $s in current-group()
                                      return
                                         concat('/',substring-after(substring($s, 2),'/'))
                                       )
                         "/>
                     </xsl:element>
                  </xsl:otherwise>
                 </xsl:choose>
               </xsl:when>
               <xsl:otherwise>
                <xsl:value-of select="current-grouping-key()"/>
               </xsl:otherwise>
             </xsl:choose>
             </xsl:if>
          </xsl:for-each-group>
         </xsl:function>
    </xsl:stylesheet>
    

    When this transformation is applied on any XML document (not used), the wanted, correct result is produced:

    <ns1:create xmlns:ns1="http://predic8.com/wsdl/material/ArticleService/1/">
       <ns1:article xmlns:ns1="xmlns:ns1='http://predic8.com/material/1/" type="richtext"
                    xml:lang="en-us"/>
       <ns1:article xmlns:ns1="xmlns:ns1='http://predic8.com/material/1/">
          <ns1:id>1</ns1:id>
          <description>bar</description>
          <name>foo</name>
          <ns1:price>
             <amount>00.00</amount>
             <currency>USD</currency>
          </ns1:price>
          <ns1:price>
             <amount>11.11</amount>
             <currency>AUD</currency>
          </ns1:price>
       </ns1:article>
       <ns1:article xmlns:ns1="xmlns:ns1='http://predic8.com/material/1/">
          <ns1:id>2</ns1:id>
          <description>some name</description>
          <name>some description</name>
          <ns1:price>
             <amount>00.01</amount>
             <currency>USD</currency>
          </ns1:price>
       </ns1:article>
    </ns1:create>
    

    Explanation:

    1. A reasonable assumption is made that throughout the generated document any two elements with the same local-name() belong to the same namespace — this covers the predominant majority of real-world XML documents.

    2. The namespace specifications follow the path specifications. A nsmespace specification has the form: <namespace of="target element's local-name" prefix="wanted prefix" url="namespace-uri"/>

    3. Before generating an element with xsl:element, the appropriate namespace specification is selected using an index created by an xsl:key. From this namespace specification the values of its prefix and url attributes are used in specifying in the xsl:element instruction the values of the full element name and the element’s namespace-uri.

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

Sidebar

Related Questions

to export some data i want to be able to generate an html output.
I have two textures generated using a fragment shader. I want to be able
I have an ASP.NET page that I want to be able to generate and
I'm able to generate patch files from one version to another using NSIS' Vpatch.
I want to be able to generate a different image (with watermark) when the
I want to be able to generate random strings from the windows command prompt.
I want to be able to generate IIS's standard 404 response, not a custom
I want to be able to generate help documentation from existing topic files (.AML)
I want to be able to generate PDF output from my (native) C++ Windows
I want to be able to generate a highly graphical (with lots of text

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.