Home > database >  how to create attribute value with the text of specifics elements of a list with XSLT 1.0
how to create attribute value with the text of specifics elements of a list with XSLT 1.0

Time:07-15

I want to create an attribute and assign to it a value, that is the text of the first, second and last elements of the node.

I have this XML

<?xml version="1.0" encoding="UTF-8"?>
<WORK>
    <TASKS>
        <OFFICE PosA="1" PosB="2" Homeoffice="yes" name="Accountant">
            <SECTION>
                <business Key="Code OFFICE" type="Position"> BOSS </business>
            </SECTION>
            <information>
                <var>this is </var> 
                <var>the information </var> 
                <var>not this </var> 
                <var>neither this </var> 
                <var>I want </var> 
            </information>
        </OFFICE>
        <OFFICE PosA="10" PosB="11" Homeoffice="NO" name="Engineer">
            <SECTION>
                <business Key="Code OFFICE" type="Position"> TECH </business>
            </SECTION>
            <information>
                <var>this is </var> 
                <var>the information </var> 
                <var>not this </var> 
                <var>neither this </var> 
                <var>I want </var>
            </information>
        </OFFICE>
    </TASKS>
</WORK>

And I want this in the output xml :

<?xml version="1.0" encoding="UTF-8"?>
   <Branch>
        <Company complete="this is the information I want"> </Company>
        <Company complete="this is the information I want"> </Company>
   </Branch>

I have this XSLT, but its not working

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ex="http://exslt.org/dates-and-times">
    <xsl:output version="1.0" encoding="UTF-8" standalone="yes" indent="yes" method="html"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="/">
    <Branch>
    <xsl:for-each select="WORK/TASK/OFFICE">
        <Company>
        <xsl:attribute name="Complete">
            <xsl:value-of select="information/var[1]/normalize- space(text())"/>
            <xsl:value-of select="information/var[2]/normalize- space(text())"/>
            <xsl:value-of select="information/var[5]/normalize- space(text())"/>
        </xsl:attribute>
        </Company>
    </Branch>
    </xsl:template>
</xsl:stylesheet>

how can I rewrite the xslt to get what I want? any help would be great!

CodePudding user response:

First I had to fix some trivial errors in your code:

  • Missing xsl:for-each end tag
  • TASK should be TASKS
  • There is no support for HTML version 1.0
  • Remove the spaces in "normalize- space()" (!)

You've used a 2.0 construct - a function call on the right-hand-side of the "/" operator - so you're going to have to change this if you're using XSLT 1.0 (which the tag on your question suggests). It needs to be normalize-space(information/var[1]). No need for the call on text().

But I don't know why you're using normalize-space() when you clearly need to retain the final space in the input data.

  • Related