Reputation: 8050
I'm still learning XSL and I'm trying to change the value I'm getting from the XSL to another value. (I'm using an online website to convert my XML to another XML using XSL)
XML
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<Student>
<Info Name="Jen" Age="20" Class="C" />
</Student>
<Student>
<Info Name="Sam" Age="21" Class="B" />
</Student>
</Person>
lets say there are many students
(with same name even)
XSLT
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="Person">
<Person>
<lookuptable>
<name first="Jen" ori="Jenny" />
<name first="Sam" ori="Sammy" />
</lookuptable>
<xsl:for-each select="Student">
<Info Name="{Info/@Name}" Age="{Info/@Age}" Class="{Info/@Class}" />
</xsl:for-each>
</Person>
</xsl:template>
</xsl:stylesheet>
i created a lookuptable
the output is
<?xml version="1.0" encoding="UTF-8"?>
<Person>
<lookuptable>
<name ori="Jenny" first="Jen" />
<name ori="Sammy" first="Sam" />
</lookuptable>
<Info Class="C" Age="20" Name="Jen" />
<Info Class="B" Age="21" Name="Sam" />
</Person>
i want to change my output, meaning each time "Jen" appears i want it to be "Jenny" etc. using my lookuptable. how can i achieve this? or is it easier to create another XSL to convert output (the last XML) to the requirement i need? thanks in advance..
Upvotes: 1
Views: 1938
Reputation: 163595
Here is the XSLT 2.0 version of @halfbit's answer (I haven't copied the lookuptable to the output, I assumed that wasn't really wanted):
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:variable name="lookuptable" as="element(name)*">
<name first="Jen" ori="Jenny" />
<name first="Sam" ori="Sammy" />
</xsl:variable>
<xsl:template match="Person">
<Person>
<xsl:for-each select="Student">
<xsl:variable name="key" select="Info/@Name"/>
<Info Age="{Info/@Age}" Class="{Info/@Class}">
<xsl:attribute name="Name" select="$lookuptable[@first=$key]/@ori"/>
</Info>
</xsl:for-each>
</Person>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 3464
You can put the lookup table in a variable and use it via the node-set extension. I tried it on http://www.xsltcake.com/
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
xmlns:exsl="http://exslt.org/common">
<xsl:variable name="lookuptable">
<lookuptable>
<name first="Jen" ori="Jenny" />
<name first="Sam" ori="Sammy" />
</lookuptable>
</xsl:variable>
<xsl:template match="Person">
<Person>
<xsl:copy-of select="$lookuptable"/>
<xsl:for-each select="Student">
<xsl:variable name="key" select="Info/@Name"/>
<Info Age="{Info/@Age}" Class="{Info/@Class}">
<xsl:attribute name="Name">
<xsl:value-of select="exsl:node-set($lookuptable)/lookuptable/name[@first=$key]/@ori"/>
</xsl:attribute>
</Info>
</xsl:for-each>
</Person>
</xsl:template>
</xsl:stylesheet>
For details on this see Pavel Minaev's answer to XSL: How best to store a node in a variable and then us it in future xpath expressions? It also explains that you will not need the exsl extension when using XSLT 2. So you might want to refine my approach for XSLT 2.
Upvotes: 1