Help with variable substitution using XSL

S

Scott

I have an XML Document in a format like:

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>


I am fairly new to doing transformations with XSLT, but so far I
haven't had luck in solving this problem. All I would like to do is
create a new XML output file where the variable declarations are gone,
and the actual values (like ABCDEFG) substituted for the names ("Bob")
for the "parameters." So basically I want the output to be:

<Function id="1">
<Parameter type="String">ABCDEFG</Parameter>
<Parameter type="String">QWERTYUI</Parameter>
</Function>

<Function id="2">
<Parameter type="String">POIUYTR</Parameter>
<Parameter type="String">ZXCVBNM</Parameter>
</Function>

I tried some of the code samples I found on the net regarding find and
replace routines, but I wasn't able to adapt them to this particular
problem.

Thanks in advance!
 
M

Martin Honnen

Scott said:
I have an XML Document in a format like:

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>
So basically I want the output to be:

<Function id="1">
<Parameter type="String">ABCDEFG</Parameter>
<Parameter type="String">QWERTYUI</Parameter>
</Function>

<Function id="2">
<Parameter type="String">POIUYTR</Parameter>
<Parameter type="String">ZXCVBNM</Parameter>
</Function>

If I correct your XML to have root element e.g.

<?xml version="1.0" encoding="UTF-8"?>
<root>

<Variable name="Bob">ABCDEFG</Variable>
<Variable name="Steve">QWERTYUI</Variable>
<Variable name="John">POIUYTR</Variable>
<Variable name="Tim">ZXCVBNM</Variable>

<Function id="1">
<Parameter type="String">Bob</Parameter>
<Parameter type="String">Steve</Parameter>
</Function>

<Function id="2">
<Parameter type="String">John</Parameter>
<Parameter type="String">Tim</Parameter>
</Function>

</root>

it is possible with the following XSLT which does start with the
identity transformation and adds two templates, one to make sure the
<Variable> elements are removed and the second to replace the content of
<Parameter> elements as needed:

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

<xsl:eek:utput method="xml"
encoding="UTF-8" />

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

<xsl:template match="Variable" />

<xsl:template match="Parameter">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:value-of select="/root/Variable[@name = current()]" />
</xsl:copy>
</xsl:template>

</xsl:stylesheet>

It might need some fine-tuning to not copy white space text nodes.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top