• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 667
  • Last Modified:

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

0
nhay59
Asked:
nhay59
  • 3
1 Solution
 
Geert BormansInformation ArchitectCommented:
you need to use
<xsl:apply-templates/>
instead of
<xsl:value-of select="text()"/>

and make a template for person
<xsl:template match="person">
   <span style="your person style goes here"><xsl:apply-templates/></span>
</xsl:template>
0
 
nhay59Author Commented:
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.
0
 
nhay59Author Commented:
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

0
 
nhay59Author Commented:
All solved now. Thanks for the help.
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now