简体   繁体   中英

Modify XSLT using C# Code

I am Working on Visual-studio 2012 in C#. I want to update the value of a node of a XSLT.

This abc.xslt is like:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
  <xsl:output method="xml" encoding="UTF-8" indent="yes" />
  <xsl:template match="/">
    <DocumentElement>
     <PositionMaster>
      <Name>
        <xsl:value-of select = "'Ryan'"/>
      </Name>
     </PositionMaster>
    </DocumentElement>

Code i have written to modify this XSLT in the C# is:

XmlDocument xslDoc = new XmlDocument();
xslDoc.Load(abc.xslt);
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xslDoc.NameTable);
nsMgr.AddNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");

I am looking to change the value of Name field to David. What should i write further here?

XmlElement valueOf = xslDoc.SelectSingleNode("/xsl:stylesheet/xsl:template[@match = '/']/DocumentElement/PositionMaster/Name/xsl:value-of", nsMgr);

if (valueOf != null)
{
  valueOf.SetAttribute("select", "'David'");
  xslDoc.Save("new.xslt");
}
else
{
  // handle case here that element was not found
}

You seem to be going about this a very odd way. Why not just use a stylesheet parameter (a global xsl:param element)?

And if you do need to modify a source stylesheet, as you sometimes do, surely it makes more sense to use XSLT for the purpose?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM