Re: XML node replacement
[email protected] Sat, 18 Aug 2007 02:45:07 +0100
| Newsgroups | gmane.text.xml.xslt.extensions |
|---|---|
| Message-ID | <OFDC6C3F42.2E38B5AC-ON8025733B.000846D0-8025733B.00099FD0@bnpparibas.com> |
Here's one way to do it (see below).
Best regards,
HMcA
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output encoding="utf-8" />
<!-- start processing ... -->
<xsl:template match = "/">
<xsl:apply-templates />
</xsl:template>
<!-- convert an instance of "node1" to an element named by its
"doc_var_name" attribute -->
<xsl:template match = "node1">
<xsl:element name = "{@doc_var_name}">
<!-- just copy the content, including attributes -->
<xsl:copy-of select = "@*|*" />
</xsl:element>
</xsl:template>
<!--
copy an instance of "test" to the output, converting only its
immediate node1 children,
and collecting nested instances of node1 within a "children"
container
-->
<xsl:template match = "test">
<xsl:copy>
<xsl:apply-templates select = "node1" />
<xsl:element name = "children">
<!-- wildcard lets us access nested instances of "node1"
without naming their containing elements (child1, child2, ...) -->
<xsl:apply-templates select = "*/node1" />
</xsl:element>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
|---------------------------------------->
| Internet |
| [email protected] |
| |
| |
| Sent by: |
| [email protected]|
| t.com |
| |
| 16/08/2007 21:48 |
|---------------------------------------->
>---------------------------------------------------------------------------------------------------------------|
| |
| |
| To|
| exslt |
| cc|
| |
| Subject|
| [exslt] XML node replacement |
| |
| |
| |
| |
| |
| |
>---------------------------------------------------------------------------------------------------------------|
Hi, I am a newbie to XSL and require help. I have two XMLs (see below) and
must convert old to new .
To summarize:
1. I have to replace all node1 with the attribute value for doc_var_name
i.e. var1 (including those occuring within <test> and <child1><chil2> etc)
2. I have to remove all child1, child2 nodes and replace them with one
instance of <children>.
I am unable to figure this one out.
Thanks!
Terry
-----------------------------------
old format:
----------------
<test>
<node1 doc_var_name="var1>
<a>1</a>
</node1>
<child1>
<node1 doc_var_name="var2>
<b>6</b>
</node1>
</child1>
<child2>
<node1 doc_var_name="var3>
<c>3</c>
</node1>
</child2>
</test>
required new format:
-----------------------------
<test>
<var1 doc_var_name="var1">
<a>1</a>
</var1>
<children>
<var2 doc_var_name="var2">
<b>7</b>
</var2>
<var3 doc_var_name="var3">
<c>3</c>
</var3>
</children>
</test>
_______________________________________________
exslt mailing list
[email protected]
http://www.exslt.org/list
This message and any attachments (the "message") is intended solely for the addressees and is confidential.
If you receive this message in error, please delete it and immediately notify the sender. Any use not in accord with
its purpose, any dissemination or disclosure, either whole or partial, is prohibited except formal approval.
The internet can not guarantee the integrity of this message.
BNP PARIBAS (and its subsidiaries) shall (will) not therefore be liable for the message if modified.
Do not print this message unless it is necessary, consider the environment.
---------------------------------------------
Ce message et toutes les pieces jointes (ci-apres le "message") sont etablis a l'intention exclusive de ses destinataires et sont confidentiels. Si vous recevez ce
message par erreur, merci de le detruire et d'en avertir immediatement l'expediteur.
Toute utilisation de ce message non conforme a sa destination, toute diffusion ou toute publication, totale ou partielle, est interdite, sauf autorisation expresse.
L'internet ne permettant pas d'assurer l'integrite de ce message, BNP PARIBAS (et ses filiales) decline(nt) toute responsabilite au titre de ce message, dans l'hypothese ou il aurait ete modifie.
N'imprimez ce message que si necessaire, pensez a l'environnement.