Transforming XML with XSLT 1

Today I am really struggling with XSLT, it was a long time ago that I had to use it. I have to edit some xml and I cannot use XSLT 2.0. Therefore I have to use 1.0. Xml im struugling with (basic example):

I tried to create a template for 2 nodes and then "call" this template to create a new node with the values โ€‹โ€‹I wanted, but that didn't work either, I'm missing something if someone can point me in 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>
      

Run codeHide result


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>
      

Run codeHide result


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>
      

Run codeHide result


What I need:

<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>
      

Run codeHide result


+3


source to share


1 answer


You can try to find au_fname

and build company

. Then you can uninstall 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>

      

+3


source







All Articles