Xml转换查询, [英] Xml transform query,
问题描述
您好。毫无疑问,对你们来说很简单。
我有一个XML需要转换成另一个XML,带一些元素并写入新元素等......
我已经大大减少了文件,希望足以让你指出正确的方向。
Hi. No doubt an easy one to you experts.
I have an XML that I need to transform into another XML, taking some of the elements and writing to new elements etc...
I have cut the file down considerably, hopefully enough for you to point me in the right direction.
<?xml version="1.0" encoding="utf-8"?>
<kmsg xmlns:env="http://xml.changedthisbit" xmlns="http://xml.changedthisbit">
<header>
<env:envelope>
<env:payload>ORDER</env:payload>
<env:cfcompany>01</env:cfcompany>
<env:service>LIVE</env:service>
<env:source branch="" endpoint="" machine="" password="" />
<env:destination branch="0002" endpoint="" machine="" />
</env:envelope>
</header>
<body>
<Order xmlns="urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01">
<OrderDate>2012-08-08T09:40:00</OrderDate>
<Extensions>
<WebOrder>true</WebOrder>
</Extensions>
<OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
<Product>
<ProductId>1234</ProductId>
</Product>
</OrderLine>
<OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
<Product>
<ProductId>9876</ProductId>
</Product>
</OrderLine>
</Order>
</body>
</kmsg>
我只是在输出时遇到麻烦OrderDate。我得到这个输出。
I am having trouble simply just outputting the OrderDate. I just get this output.
<?xml version="1.0" encoding="iso-8859-1"?>
我还没有为订单行开始每个... 。
希望你能指出我正确的方向,(我每次尝试都会填充发誓罐)。我知道它一定很简单。提前致谢。
我的尝试:
我有这个转换: -
I haven't started on the for each for the order lines....
Hoping you will be able to point me in the right direction, (I am filling the swear jar with every attempt I make). I know it must be something simple. Thanks in advance.
What I have tried:
I have this transform:-
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="iso-8859-1"/>
<xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="/">
<header>
</header>
<body>
<xsl:value-of select = "kmsg/body/Order/OrderDate"/>
</body>
</xsl:template>
</xsl:stylesheet>
推荐答案
我将xml重新格式化为:
I reformatted the xml to this:
<?xml version="1.0" encoding="utf-8"?>
<body>
<Order>
<OrderDate>2012-08-08T09:40:00</OrderDate>
<Extensions>
<WebOrder>true</WebOrder>
</Extensions>
<OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
<Product>
<ProductId>1234</ProductId>
</Product>
</OrderLine>
<OrderLine Action="Add" TypeCode="New" TypeDescription="New Item">
<Product>
<ProductId>9876</ProductId>
</Product>
</OrderLine>
</Order>
</body>
然后运行此代码:
And then ran this code:
XElement _x = XElement.Load(@"order.xml");
var orders = from el in _x.Elements("Order") select el;
foreach (var order in orders)
{
Debug.Print(order.Element("OrderDate").Value);
}
您可以像这样自动清理XML:
You could clean up the XML in an automated way like this:
XDocument doc = XDocument.Load("order.xml");
// Strip namespace...
foreach (var node in doc.Root.Descendants()
.Where(n => n.Name.NamespaceName == "urn:schemas-basda-org:2000:purchaseOrder:xdr:3.01"))
{
// Remove the xmlns='' attribute.
node.Attributes("xmlns").Remove();
node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
}
Debug.Print(doc.ToString()); // Or doc.Save(...)
这篇关于Xml转换查询,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!