我想将重复组从示例XML转换为两个分隔的文本行。这是我目前的代码版本:
string orderXml =
@"<?xml version='1.0' encoding='utf-8'?>
<Order id='79223510'>
<Status>new</Status>
<ShipMethod>Standard International</ShipMethod>
<ToCity>Tokyo</ToCity>
<Items>
<Item>
<SKU>SKU-1234567890</SKU>
<Quantity>1</Quantity>
<Price>99.95</Price>
</Item>
<Item>
<SKU>SKU-1234567899</SKU>
<Quantity>1</Quantity>
<Price>199.95</Price>
</Item>
</Items>
</Order>";
StringReader str = new StringReader(orderXml);
var xslt = new XmlTextReader(new StringReader(
@"<?xml version='1.0' encoding='UTF-8'?>" +
"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>" +
"<xsl:output method='text' indent='no'/>" +
"<!-- New line character variable -->" +
"<xsl:variable name='newline'>" +
"<xsl:text> </xsl:text>" +
"</xsl:variable>" +
"<xsl:variable name='empty'>" +
"<xsl:text></xsl:text>" +
"</xsl:variable>" +
"<xsl:template match='/'>" +
"<xsl:for-each select='Order'>" +
"<xsl:value-of select='@id'/>|" +
"<xsl:value-of select='Status'/>|" +
"<xsl:value-of select='ShipMetod'/>|" +
"<xsl:value-of select='ToCity'/>|" +
"<xsl:value-of select='$newline'/>" +
"</xsl:for-each>" +
"<xsl:for-each select='Order/Items/Item'>" +
"<xsl:value-of select='SKU'/>|" +
"<xsl:value-of select='Quantity'/>|" +
"<xsl:value-of select='Price'/>|" +
"<xsl:value-of select='$newline'/>" +
"</xsl:for-each>" +
"</xsl:template>" +
"</xsl:stylesheet>"
));
var xDoc = new XPathDocument(str);
var xTr = new System.Xml.Xsl.XslCompiledTransform();
xTr.Load(xslt);
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
xTr.Transform(xDoc, null, writer);
string[] lines = sb.ToString().Split(new string[] {"\n"}, StringSplitOptions.RemoveEmptyEntries);
lines.ToList().ForEach(System.Console.Write);
此代码目前产生以下输出:
79223510|new||Tokyo|
SKU-1234567890|1|99.95|
SKU-1234567899|1|199.95|
我想要它产生以下一个:
79223510|new||Tokyo|SKU-1234567890|1|99.95|
79223510|new||Tokyo|SKU-1234567899|1|199.95|
只使用XSL转换。请指教。
P.S。添加了文本形式的XSL以便于阅读:
@<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='text' indent='no'/>
<!-- New line character variable -->
<xsl:variable name='newline'>
<xsl:text> </xsl:text>
</xsl:variable>
<xsl:variable name='empty'>
<xsl:text></xsl:text>
</xsl:variable>
<xsl:template match='/'>
<xsl:for-each select='Order'>
<xsl:value-of select='@id'/>|
<xsl:value-of select='Status'/>|
<xsl:value-of select='ShipMetod'/>|
<xsl:value-of select='ToCity'/>|
<xsl:value-of select='$newline'/>
</xsl:for-each>
<xsl:for-each select='Order/Items/Item'>
<xsl:value-of select='SKU'/>|
<xsl:value-of select='Quantity'/>|
<xsl:value-of select='Price'/>|
<xsl:value-of select='$newline'/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:2)
你犯了一些错误:
Order
元素,接下来的两行来自Item
元素。 (实际上你不需要任何循环。下面的解决方案。)ShipMetod
,所以你的一个字段是空白的。|
值。 (我的解决方案没有解决这个问题,因为我不确定你想做什么。)更好的解决方案是使用模板匹配和字符串连接。
<xsl:stylesheet version="1.0" xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method="text" indent="no" media-type="text/plain" />
<xsl:variable name='newline'><xsl:text> </xsl:text></xsl:variable>
<xsl:variable name='delimiter'>|</xsl:variable>
<!-- by default, don't copy any nodes to output -->
<xsl:template match="node()|@*"><xsl:apply-templates select="node()|@*"/></xsl:template>
<xsl:template match="/Order/Items/Item"><xsl:value-of
select="concat(
../../@id, $delimiter,
../../Status, $delimiter,
../../ShipMethod, $delimiter,
../../ToCity, $delimiter,
SKU, $delimiter,
Quantity, $delimiter,
Price, $delimiter,
$newline)"/></xsl:template>
</xsl:stylesheet>
这会产生以下输出:
79223510|new|Standard International|Tokyo|SKU-1234567890|1|99.95|
79223510|new|Standard International|Tokyo|SKU-1234567890|1|199.95|