Possible with XSLT? *seems* easy

L

Lord0

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:eek:therwise>
<!-- this is the bit I'm not sure about -->
just output the element and value as is
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>

Am I mad?

Cheers

Lord0
 
J

Joseph Kesselman

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
 
R

roy axenov

Lord0 said:
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:eek:therwise>
<!-- 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:eek:therwise>
</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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top