2017-08-03 50 views
0

我有xml中的命名空间问题(它是来自供应商的xml),以及如何在xsl上解决这个问题。namespace from xml in xsl template

我的XML:

<?xml version="1.0" encoding="utf-8"?> 
    <string xmlns="https://www.website.com/getdata/service/" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xmlresponse> 
     <header> 
     <reportinformation> 
      <time>27/06/2017 19:42:07</time> 
      <reporttype>getcompanyinformation</reporttype> 
     </reportinformation> 
     </header> 
    </xmlresponse> 
    </string> 

我的XSL是

?xml version="1.0"?> 
<xsl:stylesheet version="1.0" 
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:my="https://www.website.com/getdat/service/"> 
<xsl:output indent="yes" method="xhtml" omit-xml-declaration="yes"/> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 

<xsl:template match="/my:xmlresponse"> 
<html> 
    <head> 
    <title>www.website.com GetData XML Stylesheet</title> 
    </head> 
    <body> 
    <table> 
     <tr> 
     <td><span style="{$reporttitle}">GetData </span></td> 
     </tr> 

     <xsl:apply-templates select="my:reportinformation"/> 

    </table> 
    </body> 
</html> 
</xsl:template> 

<xsl:template match="my:reportinformation"> 
<tr> 
    <td align="left"> 
    <table style="{$datatable}"> 
     <tr> 
     <td><span style="{$prompt}">Timestamp:</span></td> 
     <td><span style="{$data}"><xsl:value-of select="my:time"/></span></td> 
     </tr> 
     <tr> 
     <td><span style="{$prompt}">Report type:</span></td> 
     <td><span style="{$data}"><xsl:value-of select="my:reporttype"/></span></td> 
     </tr> 
    </table> 
    </td> 
</tr> 
</xsl:template> 

问题我已经被显示来自模板reportinformation信息,但不匹配/显示来自主模板信息。

我不擅长xml,更多的是在前端。好朋友要正确转换什么?

我搜索SO和谷歌,但仍然或显示主要模板或子模板。

回答

1

你有大量的语法错误,你应该能够通过你的样式与这一个比较,以检测:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
xmlns:my="https://www.website.com/getdata/service/" 
exclude-result-prefixes="my"> 

<xsl:template match="/my:string"> 
    <html> 
     <head> 
      <title>www.website.com GetData XML Stylesheet</title> 
     </head> 
     <body> 
      <table> 
       <tr> 
        <td> 
         <span style="{UNDEFINED_VARIABLE}">GetData </span> 
        </td> 
       </tr> 
       <xsl:apply-templates select="my:xmlresponse/my:header/my:reportinformation"/> 
      </table> 
     </body> 
    </html> 
</xsl:template> 

<xsl:template match="my:reportinformation"> 
    <tr> 
     <td align="left"> 
      <table style="{UNDEFINED_VARIABLE}"> 
       <tr> 
        <td> 
         <span style="{UNDEFINED_VARIABLE}">Timestamp:</span> 
        </td> 
        <td> 
         <span style="{UNDEFINED_VARIABLE}"> 
          <xsl:value-of select="my:time"/> 
         </span> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <span style="{UNDEFINED_VARIABLE}">Report type:</span> 
        </td> 
        <td> 
         <span style="{UNDEFINED_VARIABLE}"> 
          <xsl:value-of select="my:reporttype"/> 
         </span> 
        </td> 
       </tr> 
      </table> 
     </td> 
    </tr> 
</xsl:template> 

</xsl:stylesheet> 
1

我可以看到您正确使用前缀my:来指定XPath中匹配模式的名称空间中的元素。这确实是你需要做的。

但是,您还需要将此前缀链接到您的名称空间,否则此前缀并不代表任何内容,实际上无效。

所有你需要做在你的XSLT被取代,

xmlns="https://www.website.com/getdat/service/" 

xmlns:my="https://www.website.com/getdat/service/" 

编辑添加有关其他问题:

,需要相应地匹配和选择您想要执行的元素。

您的第一个模式不应该以xmlresponse为目标。它应该针对可以通过match =“/”简单到达的根。如果xmlresponse是根元素,使用match =“/ my:xmlresponse”只会匹配,但不是。

调用apply-templates时,不应指定select。如果您愿意,应用模板将最终达到报告信息。但是,如果您尝试使用select =“my:reportinformation”进行选择,那么只有在报告信息是儿童时才会起作用。它不是,它是一个后裔。你需要

select=".//my:reportinformation" 

或者根本不指定一个选择的模板适用于儿童和他们自己将桶传递到报告信息。

+0

我做到了,但仍然没有工作。 我更新了代码 – maw2be

+0

@ maw2be错误的文件。我在XSLT中说过。 此外,你能真正修改你的提供商给你的XML吗? – kumesana

+0

抱歉,您的权利我编辑错误的文件。应该编辑xslt – maw2be