sql vb xml 换行,h将换行符插入XML中,以便在VB.NET中呈现XSL之后出现

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:

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?

解决方案

You problem resolves around this line....

resultParent = resultParent + " "

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

George Aaron Susan Lee Aaron Richard Elliot Aaron

However, this escaped 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 + " " 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
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

George Aaron
Susan Lee Aaron
Richard Elliot Aaron

(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..

George Aaron

Susan Lee Aaron

Richard Elliot Aaron

Albert Smith

Displaying this as HTML would also be straight-forward

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

;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值