hInserting line-break into XML so that it appears

2019-07-20 06:05发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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>