Archive for the 'XML' Category

Xml Schema Explorer of VS 2008 sp1

I like this feature, sweety!

vs2008 sp1 xml schema explorer

Visual Stuido XSLT Profiler

xsltprofiler_t
如果你和我一样会用Visual Studio写大量的XSLT. 那么这个vs的plug-in你绝对不能错过, XSLT Profiler for Visual Studio (still CTP) (doc: XSLT profiler doc).  更希望有个msbuild task.

Get processing-instruction in XPath

xpath中有3个特殊的方法, 分别是comment(), text() 和processing-instruction(); 后者可以用来获得xml的processing instruction.

    <xslt:for-each select=”processing-instruction(’DocumentId’)”>
      <xslt:value-of select=”.”/>
    <xslt:for-each>

processing-instruction同时也支持用processor的name来做参数. 如processing-instruction(’name’)

XML Learning & JSON

JSON正在越来越收到欢迎. 那天在和cmsware的Hawking聊天的时候, Hawking就提到他在Ajax的时候越来越多的使用JSON, 因为无论是在Javascript还是在PHP中使用JSON都非常simple. 对此我非常认同, JSON既简单又极具扩展性. 特别是那种类似hash的access方式, 比起DOM来确实方便. 于是很多人提出用JSON替换XML在Ajax中的位置.

在我看来很多人不喜欢XML的原因在于XML的解析稍显复杂, 并因此认为XML不好用, 效率低. 比如很多人不喜欢用Spring是因为Spring的配置方式. 在我没用从事现在的开发工作前, 我对XML也挺发怵的, 觉得XML挺复杂的. 然而实质却是XML太简单了, 本质就是Element. 所以我现在的生活已经无法缺少XML了, 当然这也和我的工作有关.

个人认为学习并掌握所有XML的相关基础知识的时间不超过1个月. 而几个最基础的点在于XML, XML Schema(DTD就算了, 了解一下即可), XPath,  XSLT.  并且这些基础点都可以同步进行, 互相促进.  而且由于Schema和XSLT本身就是XML, 所以学习起来相当简单. Schema些许难掌握的是ComplexType, 而XSLT的难点在于递归, 如果这些都掌握了. 那XML就是非常有力的武器, 在你的Application的各个方面.

JSON最具杀伤力的弱点在于没有Validation. :D 同时没有XML便于查询的优点, 并且没有XSL那样的转换技术. 所以本质上来说JSON仅是一种数据交换方式.

Schema Tip: 允许无序数量不定的Element

有时候会需要这样的定义. 比如在某个element下面只能有某些element出现, 但是element又是无序无数量要求的. 那下面的schema可以做到. 

<?xml version=”1.0″ encoding=”utf-8″ ?>
<xs:schema id=”test”
                  xmlns:xs=”http://www.w3.org/2001/XMLSchema”>
  <xs:element name=”ele1″>
    <xs:complexType>
      <xs:group ref=”group1″ maxOccurs=”unbounded” minOccurs=”0″/>
    </xs:complexType>
  </xs:element>
  <xs:group name=”group1″>
    <xs:choice>
      <xs:element name=”tag1″ type=”xs:string” />
      <xs:element name=”tag2″ type=”xs:string” />
      <xs:element name=”tag3″ type=”xs:string” />
    </xs:choice>
  </xs:group>
</xs:schema>

Get InnerXML from XPathNavigator

First thanks a lot to my colleague, Eunge. He helped me to solve this problem. And it’s really a long time I haven’t written .Net codes.

Sometimes we need to get InnerXML from the XPathNodeIterator, especially when you are writing codes in XSLT.

If you are using .Net Framework 2.0, it’s very easy. There is a property, InnerXML, in XPathNavigator, and from disassembled code we could find new functionalities of XMLWriter. But if you are using .Net 1.1, it’s really not quite convenient to do it. And there are two concrete classes which inherit abstract class XPathNavigator. One is XPathDocumentNavigator, the other is DocumentXPathNavigator. There is an important difference between two classes. DocumentXPathNavigator implements the interface IHasXmlNode, but XPathDocumentNavigator not. That means if your XPathNavigator is DocumentXPathNavigator, it’s easy to get InnerXML.

[code=csharp]
XPathNavigator nav = dom.CreateNavigator();
// do some select
String innerXml = ((IHasXmlNode)nav).GetNode().InnerXML;
[/code]

(more…)