XSLT非常新,需要帮助我无法正常工作的XSLT转换。我遇到的问题是转换后的文档带有空白的CONO和CUNO元素。
我一直在使用http://www.w3schools.com/xsl/xsl_value_of.asp作为指南,但它似乎没有用。
这是原始的XML。
<?xml version="1.0" encoding="utf-8"?>
<GetBasicData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="CRS610MI">
<CONO xmlns="">1</CONO>
<CUNO xmlns="">123456</CUNO>
</GetBasicData>
这是我的XSLT
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml"/>
<xsl:template match="/">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<GetBasicData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="CRS610MI">
<CONO xmlns="">
<xsl:value-of select="GetBasicData/CONO"/>
</CONO>
<CUNO xmlns="">
<xsl:value-of select="GetBasicData/CUNO"/>
</CUNO>
</GetBasicData>
</soap:Body>
</soap:Envelope>
</xsl:template>
</xsl:stylesheet>
这是我用于转换的C#代码
private static Boolean TransformXML(XPathDocument xPathDocument, String xslPath, out XmlDocument xmlDocument)
{
try
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (StreamWriter streamWriter = new StreamWriter(memoryStream))
{
XmlWriter xmlWriter = XmlWriter.Create(streamWriter);
XsltSettings xsltSettings = new XsltSettings();
xsltSettings.EnableScript = true;
XslCompiledTransform xslCompiledTransform = new XslCompiledTransform();
xslCompiledTransform.Load(xslPath, xsltSettings, null);
xslCompiledTransform.Transform(xPathDocument, xmlWriter);
memoryStream.Position = 0;
StreamReader streamReader = new StreamReader(memoryStream);
XmlReader xmlReader = XmlReader.Create(streamReader);
xmlDocument = new XmlDocument();
xmlDocument.Load(xmlReader);
}
}
}
catch (Exception exception)
{
Debug.WriteLine(exception);
xmlDocument = null;
return false;
}
return true;
}
答案 0 :(得分:1)
在输入XML中,GetBasicData
元素具有a_default namespace_(恰好是“CRS610MI”)。另一方面,CONO
和CUNO
元素没有命名空间。
在样式表中添加名称空间声明。此外,输出选项indent="yes"
使输出更具人性化。
您的意图似乎是将输入XML放在soap:Body
中而不更改内容。在这种情况下,您不必重新定义XSLT样式表中的所有内容 - 尽可能从原始XML进行复制。
<强>样式表强>
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:crs="CRS610MI" exclude-result-prefixes="crs">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<xsl:apply-templates select="node()|@*"/>
</soap:Body>
</soap:Envelope>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<强>输出强>
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<soap:Body>
<GetBasicData xmlns="CRS610MI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<CONO xmlns="">1</CONO>
<CUNO xmlns="">123456</CUNO>
</GetBasicData>
</soap:Body>
</soap:Envelope>