Link to home
Start Free TrialLog in
Avatar of nhay59
nhay59

asked on

xslt query regarding child elements

HI,

I have the following type of xml,

<name type="1">this is some demo text <person type="2">more demo text</person> about a name</name>

I can get the 'name' element to be converted to html using xslt, but I'm having problems with the 'person' element. I'm currently using the xsl as shown in the code snippet. I can get the xsl to show all of the line in html by using

<xsl:value-of select="."/>

but the child element 'person' is only shown as part of the 'name' element, and therefore without its own specific styling etc. If I can the above to,

<xsl:value-of select="text()"/>

then only the first part of the text up to the element 'person' is shown in the html. Everyting after this element is not shown.

How do I get the line to show correctly in html with the required styles etc per element?

Any help or examples appreciated.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="html" version="4.0" indent="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="text()[not(parent::name or parent::person)]"/>
    
   <!-- <xsl:template match="text()"/>-->
    
    <xsl:template match="body">
        <div id="body">
            
            <xsl:apply-templates/>
            
        </div>
    </xsl:template>
    
    <xsl:template match="name">
        <span style="color:#000000; margin:10px;">
            <xsl:choose>
                <xsl:when test="string-length(.) &gt; 0">
                    <xsl:value-of select="text()"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="following-sibling::text()[1]"/>
                </xsl:otherwise>
            </xsl:choose>
        </span>
        <br />
        </xsl:template>
    
  <!--  <xsl:template match="name">
        <span style="display:block;
            padding:10px;">
            <xsl:value-of select="."/></span>
        <br />
    </xsl:template> -->
    
  
    
    <xsl:template match="person">
        <span style="
            color:#cccccc;
            float:left;
            width:20%;
            font-style:italic;
            border:solid 1px #CCCCCC;
            padding:10px;
            margin:10px;">
            <xsl:value-of select="text()"/></span>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nhay59
nhay59

ASKER

Hi,

Thanks for the reply. I'm not certain I understand your comment. I've already added a template for person, and where do i change,

<xsl:apply-templates/>
instead of
<xsl:value-of select="text()"/>

The problem is that with the current code, the 'name' element is being converted to html up to the point where the 'person' element begins. Then any text after the 'person' element, but still within the 'name' element, is not being shown in the html.

Thanks for your help with the problem.
Avatar of nhay59

ASKER

Hi,

I've managed to get the xsl to show in html the full text of the <name> element and the <person> element for the first occurrence, but every subsequent occurrence still has the same problem.

Therefore, the first <name> element containing text and a <person> element is shown correctly in html, however, every subsequent occurrence of this type only shows the first part of the text for the <name> element, and then puts the <person> element on a new line without the rest of the text for the <name> element.

I'm currently using the following xsl to transform to html.

Any help or examples really appreciated.

Thanks
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    
    <xsl:output method="html" version="4.0" indent="yes"/>
    
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="text()[not(parent::name or parent::person)]"/>
    
   <!--<xsl:template match="text()"/>-->
    
    <xsl:template match="body">
        <div id="body">
            
            <xsl:apply-templates/>
            
        </div>
    </xsl:template>
    
   <xsl:template match="name">
        <span style="color:#000000; margin:10px;">
            <xsl:choose>
                <xsl:when test="string-length(.) &gt; 1">
                 <!--<xsl:value-of select="."/>-->
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="following-sibling::text()[1]"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:apply-templates/>
        </span>
        <br />
        </xsl:template>
    
    <xsl:template match="person">
        <span style="text-decoration: line-through;">
            <xsl:apply-templates/>
        </span>
    </xsl:template>
 
</xsl:stylesheet>

Open in new window

Avatar of nhay59

ASKER

All solved now. Thanks for the help.