XSLT:文档分析:如何输出文档内的唯一路径?
为了帮助对 XML 文件进行逆向工程,我使用了如下的 Python SAX 处理程序。 有人可以提供等效的 XSLT 来执行相同的工作吗? 这是一个示例输入文件:
<beatles>
<beatle>
<name>
<first>John</first>
<last>Lennon</last>
</name>
</beatle>
<beatle>
<name>
<first>Paul</first>
<last>McCartney</last>
</name>
</beatle>
<beatle>
<name>
<first>George</first>
<last>Harrison</last>
</name>
</beatle>
<beatle>
<name>
<first>Ringo</first>
<last>Starr</last>
</name>
</beatle>
</beatles>
因此,其想法是获取所有唯一路径(忽略属性)的列表,以获得编写模板等的基本起点。
from xml.sax.handler import ContentHandler
from xml.sax import make_parser
from xml.sax import SAXParseException
class ShowPaths(ContentHandler):
def startDocument(self):
self.unique_paths=[]
self.current_path=[]
def startElement(self,name,attrs):
self.current_path.append(name)
path="/".join(self.current_path)
if path not in self.unique_paths:
self.unique_paths.append(path)
def endElement(self,name):
self.current_path.pop();
def endDocument(self):
for path in self.unique_paths:
print path
if __name__=='__main__':
handler = ShowPaths()
saxparser = make_parser()
saxparser.setContentHandler(handler)
in_f=open("d:\\beatles.xml","r")
saxparser.parse(in_f)
in_f.close()
以及在示例上运行程序的结果:
beatles
beatles/beatle
beatles/beatle/name
beatles/beatle/name/first
beatles/beatle/name/last
To help reverse engineer XML files, I'm using a Python SAX handler as below.
Can somebody provide an equivalent XSLT to perform the same job ?
This is an example input file:
<beatles>
<beatle>
<name>
<first>John</first>
<last>Lennon</last>
</name>
</beatle>
<beatle>
<name>
<first>Paul</first>
<last>McCartney</last>
</name>
</beatle>
<beatle>
<name>
<first>George</first>
<last>Harrison</last>
</name>
</beatle>
<beatle>
<name>
<first>Ringo</first>
<last>Starr</last>
</name>
</beatle>
</beatles>
So the idea is to get a list of all unique paths (ignoring attributes) to get a basic starting point to writing templates etc.
from xml.sax.handler import ContentHandler
from xml.sax import make_parser
from xml.sax import SAXParseException
class ShowPaths(ContentHandler):
def startDocument(self):
self.unique_paths=[]
self.current_path=[]
def startElement(self,name,attrs):
self.current_path.append(name)
path="/".join(self.current_path)
if path not in self.unique_paths:
self.unique_paths.append(path)
def endElement(self,name):
self.current_path.pop();
def endDocument(self):
for path in self.unique_paths:
print path
if __name__=='__main__':
handler = ShowPaths()
saxparser = make_parser()
saxparser.setContentHandler(handler)
in_f=open("d:\\beatles.xml","r")
saxparser.parse(in_f)
in_f.close()
And the result of running the program over the example:
beatles
beatles/beatle
beatles/beatle/name
beatles/beatle/name/first
beatles/beatle/name/last
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这很容易:
当此转换应用于提供的 XML 文档时:
生成所需的正确结果:
This is easy:
when this transformation is applied on the provided XML document:
the wanted, correct result is produced:
我可能没有抓住要点,但我理解这个问题意味着您想要唯一的命名路径。
因此,从这个 XSL:
我得到以下输出:
I might be missing the point here but I understood the question to mean that you wanted unique named paths.
So from this XSL:
I get the following output: