XSLT question to add element to a particular xpath...

Hi I want to achieve the following:

I have the below schema:

<?xml version="1.0" encoding="iso-8859-1"?>
<database>
    <rad>
        <timeout> 45 </timeout>
    </rad>
    <tac>
        <timeout> 70 </timeout>
    </tac>    
</database>

1) Using XSLT I want to check if /database/rad/timeout value is greater than 30(in this case its 45), change it to 30.

2) Add new tags as follows:

<warnings>
  <warning>Time out of RAD changed.</warning>
</warnings>
So the output XML should contain the following:-

    <?xml version="1.0" encoding="iso-8859-1"?>
    <database>
        <rad>
            <timeout> 45 </timeout>
        </rad>
        <tac>
            <timeout> 70 </timeout>
        </tac>
     <warnings>
      <warning>Time out of RAD changed.</warning>
    </warnings>  
    </database>
There are a lot of such conditions that could be there. I was able to do the first part:-

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="/database/rad/timeout">
        <timeout>
                <xsl:choose>
                    <xsl:when test=". > 30">30</xsl:when>
                    <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
                </xsl:choose>
        </timeout>
    </xsl:template>
        <!-- ignore text content of nodex -->
    <xsl:template match="text()" />
    </xsl:stylesheet>

Basically I want to log some of the changes to the error/warning node depending upon the changes. Can some one help ?
tpatAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Geert BormansInformation ArchitectCommented:
I am not 100 % certain what you mean with "has changed",
but I restructured your stylesheet a bit and added what I thought you needed

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xslt="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- ignore text content of nodex -->
    <xsl:template match="text()" />
    
    <xsl:template match="/database/rad/timeout[normalize-space(.) > 30]">
        <xsl:copy>
            <xsl:text>30</xsl:text>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/database/rad/timeout[not(normalize-space(.) > 30)]">
        <xsl:copy>
            <xsl:value-of select="."/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="database">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
            <xsl:if test="normalize-space(tac/timeout) > normalize-space(rad/timeout)">
                <warnings>
                    <warning><xsl:text>Time out of RAD changed.</xsl:text></warning>
                </warnings>
            </xsl:if>
        </xsl:copy>
    </xsl:template>
 
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates select="node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

Open in new window

0
tpatAuthor Commented:
Hi Geert,

Thanks. That definitely helps. Sorry if i was not clear. Basically i want to give users the ability to add "warnings" and "errors" if something changed(for eg in this case timeout changed). Can you think of some way in which we can just pass in the message to a template and it adds it to the /database/warnings xpath without much changes to existing XSLT since its huge with different templates.

So it could be:
<warnings>
 <warning>Timeout changed </warning>
<warning>Content got deleted</warning
<warning>Content got added</warning>
</warnings>

Thanks.
0
Geert BormansInformation ArchitectCommented:
Well, the changes I made can be included in a bigger stylesheet with no problems,
I just showed you a way to have conditionals without choose
In your bigger stylesheet just replace
 <xsl:template match="/database/rad/timeout">
with the two
 <xsl:template match="/database/rad/timeout[...]">
and the result would be equal
(I recommend you do because it is a more flexible style)

Now about the actual change you want...
I am not sure what you mean with "pass in"
You can simply call a named template, you can pass parameters when applying templates
you want to give "users" the possibility... who are the users?

You are not giving enough information, really.
Your questiosn are not at all clear, simply because you imply we know what you are doing,
you need to give at least a bit of background
Is this generating a user interface? Are users the developers that use your stylesheet in a different setting... context please
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

tpatAuthor Commented:
Sorry for  the confusion. Yeah, the users are developers who use the stylesheet. I have attached the stylesheet. Basically there are various templates and when something is changed in the template i.e. added/removed, i want to log those changes without rewriting/changing much of existing stuff since there are a lot of such templates in the whole file plus there are many files. So ii want to add those changes as warnings or error elements and then I can retrieve it using xpath. Let me know if there are questions. Thanks.
test.txt
0
Geert BormansInformation ArchitectCommented:
so you need a change tracking methodology for XSLT stylesheets?
But you want to log the stylesheet change in the document?

It might be smarter to use SVN or Github to register the changes,
or ask the developers/users to put a specific processing instruction before and after each change

You could use Delta XML to track the differences,
Automation would seriously improve the roustness of your solution
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tpatAuthor Commented:
Unfortunately this is a config file and the style sheet is applied to input XML file depending on the version on the fly in a customer production setup.
0
Geert BormansInformation ArchitectCommented:
I still don't get it entirely, your descriptions are really vague,
please note you have the system in your mind, we don't

So please be very precise in what you have and what you need

I have given you code to add a warning message based on a condition from  the context of the database. That should give you something to work from.
If not, please describe the process very carefully so an outstander can understand what exactly you want.
After that, I can help you without waisting time in an on and off discussion
0
Geert BormansInformation ArchitectCommented:
But if you are adding messages as you are, the <database> context is the place to start
0
Geert BormansInformation ArchitectCommented:
I think I once have done something that could help you
I have created a register outside the XSLT (java extension function to saxon, so it would be browser depending) I used that for error logging
Allowing to call a function from each and every template, which registers a message, and merge the register after transformation conclusion
You could use xsl:message for that actually if your XSLT processor picks it up right

But all of this is shooting in the dark.

Don't forget to mention your specific processor in your description
0
tpatAuthor Commented:
Yeah, that was what i had initially, Basically parse the messages and was working. However I was trying to figure out an alternative. Currently I am thinking that I will add warning/error elements inline wherever needed and then in the code(using libxslt) unlink the warning/error nodes once I get the messaged. Thanks for the help though.
0
Geert BormansInformation ArchitectCommented:
Ah, seems I grasped at least some of the context in the end.

The more robust solution could be a register (maybe hook in a small repository for that)
If you are getting some C programming you could develop an extension function to the libxslt, that could take care of that... very solid that would be
0
tpatAuthor Commented:
Yeah, Let me give that a thought. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.