|
how I can create multiple XML files from a single XML file using xsltproc?
|
|
|
|
|
If you are using XSL 2, you can use xsl:result-document to target multiple output documents.
|
|
|
|
|
I must use XSLTProc
it does not support XSLT 2.0
|
|
|
|
|
The only way I can see to do this with this tool then is to have multiple XSLT files and run them in sequence. It's not pretty, but XSLT 1 was not designed for a 1-in to many-out scenario.
|
|
|
|
|
|
You are welcome. I hope we have helped.
|
|
|
|
|
Good afternoon.
I have the following XML file structure:
<?xml version="1.0" ?>
- <WEB_SITE>
- <RECORD>
<Name>Homepage</Name>
<Address>http:
</RECORD>
</WEB_SITE>
I would like to derive the URL from the XML file if the name is Homepage.
I have this so far:
string strtmpName = string.Empty;
string strtmpAddress = string.Empty;
XmlTextReader reader = null;
XDocument xmlDoc = new XDocument();
reader = new XmlTextReader(cv.StrIndivPath1 + strLANID + cv.StrIndivPath2 + "Website.xml");
xmlDoc = XDocument.Load(reader);
reader.Close();
XElement Results = XElement.Parse(xmlDoc.ToString());
foreach (XElement xe in Results.Elements("RECORD").Descendants())
{
switch (xe.Name.ToString())
{
case "Name":
if (xe.Value.ToLower() == "homepage")
{
strtmpAddress = Results.Elements("RECORD").Descendants("Address").ToString();
MessageBox.Show(strtmpAddress);
}
break;
default:
break;
}
}
Any suggestions? Thank you, WHEELS
|
|
|
|
|
XPath can be used. Here is how it works:
String myXPath="/WEB_SITE/RECORD[Name='Homepage']/Address";
XmlNodeList myNodeList=xmlDoc.SelectNodes(myXPath);
XmlNode myXmlNode;
foreach (myXmlNode in xmlNodeList)
{
MessageBox.Show(myXmlNode.Value);
}
Cheers!
|
|
|
|
|
I am getting a Type and identifier are both required in a forach statement.
|
|
|
|
|
well you have to add this :
using System.Xml.Xpath;
so that it can identify the XPath.
|
|
|
|
|
Excellent. That does the trick. Thank you.
|
|
|
|
|
|
In order to use SelectNodes to select nodes and attributes that have a namespace prefix, you need an XmlNamespaceManager that knows about the namespace and prefix. Unfortunately, I haven't found a way to access a pre-populated XmlNamespaceManager or to automagically populate one from an XmlDocument.
According to the documentation, XmlReaders use XmlNamespaceManagers, so after I load a document with an XmlReader I should be able to copy the XmlReader's XmlNamespaceManager -- but it shows null (and requires Reflection). I should be able to pass an XmlNamespaceManager to an XmlReader and the XmlReader should use it -- but it still shows null.
I've gone through a bunch of documentation and searching articles here and elsewhere hasn't yielded a silver bullet. They all show code calling AddNamespace to add specific namespaces -- I need it to be dynamic.
So I'm trying to roll my own. At this point, I'm loading the document with an XmlReader and then trying to query any attributes in the xmlns namespace, I should then be able to populate an XmlNamespaceManager with AddNamespace.
The code, as I have it now (I've tried several variations of the XPath) is:
System.Xml.XmlNamespaceManager result =
new System.Xml.XmlNamespaceManager ( Document.NameTable ) ;
System.Xml.XmlNodeList nodes = Document.DocumentElement.SelectNodes ( "//@xmlns:*" , result ) ;
This should yield all the attributes in the xmlns namespace (and the document contains one), but the list comes up empty.
0) Can anyone provide me with an XPath that will do what I want?
1) Does anyone know how to get a pre-populated XmlNamespaceManager from an XmlReader or an XmlDocument?
|
|
|
|
|
I used this:
if (namespace_found)
{
nsmgr = gcnew XmlNamespaceManager (doc->NameTable);
nsmgr->AddNamespace(L"ie", DocNamespace);
}
namespace_found is set to true when a namespace and schema are present on the XML file.
I couldnt add a schema to the XML file at run time.
GER
Ger
|
|
|
|
|
In order to at least have something that works, I now have:
public static System.Xml.XmlNamespaceManager
GetNamespaceManager
(
System.Xml.XmlDocument Document
)
{
System.Xml.XmlNamespaceManager result =
new System.Xml.XmlNamespaceManager ( Document.NameTable ) ;
System.Xml.XmlNodeList nodes = Document.DocumentElement.SelectNodes ( "//*" , result ) ;
foreach
(
System.Xml.XmlNode nod
in
nodes
)
{
foreach ( System.Xml.XmlAttribute att in nod.Attributes )
{
if ( att.Prefix == "xmlns" )
{
result.AddNamespace ( att.LocalName , att.Value ) ;
}
}
}
return ( result ) ;
}
Which gets the job done, and I suppose that's what the framework would be doing anyway so it shouldn't be too much slower. But, if the XmlReader builds one anyway, I sure wish I could access it and not have to build my own.
|
|
|
|
|
There's a Scott Hanselman post here[^] that talks about using an XPathNavigator to get namespaces once it has a currentNode for scope. So this kind of thing works:
XmlNamespaceManager nsmgr = new XmlNamespaceManager( Document.NameTable );
XPathNavigator nav = Document.CreateNavigator();
nav.MoveToFollowing( XPathNodeType.Element );
foreach (var ns in nav.GetNamespacesInScope( XmlNamespaceScope.ExcludeXml ))
{
nsmgr.AddNamespace( ns.Key, ns.Value );
}
As you say, the framework may be doing the same work in each case.
|
|
|
|
|
Hmmm... interesting, thanks, I'll have to take a deeper look. So far I've just looked to see whether or not I can access a NamespaceManager within that, but I didn't see one.
|
|
|
|
|
hello guys,
I am reading xml file and showing in grid in c# but now i want to read specific record(node) of that xml file like where clouse in sqlserver
Example
<Root>
<Data id="1" Name="aaa" Phone="852963" Gender="Male" />
<Data id="2" Name="bbb" Phone="123456789" Gender="Female" />
<Data id="3" Name="ccc" Phone="987654" Gender="Male" />
<Data id="3" Name="ddd" Phone="7676767" Gender="Male" />
</Root>
i need to read the first(TOP)id record and who's Gender="male".
how to do this
Regards
Shafiq
|
|
|
|
|
Try Bipin Joshi's book "Beginning XML with C# 2008" by Apress. I think thats there.
~GER
Ger
|
|
|
|
|
|
Here is the Answer!
XmlDocument xmlDoc =new XmlDocument;
xmlDoc.Load("C:\MyXml");
string myXPath="/Root/Data[@Gender='Male']";
XmlNodeList NodeList=xmlDoc.SelectNodes(myXPath);
XmlNode xmlNod=NodeList(0);
Int myId=xmlNod.Attributes("id").Value;
Cheers!
|
|
|
|
|
It would be much better if you use XQuery or LINQ which has the same querying pattern like that of SQL. But i am still in the learning phase, so once i get a good hold in it, i shall write my answer.
|
|
|
|
|
XPath is easy to use, uses a hierarchical syntax and is available in many languages.
LINQ is far more flexible, but is .NET-centric.
|
|
|
|
|
hi i am new to xml but i wrote the xml file using xml writer . but the header information is not oming properly. i need the output like this.
<Class xmlns:tns="http://www....." xmlns:xsi="http//...." xsi:text="http://www......">
|
|
|
|
|
Hi All,
I am creating xsd files by using Datasets in my application. I have three datasets, when I create them using dt.WriteXMLSchema, all three datasets are cerating xsd with same class name inside. When I include them in my application its giving me error that same functions are defined.
Is there any way to resolve this problem so that all three xsd should come with their own table names or different names. Here the class names should come differently than "NewDatset".
Any links or any suggestion would be great helpfull. Thanks in advance.
Thanks & Regards,
Md. Abdul Aleem
NIIT technologies
|
|
|
|