Xml
Write a program need to change the root of XML, feel very inconvenient, so wrote a function, general ideas and implementation as follows:
The first thought is that XSLT can also filter something and then send it back to the client, so it's decided to use XSLT
Then you try to write a root-change XSL, as follows
<?xml version= "1.0" encoding= "gb2312"?>
<xsl:stylesheet version= "1.0" xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:output method= "xml" version= "1.0" encoding= "gb2312" omit-xml-declaration= "yes" indent= "yes"/>
<xsl:template match= "/" >
<NEWNODENAME>
<xsl:apply-templates select= "@* | Node () "/>
</NEWNODENAME>
</xsl:template>
<xsl:template match= "@* | Node () ">
<xsl:choose>
<xsl:when test= ".! =ancestor::node () "><xsl:copy><xsl:apply-templates select=" @* | Node () "/></xsl:copy></xsl:when>
<xsl:otherwise><xsl:apply-templates select= "@* | Node () "/></xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The general meaning is to parse all nodes and attributes in an XML, if they are copied directly from the root, or to see if there are any child nodes and attributes. Please refer to MSXML's SDK documentation for specific information.
Then write a function so that newnodename can specify:
This code for the implementation of vb.net, in the implementation of ASP need to pay attention to the object attached to the value of the set, and return the value of the problem
Public Function Changexmlrootnodename (ByRef oxml as MSXML2. DOMDocument, ByVal name as String) as String
If oXML is nothing Then
Return ""
End If
Dim Sxslt as String
SXSLT = "<?xml version=" "1.0" "encoding=" "gb2312" "?>" & VbCrLf
SXSLT = sxslt & "<xsl:stylesheet version=" "1.0" "xmlns:xsl=" "Http://www.w3.org/1999/XSL/Transform" ">" & VbCrLf
SXSLT = sxslt & "<xsl:output method=" "xml" "version=" "1.0" "encoding=" "gb2312" "omit-xml-declaration=" "Yes" Indent= "yes" "/>" & vbCrLf
Sxslt = sxslt & vbCrLf
SXSLT = sxslt & "<xsl:template match="/">" & vbCrLf
SXSLT = sxslt & "<" & name & ">" "& VbCrLf"
SXSLT = sxslt & "<xsl:apply-templates select=" @* | Node () "/>" & vbCrLf
SXSLT = sxslt & "</" & name & ">" & vbCrLf
SXSLT = sxslt & "</xsl:template>" & VbCrLf
Sxslt = sxslt & vbCrLf
SXSLT = sxslt & "<xsl:template match=" @* | Node () ">" & vbCrLf
SXSLT = sxslt & "<xsl:choose>" & VbCrLf
SXSLT = sxslt & "<xsl:when test=" ".! =ancestor::node () "" "><xsl:copy><xsl:apply-templates select=" "@* | Node () "/></xsl:copy></xsl:when>" & VbCrLf
SXSLT = sxslt & "<xsl:otherwise><xsl:apply-templates select=" @* | Node () "/></xsl:otherwise>" & VbCrLf
SXSLT = sxslt & "</xsl:choose>" "& VbCrLf"
SXSLT = sxslt & "</xsl:template>" "& VbCrLf"
Sxslt = sxslt & vbCrLf
SXSLT = sxslt & "</xsl:stylesheet>" & VbCrLf
Dim oxsl as New MSXML2. DOMDocument
Oxsl.async = False
Oxsl.resolveexternals = False
Oxsl.loadxml (SXSLT)
Dim sXml as String
SXML = Oxml.transformnode (oxsl)
SXML = Addxmlheader (sXml)
sXSL = Nothing
Return SXML
End Function
And, of course, you can also implement an XSL file that produces that XSL, which depends on how you design it.