|
I am quite new to XSLT and I want to add unique index to elements types to transform something like this:
<item1>
<item2>
<item3></item3>
<item3></item3>
</item2>
<item2>
<item3></item3>
<item3></item3>
</item2>
</item1>
<item1>
</item1>
into:
<item1 id="1">
<item2 id="1">
<item3 id="1"></item3>
<item3 id="2"></item3>
</item2>
<item2 id="2">
<item3 id="3"></item3>
<item3 id="4"></item3>
</item2>
</item1>
<item1 id="2">
</item1>
I tried using position() but it counts based on context and not globally on document.
Thanx.
|
|
|
|
|
I have got the answer from daniweb (thanks fpmurphy) :
1. <?xml version="1.0"?>
2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
3. <xsl:output method="xml" indent="yes"/>
4.
5. <xsl:template match="/">
6. <xsl:apply-templates/>
7. </xsl:template>
8.
9. <xsl:template match="*">
10. <xsl:variable name="name" select="name()" />
11. <xsl:element name="{name()}">
12. <xsl:attribute name="id">
13. <xsl:value-of select="count(preceding::*[name()=$name]) + 1" />
14. </xsl:attribute>
15. <xsl:apply-templates />
16. </xsl:element>
17. </xsl:template>
18.
19. </xsl:stylesheet>
|
|
|
|
|
Hello Guys
I am new to XML. I have started reading XML few days back.
I have a question. Consider a table calling "Catalog" and has following
column names "book" , "author", "title", "genre", "price", "publish_date"
"description".
XML representation of Catalog Table
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
<catalog>
Compressed format of catalog table
<?xml version="1.0"?>
<Table = "catalog" enum{ book = 1, author = 2, title = 3, genre = 4, price = 5, publish_date = 6, description = 7}> </Table>
<catalog>
<1 id="bk101">
<2>Gambardella, Matthew</2>
<3>XML Developer's Guide</3>
<4>Computer</4>
<5>44.96</5>
<6>2000-10-01</6>
<7>An in-depth look at creating applications
with XML.</7>
</1>
<1 id="bk102">
<2>Ralls, Kim</2>
<3>Midnight Rain</3>
<4>Fantasy</4>
<5>6.96</5>
<6>2000-12-16</6>
<7>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</7>
</1>
<catalog>
So compressed format requires 20% less memory. I know that XML is user readable also. But while transferring huge XML data over the network, compressed format could improve the performance. And at the end (destination) we can decompress the compressed XML data.
So my question is Why cant we use compressed XML???
I have no idea about this. Please let me correct if i am wrong.
Thanks
Bipin Dalbhide
|
|
|
|
|
As I recall there is no need to change the XML structure, you can just zip (rar or whatever) the original file, send it and unzip it after (compressing algorithms are optimized for that)! For example I think that is what "new" Microsoft Word docx is all about.
|
|
|
|
|
we can zip compressed file.
|
|
|
|
|
I need a program in PHP which can extract the hidden biders from ebay.I will have to insert the item number and the program must show me all bidders of the auction and their emails.
Also the same program must extract the registered contact information for any specified bidder specially the email address
I will pay for this
If someone can help please contact me at totos_back@yahoo.com
Thanks
|
|
|
|
|
totolcm wrote: I need a program in PHP
And why you post in XML / XSL?
|
|
|
|
|
Because he's an a**hole who has posted everywhere, looking for some (probably) illegal code.
Bob
Ashfield Consultants Ltd
Proud to be a Code Project MVP
|
|
|
|
|
Maybe he is hoping for a prize.
I count 12 cross posts.
"GOLD award - for the most cross-posts on a single subject"
modified on Thursday, January 14, 2010 1:03 PM
|
|
|
|
|
Greeting!
As declared in w3.org there is predefined derived type in XSD schemas - the ENTITY. This type is used for attributes, which means are unparsed entities from DTD. Using of the ENTITY as attribute type in schema is not problem (no errors for the schema self-validation) but:
1. XML file didn't pass the schema validation - the XML file didn't find unparsed entities declared in the schema DTD (that's right - it is the schema DTD only) and from other hand the XML file DTD eliminates schema using (that's right too - the DTD is closed model). How to use the ENTITY type?
2. In XSD schemas there is predefined base type - NOTATION. The NOTATION takes values from the schema set of <notation> elements. In other words this replaces using of DTD NOTATION as attribute type. May be it is for the ENTITY type too?
|
|
|
|
|
You can't declare entities in XML Schemas. You can declare them in XML documents that are instances of the schema.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
No, IMHO it is impossible to declare entities in XML document if the document has schema because entities are declared in DTD and using of the DTD excludes using of the schema (DTD is closed model).
|
|
|
|
|
Sorry - it is possible - I've tried it successfully with Microsoft's XML parser.
See this page[^] of the XML schema recommendation which gives the example I used.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Sorry sir, can You show how You succeed? I can't validate this example. I have used Microsoft XML paser (DOM). There is error - Element "purchaseOrder" is used but not defined in DTD and schema. But there is the validation when I am commenting DTD in the XML file (and deleting symbol "&" for conversion entity to string literal). My JScript code fragment follows. Tell me please, where I have mistaken?
var xmldoc = WScript.CreateObject("MsXml2.DomDocument.5.0");
xmldoc.preserveWhiteSpace = false;
xmldoc.async = false;
xmldoc.validateOnParse = false;
xmldoc.load("a.xml");
printResult(xmldoc.parseError);
var schema = WScript.CreateObject("Msxml2.XMLSchemaCache.5.0");
schema.add("http://www.example.com/PO1", "a.xsd");
xmldoc.schemas = schema;
printResult(xmldoc.validate());
|
|
|
|
|
I used the .NET XML parser.
Here's my little C# program:
using System;
using System.Xml;
using System.Xml.Schema;
namespace a
{
class Program
{
static public void vvv(Object sender, ValidationEventArgs e)
{
Console.WriteLine(e.Message);
throw new XmlSchemaValidationException();
}
static void Main(string[] args)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = false;
xmldoc.Load("Changes.xml");
Console.WriteLine(xmldoc.DocumentElement.Name);
Console.WriteLine("The document has {0} entities", xmldoc.DocumentType.Entities.Count);
XmlSchemaSet schema = new XmlSchemaSet();
schema.Add("", "change-history.xsd");
xmldoc.Schemas = schema;
xmldoc.Validate(vvv);
Console.WriteLine("Document validated!");
}
catch (XmlSchemaValidationException)
{
Console.WriteLine("Document didn't validate!");
}
}
}
}
Here's my schema (called change-history.xsd)
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:element name="change-history">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="item">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="version">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="change">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="reason" type="xs:string" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="number" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="product" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
And here's my sample XML file
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE change-history [
<!ENTITY eacute "é">
]>
<change-history product="Sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="change-history.xsd">
<items>
<item name="Sample.exe">
<version number="2.21.0">
<change reason="issue 11">Added license to executable.</change>
<change>éFixed bug in executable.</change>
</version>
</item>
</items>
</change-history>
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Trez bien! Thank You very much! Your code is working without errors, validation is passed. But Your code converted to JScript is working with the same errors. Hence there are errors in the Ms COM implementation of Xml parser.
|
|
|
|
|
It does sound like the MS XML parser isn't complete to the standard You would have thought they'd have used the same one for both, wouldn't you
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
Hi! Unfortunly, our long and winding road is continuing. The Ms .NET XML parser is not finished too. After passing Your successfull example, I tried to pass example of using unparsed entity. I added some entity and it notation in the DTD and some attribute definition of the ENTITY type in the schema. After I added this attribute with the unparsed entity value and executed Your program. The test was ok. But after I assigned to the attribute improper value (not unparsed entity) and test was once again ok! After my tests I have a mind that the parser checks attributes of the ENTITY type as of the the NCName type (the direct ancestor of the ENTITY type). The XML file and schema is here:
Changes.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE change-history [
<!ENTITY eacute "é">
<!NOTATION MyNotation SYSTEM "My Notation">
<!ENTITY MyEntity SYSTEM "My Entity" NDATA MyNotation>
]>
<change-history product="Sample" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="change-history.xsd">
<items>
<item name="Sample.exe">
<version number="2.21.0" entity = "MyEntity">
<change reason="issue 11">Added license to executable.</change>
<change>éFixed bug in executable.</change>
</version>
</item>
</items>
</change-history>
change-history.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xs:element name="change-history">
<xs:annotation>
<xs:documentation>Comment describing your root element</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence>
<xs:element name="items">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="item">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="version">
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="change">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="reason" type="xs:string" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="number" type="xs:string" use="required"/>
<xs:attribute name="entity" type="xs:ENTITY" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="product" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:schema>
From other hand, the DTD validation is strict - only unparsed entity can by the value of the ENTITY attribute. For the proving the XML file and C# program is here:
example.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE change-history [
<!ENTITY eacute "é">
<!NOTATION MyNotation SYSTEM "My Notation">
<!ENTITY MyEntity SYSTEM "My Entity" NDATA MyNotation>
<!ELEMENT change-history EMPTY>
<!ATTLIST change-history entity ENTITY #REQUIRED>
]>
<change-history entity = "MyEntity"/>
the program
using System;
using System.Xml;
using System.Xml.Schema;
namespace a
{
class Program
{
static void Main(string[] args)
{
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = false;
XmlValidatingReader reader =
new XmlValidatingReader(new XmlTextReader("Example.xml"));
reader.ValidationEventHandler +=
new ValidationEventHandler(vh);
reader.ValidationType = ValidationType.DTD;
xmldoc.Load(reader);
Console.WriteLine(xmldoc.DocumentElement.Name);
Console.WriteLine("The document has {0} entities", xmldoc.DocumentType.Entities.Count);
Console.WriteLine("Document validated!");
}
catch (XmlSchemaValidationException)
{
Console.WriteLine("Document didn't validate!");
}
}
static private void vh(Object sender, ValidationEventArgs args)
{
Console.WriteLine(args.Message);
throw args.Exception;
}
}
}
Of course, we can use double validation - at begining to use a schema validation and after success to use own validation routine checking the unparsed entities. But IMHO it's not fine decision.
|
|
|
|
|
I get an error
XQuery Execution Error! Unexpected token - "($email, '[A-Z0-9._%+-]+@"
in the following script
xquery version "1.0";
declare function local:valid-email($email as xs:string)
as xs:boolean
{
return matches($email, '[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}')
};
Why does it occur?
Thanks --
Greetings - Jacek
|
|
|
|
|
Doesn't the 'matches' function need a namespace prefix?
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
MVP for 2010 - who'd'a thunk it!
|
|
|
|
|
See my post below. That was a pure syntax error.
Greetings - Jacek
|
|
|
|
|
I have been programming in C too long. There is no "return" instruction in XQuery which means "return". It is a part of from(...) statement exclusively.
Solution:
xquery version "1.0";
declare function local:valid-email($email as xs:string)
as xs:boolean
{
matches($email, '[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}')
};
Sorry for not posting the solution earlier.
Greetings - Jacek
|
|
|
|
|
When I try to open xml, that i created myself with IE, i get error: "There was a problem sending the command to the program". With this error showed IE also opens this xml. On the other side when i open it with Firefox, it opens xml without errors. What could be wrong with xml, that IE doesn't open it without errors?
|
|
|
|
|
|
how i can get value of tag
in xml file with C# codes.
example :
i wanna get value of Hello
|
|
|
|