Jonathan Römer
Jonathan Römer

Reputation: 628

XML transform with XSLT 1

today im really struggling with XSLT, it has been a long time that i had to use it. I have to edit some xml and i can't use XSLT 2.0. So i have to use 1.0. The xml im struugling with is (basic example) :

I tried making an template for the 2 nodes, and then 'call' that template to create a new node with the desired values but that didnt work either , im missing something if someone can point me into the right direction.

<messagemap>
    <author>
        <au_id>274-80-9391</au_id>
        <au_lname>Straight</au_lname>
        <au_fname>Dean</au_fname>
        <phone>415 834-2919</phone>
        <address>5420 College Av.</address>
        <city>Oakland</city>
        <state>CA</state>
        <zip>94609</zip>
        <contract>1</contract>
    </author>
</messagemap>

XM:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output indent="yes"/>
    <!--Identity Transform.-->
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
   
    <xsl:template match="au_fname | au_lname">
        <company>
            <xsl:value-of select="."/>
        </company>
    </xsl:template>    
</xsl:stylesheet>

What i get as a result:

<messagemap>
    <author>
        <au_id>274-80-9391</au_id>
        <company>Straight</company>
        <company>Dean</company>
        <phone>415 834-2919</phone>
        <address>5420 College Av.</address>
        <city>Oakland</city>
        <state>CA</state>
        <zip>94609</zip>
        <contract>1</contract>
    </author>
</messagemap>

What i need is:

<messagemap>
    <author>
        <au_id>274-80-9391</au_id>
        <company>Dean Straight</company>
        <phone>415 834-2919</phone>
        <address>5420 College Av.</address>
        <city>Oakland</city>
        <state>CA</state>
        <zip>94609</zip>
        <contract>1</contract>
    </author>
</messagemap>

Upvotes: 3

Views: 81

Answers (1)

Daniel Haley
Daniel Haley

Reputation: 52848

You could try matching au_fname and building company. You could then strip au_lname.

XSLT 1.0

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

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

    <xsl:template match="au_fname">
        <company>
            <xsl:value-of select="normalize-space(concat(.,' ',../au_lname))"/>
        </company>
    </xsl:template>

    <xsl:template match="au_lname"/>

</xsl:stylesheet>

Upvotes: 3

Related Questions