I have an XML file that I am transforming via XSLT. I am passing an XML as parameter to the XSLT via C#. The parameter's name is attachment and it contains XML. It is written as follows:
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.WriteStartElement("root");
if (!string.IsNullOrEmpty(sWordFileName)) {
w.WriteStartElement("mylink", sWordFileName);
w.WriteEndElement();
}
if (!string.IsNullOrEmpty(sPDFFileName)) {
w.WriteStartElement("mylink", sPDFFileName);
w.WriteEndElement();
}
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();
_exportSet[currentExportSet].Format.ParamList["attachment"] = nav.Select("./*");
My xml parameter looks like
<root><attachment xmlns=file1><attachment xmlns=file2></root>
Now in XSLT I need to iterate through this XML param and create a link.
Here is my XSLT
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my-scripts="urn:my-scripts"
xmlns="factiva.com/fcs/schemas/newsSummaries">
<xsl:param name="attachment"/>
<xsl:for-each select="$attachment">
<a target="_blank" href="#"><xsl:copy-of select="."/></a>
</xsl:for-each>
</xsl:stylesheet>
But it doesn't create a link.
An XSLT parameter is different than an XML tag name. Parameters are passed using the tag as described here.
As stated in the comments below, this problem is not too different from what is provided in the link above.
StringWriter sw = new StringWriter();
XmlTextWriter w = new XmlTextWriter(sw);
w.WriteStartElement("root");
if (!string.IsNullOrEmpty(sWordFileName)) {
w.WriteStartElement("attachment", sWordFileName);
w.WriteAttributeString("name", sWordFileName);
w.WriteEndElement();
}
if (!string.IsNullOrEmpty(sPDFFileName)) {
w.WriteStartElement("attachment");
w.WriteAttributeString("name", sPDFFileName);
w.WriteEndElement();
}
w.WriteEndElement();
w.Close();
XPathDocument doc = new XPathDocument(new StringReader(sw.ToString()));
XPathNavigator nav = doc.CreateNavigator();
XsltArgumentList xslArg = new XsltArgumentList();
xslArg.AddParam("attachment","",nav);
Here would be XSL to match per Accessing parameters which contain mark-up:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:my-scripts="urn:my-scripts"
xmlns="factiva.com/fcs/schemas/newsSummaries">
<xsl:param name="attachment" />
<xsl:template match="/">
<xsl:apply-templates select="$attachment"/>
</xsl:template>
<xsl:template match="attachment">
<a target="_blank" href="{@name}">{@name}</a>
</xsl:template>
</xsl:stylesheet>