Help with variable substitution using XSL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Scott

    #1

    Help with variable substitution using XSL

    I have an XML Document in a format like:

    <Variable name="Bob">ABCD EFG</Variable>
    <Variable name="Steve">QW ERTYUI</Variable>
    <Variable name="John">POI UYTR</Variable>
    <Variable name="Tim">ZXCV BNM</Variable>

    <Function id="1">
    <Parameter type="String">B ob</Parameter>
    <Parameter type="String">S teve</Parameter>
    </Function>

    <Function id="2">
    <Parameter type="String">J ohn</Parameter>
    <Parameter type="String">T im</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">A BCDEFG</Parameter>
    <Parameter type="String">Q WERTYUI</Parameter>
    </Function>

    <Function id="2">
    <Parameter type="String">P OIUYTR</Parameter>
    <Parameter type="String">Z XCVBNM</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!
  • Martin Honnen

    #2
    Re: Help with variable substitution using XSL



    Scott wrote:
    [color=blue]
    > I have an XML Document in a format like:
    >
    > <Variable name="Bob">ABCD EFG</Variable>
    > <Variable name="Steve">QW ERTYUI</Variable>
    > <Variable name="John">POI UYTR</Variable>
    > <Variable name="Tim">ZXCV BNM</Variable>
    >
    > <Function id="1">
    > <Parameter type="String">B ob</Parameter>
    > <Parameter type="String">S teve</Parameter>
    > </Function>
    >
    > <Function id="2">
    > <Parameter type="String">J ohn</Parameter>
    > <Parameter type="String">T im</Parameter>
    > </Function>
    >
    >[/color]
    [color=blue]
    > So basically I want the output to be:
    >
    > <Function id="1">
    > <Parameter type="String">A BCDEFG</Parameter>
    > <Parameter type="String">Q WERTYUI</Parameter>
    > </Function>
    >
    > <Function id="2">
    > <Parameter type="String">P OIUYTR</Parameter>
    > <Parameter type="String">Z XCVBNM</Parameter>
    > </Function>[/color]

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

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

    <Variable name="Bob">ABCD EFG</Variable>
    <Variable name="Steve">QW ERTYUI</Variable>
    <Variable name="John">POI UYTR</Variable>
    <Variable name="Tim">ZXCV BNM</Variable>

    <Function id="1">
    <Parameter type="String">B ob</Parameter>
    <Parameter type="String">S teve</Parameter>
    </Function>

    <Function id="2">
    <Parameter type="String">J ohn</Parameter>
    <Parameter type="String">T im</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:styleshe et
    xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:output 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="Paramete r">
    <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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/

    Comment

    Working...