2

I have a System.xml.xmlDocument() object which is rendered onto a web page by using XSL. I want to insert a 'linebreak` inside certain nodes in the XML object, so when the XML is rendered using XSLT there is an actual line break there. My Code to do this looks like this:

Dim parentNodes As System.Xml.XmlNodeList = objOutput.SelectNodes("//PARENT")
                Dim currentParentValue As String = String.Empty
                Dim resultParent As String = String.Empty
                For Each par As System.Xml.XmlNode In parentNodes
                    currentParentValue = par.InnerText
                    Dim parArray As String() = currentParentValue.Split(";")
                    If parArray.Length > 2 Then
                        resultParent = String.Empty
                        Dim parCounter As Integer = 0
                        For Each Parent As String In parArray
                            parCounter = parCounter + 1
                            resultParent = resultParent + Parent + "; "
                            If (parCounter Mod 2) = 0 Then
                                resultParent = resultParent + "
"
                            End If
                        Next
                    End If
                    par.InnerText = resultParent
                Next

And in XSL:

<td width="50%" nowrap="nowrap">
<xsl:value-of select="STUDENT_DETAILS/PARENT"/>
</td> 

However, it looks like xmlDocument is automatically escaping the next line character, so it just appears as text on the page, can anyone tell how to fix this?

Art F
  • 3,992
  • 10
  • 49
  • 81
  • Where is your XSLT invocation? Also please add relevant examples of the input XML and the desired output. The question is rather vague at the moment. – Tomalak Dec 03 '13 at 16:54
  • @Tomalak I'll try editing to add that, but I don't see how it would make a difference, I am trying to insert a line break inside a node by using ` ` and want it to appear as an actual line break on the HTML page, how would the format of the XML make a difference? – Art F Dec 03 '13 at 16:59
  • Because you could (and potentially *should*) do this right in the XSLT instead of modifying the the transformed XML after the fact. But to see if that is viable I'd need to see the source. (In any other case the question is not really about XSLT and the tag could be removed altogether) – Tomalak Dec 03 '13 at 17:01
  • AFAIK, in order to see an actual line break on an HTML page, you need to insert a `
    ` tag, not a line feed character.
    – michael.hor257k Dec 03 '13 at 17:35
  • @user3016153 already tried that, does the exact the same thing, the tag gets escaped and just appears on the screen as text. – Art F Dec 03 '13 at 17:38
  • @Tomalak XSLT is used to convert XML into HTML, so the XML is there first and is generated from a database call. – Art F Dec 03 '13 at 17:39
  • I realize that. But if I could *see* a sample of XML and the HTML you want to get, I could suggest XSLT to do what you need. Up to now all I can do is guess. My gut feeling is that messing with the input and/or output DOM tree is the wrong approach. – Tomalak Dec 03 '13 at 17:43
  • The tag gets escaped if it appears *as text* in the source XML document. You should use the XSLT stylesheet to insert it during the transformation to HTML. – michael.hor257k Dec 03 '13 at 17:47
  • Why do this pre-processing at all. Why not just have the XSLT split the value on the semi-colon delimiter and add the `
    ` tags between the items in the list. If you want to do the pre-processing, why not actually break the element up into a list of sub-elements, and then let the XSLT output them however it likes.
    – Steven Doggart Dec 03 '13 at 17:49
  • @StevenDoggart I thought it was easier to do it this way, but I am more than happy to use a solution in XSLT, I'm just not quite sure how to do that... – Art F Dec 03 '13 at 18:04
  • @user3016153 same as previous comment – Art F Dec 03 '13 at 18:05
  • 2
    Check out [this](http://stackoverflow.com/questions/3336424/does-xslt-have-split-function) and [this](http://stackoverflow.com/questions/4845660/xsl-how-to-split-strings) for examples on how to split the string in XSLT. – Steven Doggart Dec 03 '13 at 18:08
  • I am not sure how to do it either. I don't know what your XML looks like. I don't even know what version of XSLT your stylesheet is using. There's no helping you if you won't open up the hood. – michael.hor257k Dec 03 '13 at 18:35
  • Continued at:[http://stackoverflow.com/questions/20360854/trying-to-add-line-breaks-inside-a-template-in-xslt] – michael.hor257k Dec 03 '13 at 23:47

3 Answers3

0

If you change

<td width="50%" nowrap="nowrap">
<xsl:value-of select="STUDENT_DETAILS/PARENT"/>
</td> 

to

<td width="50%" nowrap="nowrap">
<pre>
  <xsl:value-of select="STUDENT_DETAILS/PARENT"/>
</pre>
</td> 

the browser will render line breaks.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Didn't work, I think I'll go with what others suggested and try making a recursive function inside XSLT. – Art F Dec 03 '13 at 18:46
0

you can just simple append "<'br\>" next to your nodes, that will insert the linebreak between yours two nodes.

Notes:

please remove the ' before br.

Peri
  • 11
  • 2
0

You problem resolves around this line....

 resultParent = resultParent + "&#xA;"

Now, you are probably trying to output your XML like this:

<PARENT>George Aaron&#xA; Susan Lee Aaron&#xA; Richard Elliot Aaron&#xA;</PARENT>

However, this escaped &#xA; entity is only relevant if the document has yet to be parsed. If it were a text document, that gets subsequent read and parsed into an XML document, then the entities would be handled as expected. But you are working with an XML document that has already been parsed. Therefore, when you do resultParent = resultParent + "&#xA;" it is actually going to insert a string of five characters into an existing text node, and because & is a special character, it gets escaped.

Now, what you can simply do is this...

 resultParent = resultParent + chr(10)

But ultimately this will prove fruitless because HTML doesn't recognise line-break characters, so you would have to write your XSLT to replace the line break with a <br /> element.

If you wanted to do this in your VB code though, you could create new br elements yourself, and insert them

For Each par As System.Xml.XmlNode In parentNodes
  currentParentValue = par.InnerText
  par.InnerText = String.Empty
  Dim parArray As String() = currentParentValue.Split(";")
  For Each Parent As String In parArray
    If Parent.Length > 0 Then
      Dim person As XmlText = objOutput.CreateTextNode(Parent)
      par.AppendChild(person)
      par.AppendChild(objOutput.CreateElement("br"))
    End If
  Next
Next

So, this takes the PARENT node, clears it down, then adds a text node, and new br element for each parent. The output would then be like so, which would be much easier to output as HTML using XSLT

<PARENT>George Aaron<br />Susan Lee Aaron<br />Richard Elliot Aaron<br /></PARENT>

(It shouldn't be too hard to add the br after every second parent if required).

However, if may not necessarily be a good idea to put "presentational" information in a XML file. Suppose you later had to transform the XML into a different format? An alternate approach would be separate each parent into their own element.

For Each par As System.Xml.XmlNode In parentNodes
  currentParentValue = par.InnerText
  par.InnerText = String.Empty
  Dim parArray As String() = currentParentValue.Split(";")
  For Each Parent As String In parArray
    If Parent.Length > 0 Then
      Dim person As XmlElement = objOutput.CreateElement("PERSON")
      person.InnerText = Parent.Trim()
      par.AppendChild(person)
    End If
  Next
Next

This would output something like this..

<PARENT>
   <PERSON>George Aaron</PERSON>
   <PERSON>Susan Lee Aaron</PERSON>
   <PERSON>Richard Elliot Aaron</PERSON>
   <PERSON>Albert Smith</PERSON>
</PARENT>

Displaying this as HTML would also be straight-forward

Hint: To display in groups of two, your XSLT may look something like this....

<xsl:for-each select="PERSON[postion() mod 2 = 1]">
    <xsl:value-of select=".">;
    <xsl:value-of select="following-sibling::PERSON[1]" />
    <br />
</xsl:for-each>
Tim C
  • 70,053
  • 14
  • 74
  • 93