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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:05:42+00:00 2026-05-23T05:05:42+00:00

I need to ensure that an XML document always contains these nodes: <group> <section>0001</section>

  • 0

I need to ensure that an XML document always contains these nodes:

<group>
    <section>0001</section>
    <head>0002</head>
    <body>0003</body>
</group>

A typical XML input file looks like this (the group node should always precede category and summary):

<story>
    <group>
        <section>section-content</section>
        <head>head-content</head>
        <body>body-content</body>
        <extra>extra-content</extra>
    </group>
    <category>some text</category>
    <summary>some text</summary>
</story>

But there is no guarantee that the group element or any of it’s child elements will exist.

So given an XML document:

<story>
    <category>some text</category>
    <summary>some text</summary>
</story>

The output should look like:

<story>
    <group>
        <section>0001</section>
        <head>0002</head>
        <body>0003</body>
    </group>
    <category>some text</category>
    <summary>some text</summary>
</story>

The XSLT should not modify existing content eg.

<story>
    <group>
        <section>existing text</section>
        <extra>existing text</extra>
    </group>
    <category>some text</category>
    <summary>some text</summary>
</story>

Should transform to:

<story>
    <group>
        <section>existing text</section>
        <head>0002</head>
        <body>0003</body>
        <extra>existing text</extra>
    </group>
    <category>some text</category>
    <summary>some text</summary>
</story>

Using Tomalak’s answer as a basis, I came up with this:

<xsl:stylesheet   version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:subst="http://tempuri.com/mysubst"
>

<xsl:strip-space elements="*"/>
<xsl:output method="xml" encoding="utf-8" indent="yes"/>

  <!-- These defaults (elements and contents) can be modified at any time -->
  <subst:defaults>
    <subst:element name="group">
    <subst:section>0001</subst:section>
    <subst:head>0002</subst:head>
    <subst:body>0002</subst:body>
    </subst:element>
  </subst:defaults>

<!-- this makes the above available as a variable -->
<xsl:variable name="defaults" select="document('')/*/subst:defaults" />

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

<xsl:template match="story[not(group)]">
  <xsl:copy>
   <xsl:apply-templates select="@*"/>
      <xsl:element name="group"></xsl:element>
   <xsl:apply-templates select="node()"/>
  </xsl:copy>
 </xsl:template>

  <xsl:template match="group">
    <xsl:copy>
      <xsl:copy-of select="@*|*"/>
      <xsl:call-template name="create-defaults" />
    </xsl:copy>
  </xsl:template>

  <!-- Insert the defaults-->
  <xsl:template name="create-defaults">
    <xsl:variable name="this" select="." />
    <xsl:for-each select="$defaults/subst:element[@name = name($this)]/*">
      <xsl:if test="not($this/*[name() = local-name(current())])">
        <xsl:apply-templates select="." />
      </xsl:if>
    </xsl:for-each>
  </xsl:template>

<!-- Remove the namespaces -->
  <xsl:template match="*">
    <xsl:element name="{local-name()}">
      <xsl:apply-templates select="@*|node()"/>
    </xsl:element>
</xsl:template>

</xsl:stylesheet>

It seems works if the group element already exists, but I can’t figure out how to get it to work if the group element doesn’t exist.

  • 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-23T05:05:43+00:00Added an answer on May 23, 2026 at 5:05 am

    This XSLT 1.0 transformation

    <xsl:stylesheet   version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:subst="http://tempuri.com/mysubst"
      exclude-result-prefixes="subst"
    >
      <xsl:strip-space elements="*"/>
      <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    
      <subst:defaults>
        <subst:element name="group">
          <subst:element name="section">0001</subst:element>
          <subst:element name="head">0002</subst:element>
          <subst:element name="body">0003</subst:element>
        </subst:element>
      </subst:defaults>
    
      <xsl:variable name="subst" select="document('')/*/subst:defaults/subst:element" />
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    
      <!-- subst:element outputs a new element with the given @name -->
      <xsl:template match="subst:element">
        <xsl:element name="{@name}">
          <!-- this would also copy any additional "default" attribute! -->
          <xsl:apply-templates select="@*[not(name() = 'name')] | node()"/>
        </xsl:element>
      </xsl:template>
    
      <!-- only stories without any group get "special" treatment -->
      <xsl:template match="story[not(group)]">
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:apply-templates select="$subst[@name = 'group']" mode="copy-or-default" />
          <xsl:apply-templates />
        </xsl:copy>
      </xsl:template>
    
      <!-- a group first outputs all "default" children, then any extra children -->    
      <xsl:template match="group">
        <xsl:variable name="defaults" select="$subst[@name = 'group']/*" />
        <xsl:copy>
          <xsl:apply-templates select="@*" />
          <xsl:apply-templates select="$defaults" mode="copy-or-default">
            <xsl:with-param name="parent" select="." />
          </xsl:apply-templates>
          <!-- any elements that don't have a default AND any non-element children -->
          <xsl:apply-templates select="
            *[not(name() = $defaults/@name)] | node()[not(self::*)]
          " />
        </xsl:copy>
      </xsl:template>
    
      <!-- in "copy-or-default" mode, this checks the context parent
           and either copies the element from there or uses the default --> 
      <xsl:template match="subst:element" mode="copy-or-default">
        <xsl:param name="parent" />
    
        <xsl:choose>
          <xsl:when test="$parent and $parent/*[name() = current()/@name]">
            <xsl:apply-templates select="$parent/*[name() = current()/@name]" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="." />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
    
    </xsl:stylesheet>
    

    when applied to this input

    <x>
      <story>
        <group>
          <section>0001</section>
          <head>0002</head>
          <body>0003</body>
        </group>
        <category>some text</category>
        <summary>some text</summary>
      </story>
      <story>
        <group>
          <body>0004</body>
          <!-- a comment! -->
          <foo>blah</foo>
        </group>
        <category>some text</category>
        <summary>some text</summary>
      </story>
      <story>
        <category>some text</category>
        <summary>some text</summary>
      </story>
    </x>
    

    produces:

    <x>
      <story>
        <group>
          <section>0001</section>
          <head>0002</head>
          <body>0003</body>
        </group>
        <category>some text</category>
        <summary>some text</summary>
      </story>
      <story>
        <group>
          <section>0001</section>
          <head>0002</head>
          <body>0004</body>
          <!-- a comment! -->
          <foo>blah</foo>
        </group>
        <category>some text</category>
        <summary>some text</summary>
      </story>
      <story>
        <group>
          <section>0001</section>
          <head>0002</head>
          <body>0003</body>
        </group>
        <category>some text</category>
        <summary>some text</summary>
      </story>
    </x>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to ensure that the input value contains at least one dot, so
I need to ensure that any number of websites and services haven't bitten the
I need to ensure that a given field does not have more than one
I have a Queue object that I need to ensure is thread-safe. Would it
So I need to open an XML document, write to it and then save
In my game, I need to ensure that angles do not exceed 2 pi.
I am in the US and have Irish client. I need to ensure that
I need to build a proxy (maybe a bad description) that receives an XML
I need to ensure that tables are not dropped from my database. Should I..
I need to ensure 2 calls are enlisted in a transaction Sub InsertOrder(order) ...insert

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.