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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:12:50+00:00 2026-05-18T11:12:50+00:00

Edited in a feeble attempt to clarify. The system converts XML into another XML

  • 0

Edited in a feeble attempt to clarify. The system converts XML into another XML via the XSL processor.

I’m doing some XSL rule stuff for a machine that has plug-in cards. The cards will supply their own XSL rules to the main host, which in turn will colate these into one super-XSL file to be processed by itself, and also sent over to web browsers who’ve HTTP’d onto the machine. The browser is used to set configuration items for the machine, and the XSL is there to change and/or hide and/or unhide some of the XML. To simplify my query here, I’ve invented a small example.

When a user configures an item to be of type B, (a choice of several such as A, B, C, …), then the next two items are to be changed (in some way) too. In the real deal, there’s an attribute “hidden” set to true or false, and also child elements which are set. The next two items in the real thing are to be hidden, and also child elements changed.

When the user changes the item from a type B to say a type A, then I need to work out which other nodes need to have their ‘hidden’ attribute set to false. The user will change the child elements as they see fit.

All of this is “circular”, and ordered, so if a node is set to type B, and it’s the last node (no following-sibling’s) then the affected nodes are the first in the set. (In XPath terms, if node[4] is type B, then nodes [1] and [2] are to be hidden and changed).

So, for my example here, I have as an input XML:

<topline>
  <midline type="A" name="mid1" hidden="false"><source name="off"/></midline>
  <midline type="B" name="mid2" hidden="false"><source name="input 1"/></midline>
  <midline type="A" name="mid3" hidden="false"><source name="off"/></midline>
  <midline type="A" name="mid4" hidden="false"><source name="off"/></midline>
</topline>

and the XSL will change this to:

<topline>
  <midline type="A" name="mid1" hidden="false"><source name="off"/></midline>
  <midline type="B" name="mid2" hidden="false"><source name="input 1"/></midline>
  <midline type="A" name="mid3" hidden="true"><source name="input 1"/></midline>
  <midline type="A" name="mid4" hidden="true"><source name="input 1"/></midline>
</topline>

Now if the user changes his mind, and changes mid2 to be a type A:

<topline>
  <midline type="A" name="mid1" hidden="false"><source name="off"/></midline>
  <midline type="A" name="mid2" hidden="false"><source name="input 1"/></midline>
  <midline type="A" name="mid3" hidden="true"><source name="input 1"/></midline>
  <midline type="A" name="mid4" hidden="true"><source name="input 1"/></midline>
</topline>

then the XSL will unhide the circular following siblings of mid2, so result should be:

<topline>
  <midline type="A" name="mid1" hidden="false"><source name="off"/></midline>
  <midline type="A" name="mid2" hidden="false"><source name="input 1"/></midline>
  <midline type="A" name="mid3" hidden="false"><source name="input 1"/></midline>
  <midline type="A" name="mid4" hidden="false"><source name="input 1"/></midline>
</topline>

It is this second step I am struggling with. What I’ve done to solve it is, to me, rather ugly but perhaps unavoidable given what I’m trying to achieve isn’t really XSL friendly.

What I’ve landed up doing :

<xsl:for-each select="topline">
  <xsl:for-each select="midline">
    <xsl:variable name="masterPosition" select="position()"/>
    <xsl:choose>
      <xsl:when test="@type='B'>
        hide the next two nodes. This is easy: 
            translate($masterPosition, '1234', '2341')
        works nicely.
      </xsl:when>
      <xsl:otherwise>
          <xsl:variable name="prior1" select="translate(masterPosition, '1234', '4123')"/>
          <xsl:variable name="test1" select="../midline[$prior1]/source[1]/@name='off'"/>
          this second line doesn't work: I only get the first node, always. 
          So instead I have 
          <xsl:variable name="test2">
              <xsl:choice>
                  <xsl:when test="$masterPosition='1'"><xsl:value-of select="../midline[4]/source"/></xsl:when>
                  and so on for the other masterPositions
              </xsl:choice>
          </xsl:variable>
          and this is repeated for a few other variables, one each for each relevant
          prior position and for the fields I need to change. I then use these
          variables to change the XML - there's something in the main machine's
          processing to enable this, I believe it is non-standard, so please ignore: 
          (At least it doesn't run against Xalan).
          <set key={$test2}/@hidden, value="false")/>             
      </xsl:otherwise>
    </xsl:choose>
  <xsl:for-each>
</xsl:for-each>

Is there a more elegant way of doing this that you can think of? If not, don’t worry, I believe my hack will work, and also shouldn’t consume too many MIPS.

I can’t use <xsl:key> as our system doesn’t cope: we have multiple XSL sources which get colated into one, and the colation script (out of my control) simply doesn’t understand <xsl:key>, so if there is a solution using keys, I can’t use it.

Thanks
Richard

  • 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-18T11:12:51+00:00Added an answer on May 18, 2026 at 11:12 am

    Here’s one way to do what you want, without keys, based on template matching:

    <!-- start with the identity transform -->
    <xsl:template match="node()|@*">
      <xsl:copy>
        <xsl:apply-templates select="node()|@*" />
      </xsl:copy>
    </xsl:template>
    
    <!-- <midline>s that have two preceding siblings -->
    <xsl:template match="midline[@type='A'][count(preceding-sibling::midline) &gt; 1]">
      <xsl:apply-templates select="." mode="source">
        <xsl:with-param name="circular" select="
          preceding-sibling::midline[position() &lt;= 2]
        " />
      </xsl:apply-templates>
    </xsl:template>
    
    <!-- <midline>s that have one preceding sibling -->
    <xsl:template match="midline[@type='A'][count(preceding-sibling::midline) = 1]">
      <xsl:apply-templates select="." mode="source">
        <xsl:with-param name="circular" select="
          (preceding-sibling::midline|../midline[last()])
        " />
      </xsl:apply-templates>
    </xsl:template>
    
    <!-- <midline>s that have no preceding sibling -->
    <xsl:template match="midline[@type='A'][count(preceding-sibling::midline) = 0]">
      <xsl:apply-templates select="." mode="source">
        <xsl:with-param name="circular" select="
          ../midline[position() &gt; last() - 2]
        " />
      </xsl:apply-templates>
    </xsl:template>
    
    <!-- this template creates a new node with the wanted source -->
    <xsl:template match="midline" mode="source">
      <xsl:param name="circular" select="." />
      <xsl:variable name="prevB" select="$circular[@type = 'B' and not(@mode = off)]" />
      <xsl:copy>
        <xsl:copy-of select="@*" />
        <xsl:attribute name="hidden">
          <xsl:value-of select="boolean($prevB)" />
        </xsl:attribute>
        <xsl:choose>
          <xsl:when test="$prevB">
            <xsl:copy-of select="$prevB/source" />
          </xsl:when>
          <xsl:otherwise>
            <xsl:copy-of select="source" />
          </xsl:otherwise>
        </xsl:choose>
      </xsl:copy>
    </xsl:template>
    

    For your sample input, the output would be:

    <topline>
      <midline type="A" name="mid1" hidden="false"><source name="off"/></midline>
      <midline type="B" name="mid2" hidden="false"><source name="input 1"/></midline>
      <midline type="A" name="mid3" hidden="true"><source name="input 1"/></midline>
      <midline type="A" name="mid4" hidden="true"><source name="input 1"/></midline>
    </topline>
    

    Note that your description contradicts your sample output – this is what you would get when strictly following your description, unless I misunderstood you.

    The <xsl:with-param> is used to pass in the two nodes that are the “circular preceding-siblings” (or the empty node-set in case of @type=B' nodes).

    The template <xsl:template match="midline" mode="source"> uses that parameter: if it is there, it checks your condition (i.e. one of them is @type='B' and it is not @mode='off').

    If such a node exists, it copies its <source>, else it copies the original <source>.

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

Sidebar

Related Questions

This is a sample (edited slightly, but you get the idea) of my XML
Problem: edited files on windows, using git-bash, to fix IE7 problems committed, pushed to
I edited the question after David Hanak's answer (thanks btw!). He helped with the
I have HTML code edited by FCKEditor stored in a database and would like
How do I load the edited .emacs file without restarting Emacs?
I have a JavaScript resource that has the possibility of being edited at any
Here is an example of polymorphism from http://www.cplusplus.com/doc/tutorial/polymorphism.html (edited for readability): // abstract base
My VB.Net Winforms app is a tool to allow hierarchical data to be edited
I'd like to write a plugin that does something with the currently edited file
I've always just FTPed files down from sites, edited them and put them back

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.