3

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>

1 Answer 1

3

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>
Sign up to request clarification or add additional context in comments.

3 Comments

What kind of witchcraftis this! Really, was it really that simple. Anyway thanks. I only have a question about the concat does the first '.,' equals au_fname then? The rest is very clear.
@JonathanRomer - You're very welcome. Yes the . is the current context which is au_fname. Also, the normalize-space() was added to strip the trailing space that would be added by the concat() if ../au_lname didn't exist.
You are providing for the case where au_lname is missing, but disregard the possibility of au_fname not existing (as a node, not as a value).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.