为XML元素添加ID

首先下载Apache xalan,http://xml.apache.org/xalan-j/downloads.html

使用windows命令行模式,因为我没有设置java path,所以使用了如下的命令行:

java -cp xalan.jar;serializer.jar;xml-apis.jar;xercesImpl.jar org.apache.xalan.xslt.Process -IN books.xml -XSL idmaker2.xsl -OUT test.xml

其中IN后面是你要转化的源文件

<?xml version="1.0" encoding="ISO-8859-1"?> <bookstore> <ack>Success</ack> <version>1.0.1</version> <timestamp>2009-10-13T16:32:00.614Z</timestamp> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">XQuery Kick Start</title> <author>James McGovern</author> <author>Per Bothner</author> <author>Kurt Cagle</author> <author>James Linn</author> <author>Vaidyanathan Nagarajan</author> <year>2003</year> <price>49.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>

XSL后面是XSLT文件

<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" indent="yes" /> <xsl:template match="*"> <xsl:variable name="special-id"> <xsl:for-each select="ancestor-or-self::*"> <xsl:value-of select="generate-id(.)"/> </xsl:for-each> </xsl:variable> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:attribute name="ID"><xsl:value-of select="$special-id"/></xsl:attribute> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet>

OUT是输出文件

<?xml version="1.0" encoding="UTF-8"?><bookstore ID="N10001"> <ack ID="N10001N10004">Success</ack> <version ID="N10001N10007">1.0.1</version> <timestamp ID="N10001N1000A">2009-10-13T16:32:00.614Z</timestamp> <book category="COOKING" ID="N10001N1000D"> <title lang="en" ID="N10001N1000DN10010">Everyday Italian</title> <author ID="N10001N1000DN10014">Giada De Laurentiis</author> <year ID="N10001N1000DN10017">2005</year> <price ID="N10001N1000DN1001A">30.00</price> </book> <book category="CHILDREN" ID="N10001N1001E"> <title lang="en" ID="N10001N1001EN10021">Harry Potter</title> <author ID="N10001N1001EN10025">J K. Rowling</author> <year ID="N10001N1001EN10028">2005</year> <price ID="N10001N1001EN1002B">29.99</price> </book> <book category="WEB" ID="N10001N1002F"> <title lang="en" ID="N10001N1002FN10032">XQuery Kick Start</title> <author ID="N10001N1002FN10036">James McGovern</author> <author ID="N10001N1002FN10039">Per Bothner</author> <author ID="N10001N1002FN1003C">Kurt Cagle</author> <author ID="N10001N1002FN1003F">James Linn</author> <author ID="N10001N1002FN10042">Vaidyanathan Nagarajan</author> <year ID="N10001N1002FN10045">2003</year> <price ID="N10001N1002FN10048">49.99</price> </book> <book category="WEB" ID="N10001N1004C"> <title lang="en" ID="N10001N1004CN1004F">Learning XML</title> <author ID="N10001N1004CN10053">Erik T. Ray</author> <year ID="N10001N1004CN10056">2003</year> <price ID="N10001N1004CN10059">39.95</price> </book> </bookstore>

参考:

http://xml.apache.org/xalan-j/commandline.html

http://lab.tojio.com/2009/03/06/unique-id-for-each-node-in-an-xml-file-en/

http://www.stylusstudio.com/xsllist/200512/post30240.html

http://www.roseindia.net/xml/dom/

你可能感兴趣的:(xml,Web,XSL,encoding,XSLT,stylesheet)