开发者

Using XSLT to copy all nodes in XML, with support for special cases

Say I have a large XML file that the following structure:

<MyXml>
  <Data1>
    <Node1>1234</Node1>
    <Node2>abc<Node2>
    <Node3>gfdf</Node3>
    ...
    <Node10000>more text</Node10000>
  </Data1>
  <Data2>
    ...
  </Data2>
</MyXml>

I want to transform this XML into another XML that looks exactly the same, but has a certain string concatinated to a certain node, sa开发者_JAVA技巧y Node766. I am using an XSLT of course and wondering how I can tell it to copy everyhing as-is except for Node766, where I have to do something before outputing it.


<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!--Identity template, 
        provides default behavior that copies all content into the output -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!--More specific template for Node766 that provides custom behavior -->
    <xsl:template match="Node766">  
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
            <!--Do something special for Node766, like add a certain string-->
            <xsl:text> add some text </xsl:text>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>


Start with an identity transform, and include a template match for your exception.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜