Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputting text and subelements within XML using XSLT 1

Status
Not open for further replies.

TeaAddictedGeek

Programmer
Apr 23, 1999
271
US
I have an overly complicated bit of XML that I have to analyze using an XSLT stylesheet, and parts of it go like this:

<parent_node>
Lots and lots and lots and lots of text here
<child_node1>more text</child_node1>
<child_node2>
even more text
<child_node3>the text never ends!</child_node3>
</child_node2>
</parent_node>

How do I get it to output the text inside of parent_node via the XSL tags without accidently repeating what is in child_node1? I can't seem to get that part to work. I'm a little new at this I admit, and I checked the archives but couldn't find an answer.


Thanks!
 
Materially like this. (position 1 is just for fixing a context node to be concrete.)
[tt]
<xsl:template match="//parent_note[1]">
<xsl:value-of select="string()" />
</xsl:template>
[/tt]
 
What if you have cases like this?

<child_node1>
Lots of text, <textstyle logic="bold">this text is bold</textstyle>, and more text
</child_node1>
<child_node2>
<textstyle logic="italic">This text is italicized</textstyle> and there's even more text
</child_node2>


Thanks!

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
For another instance, if that makes you see more clearly.

[tt]<xsl:for-each select="//child_node1">
<xsl:value-of select="string()" />
</xsl:for-each>[/tt]

Mathematically called self-similarity, you relist a fragment which is in essence the same as the 1st post, only changing names etc. xpath is in good position to handle that kind of sturcture.
 
I'm trying to do that, but it repeats the string so that the output looks like this:


Lots of text, this text is bold, and more text this text is bold


What am I doing wrong?

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
I'm not using multiple templates, no. The problem is trying to evaluate the text formatting and rules based on the different child text nodes. Otherwise this would be a piece of cake.

"The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs."
-Joseph Weizenbaum
 
So what is your xsl and xml. Cake or non-cake, it does not solve any problem and does not demonstrate your forthcoming with put forward a well-posed problem.
 
Try running this stylesheet against your XML document.
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="/">
    <xsl:apply-templates  mode="showme" />
  </xsl:template>
  <xsl:template match="*" mode="showme">
    <xsl:element name="{local-name(.)}">
      <xsl:attribute name="localname"><xsl:value-of select="local-name(.)"/></xsl:attribute>
      <xsl:attribute name="parentname"><xsl:value-of select="local-name(parent::*)"/></xsl:attribute>
      <xsl:attribute name="position"><xsl:value-of select="position()"/></xsl:attribute>
      <xsl:value-of select="text()"/>
      <xsl:apply-templates select="*[local-name() != '']" mode="showme"/>
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>
Take this and play with it, and you will probably get a better feel about how text nodes operate.

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top