XSLT: small changes to xml from XPath

K

Karsten

Hello

How do I copy most of a xml file, but change a few attributes and/or
elements based on a xpath?
How do I make the xslt file?


I know this description isn't much, but here is a small example

<root>
...
<element atr="value"/>
...
</root>

to

<root>
...
<element atr="*new value*"/>
...
</root>
 
J

Joseph Kesselman

Karsten said:
How do I copy most of a xml file, but change a few attributes and/or
elements based on a xpath?
How do I make the xslt file?

Start with the XSLT "Identity Transform" -- which you can get from the
XSLT spec or from any good XSLT tutorial.

Write an additional template which recognizes the node you want to
alter, and which constructs the new result.
 
G

George Bina

Like below:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="node() | @*">
<xsl:copy>
<xsl:apply-templates select="node() | @*"/>
</xsl:copy>
</xsl:template>

<xsl:template match="element">
<xsl:copy>
<xsl:attribute name="atr">*new value*</xsl:attribute>
<xsl:apply-templates select="node|@*[not(name()='atr')]"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Best Regards,
George
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top