h将换行符插入XML中,以便在VB.NET中呈现XSL之后出现 [英] hInserting line-break into XML so that it appears after XSL rendering in VB.NET

查看:163
本文介绍了h将换行符插入XML中,以便在VB.NET中呈现XSL之后出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 System.xml.xmlDocument()对象,它使用XSL呈现在网页上。我想在XML对象的特定节点中插入一个'linebreak',所以当使用XSLT呈现XML时,会出现实际的换行符。我的代码如下所示:

  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(;)
如果parArray.Length> 2然后
resultParent = String.Empty
Dim parCounter As Integer = 0
对于每个父母作为字符串在parArray
parCounter = parCounter + 1
resultParent = resultParent + Parent +;
如果(parCounter Mod 2)= 0那么
resultParent = resultParent +& #xA;
End If
Next
End If
par.InnerText = resultParent
Next

在XSL中:

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

然而,它看起来像 xmlDocument 是自动的转义下一行字符,所以它只是在页面上显示为文本,任何人都可以告诉如何解决这个问题? 问题可以通过这一行来解决....

  resultParent = resultParent +& #xA; 

现在,您可能试图输出XML如下所示:

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

但是,这个转义符& #xA; 实体只在文档尚未解析时才有用。如果它是一个文本文档,它会被后续读取并解析为一个XML文档,那么这些实体将按照预期进行处理。但是您正在处理已经解析过的XML文档。因此,当您执行 resultParent = resultParent +&#xA;时,它实际上会将五个字符的字符串插入现有文本节点,并且因为& 是一个特殊字符,它会被转义。



现在, / p>

  resultParent = resultParent + chr(10)

但是最终这将证明是徒劳的,因为HTML不能识别换行符,所以你必须编写你的XSLT来用<如果你想在你的VB代码中这样做,你可以创建新的元素。 br 元素自己,然后插入它们

 对于每个参数为System.Xml.XmlNode在parentNodes 
currentParentValue = par.InnerText
par.InnerText = String.Empty
Dim parArray As String()= currentParentValue.Split(;)
对于每个父母作为字符串在parArray
如果Parent.Length> 0然后
Dim person As XmlText = objOutput.CreateTextNode(Parent)
par.AppendChild(person)
par.AppendChild(objOutput.CreateElement(br))
End If
Next
Next

所以,这需要 PARENT 节点,将其清除,然后添加文本节点,并为每个父节点添加新的 br 元素。然后,输出将如此,使用XSLT将其输出为HTML要容易得多。

 < PARENT> George Aaron<苏珊李亚伦< />理查德艾略特亚伦< br />< / PARENT> 

(每次添加 br 都不应太难如果需要,第二个父母)。



然而,如果将表示性信息放入XML文件中可能不一定是个好主意。假设你以后不得不将XML转换成另一种格式?

 对于每个参数为System.Xml.XmlNode在parentNodes中
currentParentValue = par.InnerText
par.InnerText = String.Empty
Dim parArray As String()= currentParentValue.Split(;)
对于每个父母作为字符串在parArray
如果Parent.Length> 0然后
Dim person As XmlElement = objOutput.CreateElement(PERSON)
person.InnerText = Parent.Trim()
par.AppendChild(person)
End If
下一个
下一个

这会输出类似这样的内容。

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

以HTML格式显示此内容也很简单



提示:要以两个组的形式显示,您的XSLT可能看起来像这样....

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


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 + "&#xA;"
                            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?

解决方案

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>

这篇关于h将换行符插入XML中,以便在VB.NET中呈现XSL之后出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆