CodexBloom - Programming Q&A Platform

XSLT Transformation with Conditional Logic scenarios in .NET Core 3.1 - Unexpected Output

👀 Views: 0 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-14
xml xslt dotnet-core C#

I'm working on a project and hit a roadblock... Hey everyone, I'm running into an issue that's driving me crazy. I'm working on an XML transformation using XSLT in a .NET Core 3.1 application, and I'm running into issues when trying to implement conditional logic. My XML input looks something like this: ```xml <items> <item> <name>Item 1</name> <category>A</category> </item> <item> <name>Item 2</name> <category>B</category> </item> </items> ``` My goal is to transform this XML such that if the category is 'A', the output should include a specific tag, while any other category should be ignored. Here's the XSLT I'm using: ```xml <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <transformed> <xsl:for-each select="items/item"> <xsl:if test="category='A'"> <result> <xsl:value-of select="name" /> </result> </xsl:if> </xsl:for-each> </transformed> </xsl:template> </xsl:stylesheet> ``` However, when I run the transformation, the output is empty, and I'm not seeing any errors or exceptions. I've tried debugging by adding some `<xsl:message>` calls to see where it might be failing, but they don't seem to trigger at all. I've also verified that the XML is being loaded correctly in my C# code: ```csharp var xmlDocument = new XmlDocument(); xmlDocument.Load("path_to_xml_file.xml"); var xslt = new XslCompiledTransform(); xslt.Load("path_to_xslt_file.xslt"); using (var writer = new XmlTextWriter("output.xml", Encoding.UTF8)) { xslt.Transform(xmlDocument, null, writer); } ``` When I check the `output.xml`, it's just empty. I suspect it might be something related to how the XSLT is processing the nodes, but I'm not sure what I'm missing. Has anyone experienced similar issues or can provide insights into what might be going wrong? My development environment is macOS.