基于已知 XSL 对未知 XML 进行逆向工程 [英] Reverse-Engineering unknown XML based on known XSL

查看:29
本文介绍了基于已知 XSL 对未知 XML 进行逆向工程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照 Matti 的建议,我删除了自定义函数,一切正常.

After following Matti's suggestions, I removed the custom functions and all is well.

我今天刚接触 XSLT,所以我相信这对你们中的许多人来说是显而易见的.无论如何:

我的任务是为我公司的网站创建一个小部件,该小部件使用第三方供应商提供的数据.

I've been tasked with creating a widget for my company's website that uses data provided by a 3rd-party vendor.

供应商拒绝向我们发送示例 XML 文件(甚至是一个只有元素标签的空白文件!)所以我试图根据我在其中看到的内容重新创建 XML他们确实发给我们的 XSLT.(嘲笑比比皆是)

The vendor refuses to send us a sample XML file (even a blanked-out one with just the element tags!) so I'm trying to recreate the XML based on what I can see in the XSLT that they -did- send us. (ridiculosity abounds)

这是我们发送的(剥离的)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() &lt; $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" />&#38;adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&#38;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() &lt; $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" />&#38;adid=<xsl:value-of select="paperitemid" /></xsl:attribute>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:attribute name="href">http://website/?pid=<xsl:value-of select="$PaperId" />&#38;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>

以及我对 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>

在我看来,毫无疑问,我忽略了一些简单的事情,但我对 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>

  1. [] 内容不是指元素名称的一部分,而是指元素的位置.所以元素名称只是Table.
  2. 您错过了 descrpaperid 元素.
  1. The [] stuff doesn't refer to parts of the element name, it refers to the position of the element. So the element name is just Table.
  2. You missed the descr and paperid elements.

XSLT 似乎正在做的是在列中的列表中布置项目.是的,XSLT 就是这么复杂.

What the XSLT seems to be doing is laying out items on a list in columns. Yes, it is that ridiculously complicated in XSLT.

此外,如果定义了 imageurl,它似乎会忽略 paperidpaperitemid,而忽略 descr如果提供了 shortdescr.这可能会帮助您完成任务.

Also, it would seem that it's ignoring paperid and paperitemid if imageurl is defined, and ignoring descr if shortdescr is provided. This might help you on your quest.

...你应该如何在没有实际 XML 的情况下测试这个,顺便说一句?

...how are you supposed to test this without the actual XML, btw?

这篇关于基于已知 XSL 对未知 XML 进行逆向工程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆