creating an XSLT file
i have two xml files:
<m2>
<header>
<to>ggg</to>
<from>aaa</from>
<id>11</id>
<name>gd</name>
<mtype>me</mtype>
</header>
<bod开发者_如何转开发y>some text</body>
</m2>
2.
<m2>
<header>
<desc>
<to>ggg</to>
<from>aaa</from>
</desc>
<id>11</id>
<name>gd</name>
<mtype>nothing</mtype>
</header>
<body>some text</body>
</m2>
what is the xslt file that convert from the first xml to the second xml? the xslt need to transform the value of the mtype from "me" to "some text" and insert the "to" and "from" elements into the "desc" element.
This transformation:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="to">
<desc>
<xsl:copy-of select=".|../from"/>
</desc>
</xsl:template>
<xsl:template match="mtype/text()">nothing</xsl:template>
<xsl:template match="from"/>
</xsl:stylesheet>
when applied o the provided XML document, restored from the picture ( Never do this again!):
<m2>
<header>
<to>ggg</to>
<from>aaa</from>
<id>11</id>
<name>gd</name>
<mtype>me</mtype>
</header>
<body>some text</body>
</m2>
produces the wanted result:
<m2>
<header>
<desc>
<to>ggg</to>
<from>aaa</from>
</desc>
<id>11</id>
<name>gd</name>
<mtype>nothing</mtype>
</header>
<body>some text</body>
</m2>
Explanation: Simple application of the identity rule design pattern. Using and overriding the identity template is the most fundamental and powerful XSLT design pattern.
See examples and explanations at: http://dpawson.co.uk/xsl/sect2/identity.html
There's a lot of tutorials for this; just do a web search on "XSLT Tutorial". Here's one of the sites:
http://www.w3schools.com/xsl/
精彩评论