473,500 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible with XSLT? *seems* easy

Hi there,

Is the following possible with XSLT? Given the following example XML
docs:

<!-- doc 1-->
<user>
<username>myUsername</username>
<password></password>
<phone>12345</phone>
</user>

<!-- doc2 -->
<user>
<title>Mr</title>
<username>myUsername</username>
<password></password>
<phone>12345</phone>
<address>some st</address>
</user>

transform doc 1 into

<user>
<username>myUsername</username>
<password>PASSWORD MISSING</password>
<phone>12345</phone>
</user>

transform doc 2 into

<user>
<title>Mr</title>
<username>myUsername</username>
<password>PASSWORD MISSING</password>
<phone>12345</phone>
<address>some st</address>
</user>

So what I want to do is modify some elements but reproduce others "as
is". I do not know what the input elements may be but I do know that
the XPath for the elements to be modified/tested will not change. I am
thinking of XSLT, pseudo, like:

<xsl:template match="/">
<xsl:for-each select="/">
<xsl:choose>
<xsl:when test="/user/password is empty">
<password>PASSWORD MISSING</password>
</xsl:when>
<xsl:otherwise>
<!-- this is the bit I'm not sure about -->
just output the element and value as is
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Am I mad?

Cheers

Lord0

Oct 19 '06 #1
4 1487
Standard approach: Start with the "identity transform", which passes a
document through unchanged, then add templates that recognize the
exceptions and process them appropriately.

This should be the first technique any XSLT programmer learns, because
it's the single most useful design pattern in the language.

A few examples can be found at
http://www.dpawson.co.uk/xsl/sect2/identity.html
Oct 19 '06 #2

Lord0 wrote:
Is the following possible with XSLT?
Have you tried doing it?
<user>
<username>myUsername</username>
<password></password>
<phone>12345</phone>
</user>

transform doc 1 into

<user>
<username>myUsername</username>
<password>PASSWORD MISSING</password>
<phone>12345</phone>
</user>

So what I want to do is modify some elements...
So create a template that matches the elements you want to
modify -- and, well, modify them.
...but reproduce others "as is".
Google identity transformation.
I do not know what the input elements may be but I do
know that the XPath for the elements to be
modified/tested will not change.
You probably should've spent half an hour or so reading
about XSLT.
I am thinking of XSLT, pseudo, like:

<xsl:template match="/">
<xsl:for-each select="/">
Nah, that's wrong in more than one way.
<xsl:choose>
<xsl:when test="/user/password is empty">
<password>PASSWORD MISSING</password>
</xsl:when>
<xsl:otherwise>
<!-- this is the bit I'm not sure about
-->
just output the element and value as is
You should've tried reading an XSLT reference. There are
quite a few of those around, and it's all in there.
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- identity (of sorts) -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- exception -->
<xsl:template match="/user/password[.='']">
<xsl:copy>
<xsl:text>Password missing.</xsl:text>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Figuring out how and why it works is left as a (potentially
enlightening) exercise for the reader.

--
roy axenov

Oct 19 '06 #3
thanks guys - "identity transform" seems just the ticket.

Believe it or not I did go through the following "tutorial"
http://www.w3schools.com/xsl/default.asp but it didn't mention the
identity trnasfrom technique at all!

Oct 19 '06 #4
Lord0 wrote:
Believe it or not I did go through the following "tutorial"
Haven't looked at that one, but I'd bet IBM's tutorials are better.
(Standard plug for http://www.ibm.com/xml, with standard admission that
I'm biased.)

--
Joe Kesselman /IBM/Datapower/Apache troubleshooter/W3C troublemaker
Oct 19 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
777
by: PHILIPPE | last post by:
Hi, I'm facing a annoying problem. In the old ASP times, I was able to construct dynamically an XSL file and then apply it to my XML file to create the HTML output. Today, with the .NET...
6
2719
by: Pete | last post by:
I am just getting to grips with XML and I was wondering if you could help me with something that no-one seems able or willing to help with.. I have an XSLT file which should be transforming a...
4
1625
by: titanandrews | last post by:
Hi, I have ran into a situation that I think should be possible, but I am fairly new to XSLT so maybe not. Suppose I have the following document <ROOT> <FOO name="A"> <CHILD name="B"/>...
2
1414
by: Stephen | last post by:
I have a little predicamnet. I have two xml controls on my page. Both are menus for my site. The first (Category) needs to set the data source of the other xml control. To do this I created an...
1
2018
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
2
1952
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
18
2043
by: yinglcs | last post by:
Hi, I have a newbie XSLT question. I have the following xml, and I would like to find out the children of feature element in each 'features' element. i.e. for each <featuresI would like to...
5
2892
by: Simon Brooke | last post by:
This is supposed to be a very simple XSL stylesheet to strip styling information out of HTML documents - it could not be more basic. And yet, it doesn't work. I'm obviously getting something very...
2
22747
jkmyoung
by: jkmyoung | last post by:
Here's a short list of useful xslt general tricks that aren't taught at w3schools. Attribute Value Template Official W3C explanation and example This is when you want to put dynamic values...
0
7136
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7018
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7183
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7235
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5491
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4923
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3110
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3108
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
317
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.