Narabhut
Narabhut

Reputation: 837

XML Key/Value manipulation using XSLT

I have data like this -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <key>{Salary}</key>
    <value>1000</value>
</item>

I want my output to look like this -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <Salary>1000</Salary>
</item>

Edit, instead of just a value, if I had other tags only one of which is guaranteed to be nonempty like so, what is wrong with my transform? I'm using Sean's XSLT 1.0 transform as a source.

Input -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <key>{Salary}</key>
    <value />
    <value2>1000</value2>
    <value3 />
</item>

Desired output -

<item>
    <name>Bob</name>
    <fav_food>pizza</fav_food>
    <Salary>1000</Salary>
</item>

My current transform -

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="key">
<xsl:element name="{substring-before(substring-after(.,'{'),'}')}"> 
    <xsl:choose>
        <xsl:when test="value != ''">
            <xsl:value-of select="following-sibling::value" />
        </xsl:when>
        <xsl:when test="value2 != ''">
            <xsl:value-of select="following-sibling::value2" />
        </xsl:when>
        <xsl:when test="value3 != ''">
            <xsl:value-of select="following-sibling::value3" />
        </xsl:when>
        </xsl:choose>
    </xsl:element> 
</xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 278

Answers (1)

Sean B. Durkin
Sean B. Durkin

Reputation: 12729

An XSLT 1.0 solution ...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="value" />

<xsl:template match="key">
 <xsl:element name="{substring-before(substring-after(.,'{'),'}')}"> 
   <xsl:value-of select="following-sibling::value" /> 
 </xsl:element> 
</xsl:template>

</xsl:stylesheet>

update

Here also is an XSLT 2 solution. This is untested.

<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions">
<xsl:output indent="yes" omit-xml-declaration="yes" />
<xsl:strip-space elements="*" />  

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

<xsl:template match="attribute()|text()|comment()|processing-instruction()">
  <xsl:copy/>
</xsl:template>

<xsl:template match="value" />

<xsl:template match="key">
 <xsl:element name="{fn:replace(.,'^\{(.*)\}$','$1')}"> 
   <xsl:value-of select="following-sibling::value" /> 
 </xsl:element> 
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions