Using XSLT to change the root of XML

Source: Internet
Author: User
Tags copy end string version xmlns xsl xsl file xslt
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.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.