基于已知XSL的未知XML逆向工程 解决了的!
在遵循Matti的建议后,我删除了自定义函数,一切都很好 原职: 到今天为止,我还不熟悉XSLT,所以我相信这对你们中的许多人来说都是不需要动脑筋的。无论如何:基于已知XSL的未知XML逆向工程 解决了的!,xml,xslt,reverse-engineering,Xml,Xslt,Reverse Engineering,在遵循Matti的建议后,我删除了自定义函数,一切都很好 原职: 到今天为止,我还不熟悉XSLT,所以我相信这对你们中的许多人来说都是不需要动脑筋的。无论如何: <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="facepalm.xsl"?> <NewDataSet> <Table> <paperid>123</p
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="facepalm.xsl"?>
<NewDataSet>
<Table>
<paperid>123</paperid>
<paperitemid>12345</paperitemid>
<descr>facepalm of doom</descr>
<shortdescr>facepalm</shortdescr>
<retail>true</retail>
<imageurl>http://website/facepalm.jpg</imageurl>
</Table>
<Table>
<paperid>456</paperid>
<paperitemid>67890</paperitemid>
<descr>mega-sigh</descr>
<shortdescr>sigh</shortdescr>
<retail>true</retail>
<imageurl>http://website/sigh.jpg</imageurl>
</Table>
</NewDataSet>
我的任务是为我公司的网站创建一个小部件,使用第三方供应商提供的数据
供应商拒绝向我们发送一个示例XML文件(甚至是一个只有元素标记的空白文件!),因此我尝试根据他们发送给我们的XSLT中的内容重新创建XML。(嘲笑之声不绝于耳)
这是我们收到的(剥离的)XSLT文件:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:myCustXslFunctions="urn:CustomXslFunctions">
<xsl:variable name="NumberColumns" >1</xsl:variable>
<xsl:variable name="PaperId" >1234567890ABCDEF</xsl:variable>
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="no" />
<xsl:template match="/NewDataSet">
<div><xsl:apply-templates select="/NewDataSet" mode="columns" /></div>
</xsl:template>
<xsl:template match="NewDataSet" mode="columns">
<xsl:for-each select="Table[position() mod $NumberColumns = 1 or $NumberColumns = 1]">
<p>
<xsl:for-each select=".|following-sibling::Table[position() < $NumberColumns]">
<span class="description">
<xsl:element name="a">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:attribute name="class" >description</xsl:attribute>
<xsl:choose>
<xsl:when test="retail='true'">
<xsl:attribute name="href">http://website/retail/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="imageurl != ''">
<xsl:element name="img">
<xsl:attribute name="src"><xsl:value-of select="imageurl" /></xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
<xsl:attribute name="class">thumbnail</xsl:attribute>
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="img">
<xsl:attribute name="src">http://website/thumbs/<xsl:value-of select="paperid" />_<xsl:value-of select="paperitemid" />_100.jpg</xsl:attribute>
<xsl:attribute name="border">0</xsl:attribute>
<xsl:attribute name="class">thumbnail</xsl:attribute>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:element>
</span>
</xsl:for-each>
</p>
<p>
<xsl:for-each select=".|following-sibling::Table[position() < $NumberColumns]">
<span class="description">
<xsl:element name="a">
<xsl:attribute name="target">_blank</xsl:attribute>
<xsl:attribute name="class" >description</xsl:attribute>
<xsl:choose>
<xsl:when test="retail='true'">
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="string-length(shortdescr) = 0"><xsl:value-of select="myCustXslFunctions:MakeNice(descr,20,20,'Left','true')" /></xsl:when>
<xsl:otherwise><xsl:value-of select="myCustXslFunctions:MakeNice(shortdescr,20,20,'Left','true')" /></xsl:otherwise>
</xsl:choose>
</xsl:element>
</span>
</xsl:for-each>
</p>
</xsl:for-each>
</xsl:template>
</xsl:transform>
1.
1234567890ABCDEF
_空白
描述
http://website/retail/?pid=&阿迪德=
http://website/?pid=&阿迪德=
0
缩略图
http://website/thumbs/__100.jpg
0
缩略图
_空白
描述
http://website/?pid=&阿迪德=
http://website/?pid=&阿迪德=
以及我对XML逆向工程的微弱尝试:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="facepalm.xsl"?>
<NewDataSet>
<Table>
<paperid>123</paperid>
<paperitemid>12345</paperitemid>
<descr>facepalm of doom</descr>
<shortdescr>facepalm</shortdescr>
<retail>true</retail>
<imageurl>http://website/facepalm.jpg</imageurl>
</Table>
<Table>
<paperid>456</paperid>
<paperitemid>67890</paperitemid>
<descr>mega-sigh</descr>
<shortdescr>sigh</shortdescr>
<retail>true</retail>
<imageurl>http://website/sigh.jpg</imageurl>
</Table>
</NewDataSet>
123
12345
末日之掌
掌纹
真的
http://website/facepalm.jpg
456
67890
巨大的叹息
叹息
真的
http://website/sigh.jpg
毫无疑问,我忽略了一些简单的东西,但我的XSLT新手身份已经让这成为一个多小时的项目。
非常感谢您的帮助。我的猜测更像是:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="facepalm.xsl"?>
<NewDataSet>
<Table>
<paperid>123</paperid>
<paperitemid>12345</paperitemid>
<descr>failvendor</descr>
<shortdescr>facepalm</shortdescr>
<retail>true</retail>
<imageurl>http://website/facepalm.jpg</imageurl>
</Table>
<Table>
<paperid>456</paperid>
<paperitemid>67890</paperitemid>
<descr>is fail</descr>
<shortdescr>sigh</shortdescr>
<retail>true</retail>
<imageurl>http://website/sigh.jpg</imageurl>
</Table>
</NewDataSet>
123
12345
故障供应商
掌纹
真的
http://website/facepalm.jpg
456
67890
失败了吗
叹息
真的
http://website/sigh.jpg
[]
东西并不是指元素名称的一部分,而是指元素的位置。因此,元素名只是表
descr
和paperid
元素imageurl
,则它似乎忽略了paperid
和paperitemid
,如果提供了shortdescr
,则忽略了descr
。这可能对你的任务有所帮助
…顺便说一句,如果没有实际的XML,您应该如何测试这一点?在一般情况下,仅给出一个XSLT就无法确定输入XML文件的结构 在本例中,您可能已经能够基于XSLT对XML描述进行反向工程,但在一般情况下,这是不可能正确完成的。在这种情况下,这是可能的,因为模板很小,并且每个模板都使用
XSLT是声明性的,这意味着您可以描述遇到某些节点时应该发生什么,但包含从未调用过的模板或以不明显的方式调用的模板肯定是合法的。类似地,使用
也无法洞察已知元素内部的元素
例如:
<xsl:template match="book">
<xhtml:div class="book">
<xsl:apply-templates />
</xhtml:div>
</xsl:template>
<xsl:template match="title">
<xhtml:h1><xsl:value-of select="."/></xhtml:h1>
</xsl:template>
<xsl:template match="chapter/title">
<xhtml:h2><xsl:value-of select="."/></xhtml:h2>
</xsl:template>
这本书有书名吗?书有章节吗?章节有标题吗我们不知道也不可能知道。我假设PaperId变量是在XSLT的第2行中定义的。/删除标记中的数字会给我这样的信息:“XSLT转换期间出错:调用了未知的XPath扩展函数”/“我同意,这很难测试。我们花了2天半打电话才得到XSLT文件。@jnpcl:似乎有一个名为PaperId
的变量和一个名为PaperId
的元素。您得到的错误意味着XML是正确的;但是XSLT转换试图调用命名空间urn:CustomXslFunctions
中定义的名为MakeNice
的自定义函数,这可能是缺少的。那么,您的任务是什么?您打算对他们的XML执行XSLT转换吗?任务是修改他们的XSL以满足我们网站内容框的需要,但如果没有任何XML示例,很难看到我们对设计的更改。