Meow
Meow

Reputation: 89

xslt:1.0 - Using xslt, how to rename the element name and strip blank spaces correctly

This is my XML-

<?xml version="1.0" encoding="UTF-8"?>
<update-this>
<document name="http://blah">
<amount>015-39</amount>
<shipping>US::Ground:0.00</shipping>
<c:karoke:string>abcabc</c:karoke:string>
<size>11.5 D</size>
<price>100.00 USD</price>
<stock>present in stock </stock>
</document>
</update-this>

This is how I want my final XML-

<?xml version="1.0" encoding="UTF-8"?>
<document name="http://blah">
<amount>015-39</amount>
<shipping>US::Ground:0.00</shipping>
<karoke>abcabc</karoke>
<size>11.5 D</size>
<price>100.00 USD</price>
<stock>present in stock </stock>
<size_stock>11.5D_presentinstock</size_stock>
<size_present>11.5D_present>
<discounted_price>90 USD</discounted_price>
<increased_price>110 USD</increased_price>
</document>

This is the XSLT-

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

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


    <xsl:template match="document">
        <xsl:variable name="price" select="substring-before(normalize-space(price),' ')"/>
        <xsl:variable name="currency" select="substring-after(normalize-space(price),' ')"/>
        <xsl:variable name="difference" select="number($price) * .10"/>
        <xsl:variable name="size">
        <xsl:value-of select="size"/>
        </xsl:variable>
        <xsl:variable name="stock">
         <xsl:value-of select="stock"/>
         </xsl:variable>
         <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <discounted_price><xsl:value-of select="concat(format-number($price - $difference,'#,###.00'),' ',$currency)"/></discounted_price>
            <increased_price><xsl:value-of select="concat(format-number($price + $difference,'#,###.00'),' ',$currency)"/></increased_price>
            <size_stock> <xsl:value-of select="concat($size,'_',$stock)" /></size_stock>
            <size_present><xsl:value-of select="concat($size,'_','present')" /></size_present>
        </xsl:copy>
         <xsl:choose>
        <xsl:when test="starts-with(name(), 'c:karoke:string')">
            <xsl:element name="karoke">
                <xsl:apply-templates select="@*|node()"/>
            </xsl:element>
        </xsl:when>
        <xsl:otherwise>
           <xsl:copy>
              <xsl:apply-templates select="@*|node()" />
           </xsl:copy>
        </xsl:otherwise>
    </xsl:choose>
        </xsl:template>

 <xsl:template match="size_stock | size_present">
    <xsl:copy>
        <xsl:value-of select="translate(., ' ', '')"/>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>

I am using XSLT 1.0, my XML has multiple document element. Why is the rename of the element c:karoke:string to just karoke not working? Also, the blank spaces in size_stock and size_present are not getting removed. Can you please help me?

Upvotes: 0

Views: 104

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117100

When you do:

 <xsl:when test="starts-with(name(), 'c:karoke:string')">

you are in the context of:

<xsl:template match="document">

Therefore the name() function will return the string "document" which does not start with 'c:karoke:string' so this test will never return true.

Upvotes: 1

Related Questions