使用XSL将XML文件转换为HTML文件 [英] Transforming XML file to HTML file using XSL

查看:222
本文介绍了使用XSL将XML文件转换为HTML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求如下所示:
1.在网页
上载一个xml文件2.使用xsl文件解析上传的xml
3.在html表格中显示结果同样的网页。



但我无法用我的xml文件进行解析。我尝试了简单的XML文件,它的工作。
我的xml文件如下所示:

 <?xml version =1.0encoding = UTF-8standalone =no?> 
< DCinemaSecurityMessage xmlns =http://www.smpte-ra.org/schemas/430-3/2006/ETMxmlns:ds =http://www.w3.org/2000/09 / xmldsig#xmlns:enc =http://www.w3.org/2001/04/xmlenc#>
< AuthenticatedPublic Id =ID_AuthenticatedPublic>
< MessageId> urn:uuid:3963b8fc-e989-4be2-a5d4-139d8561643c< / MessageId>
< MessageType> http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type< / MessageType>
< AnnotationText> NBS< / AnnotationText>
< IssueDate> 2016-02-25T17:43:29-00:00< / IssueDate>
< Signer>
< / Signer>
< RequiredExtensions>
< KDMRequiredExtensions xmlns =http://www.smpte-ra.org/schemas/430-1/2006/KDM>
< Recipient>< X509IssuerSerial>< ds:X509IssuerName xmlns:ds =http://www.w3.org/2000/09/xmldsig#> dnQualifier = vUlg / 0Tl / y5rXEFbSb7xF76F / 2U =,CN = .DC.DOLPHIN.DC2.SMPTE,OU = DC.DOREMILABS.COM,O = DC2.SMPTE.DOREMILABS.COM< / ds:X509IssuerName>< ds:X509SerialNumber xmlns:ds =http:// www.w3.org/2000/09/xmldsig#\">25149</ds:X509SerialNumber></X509IssuerSerial> ;<X509SubjectName> ;dnQualifier=ejcAIJ/TlzuipjwSFRTGhPT/8go=,CN=LE SPB MD SM.DCP2000-200007 -03.DC.DC2.SMPTE,OU = DC.DOREMILABS.COM,O = DC2.SMPTE.DOREMILABS.COM< / X509SubjectName>
< /收件人>
< CompositionPlaylistId> urn:uuid:16e5f6c6-89b5-4c0e-a01d-79dcdf792daa< / CompositionPlaylistId>
< ContentTitleText> American_Pastoral_FTR-3-Temp_S_EN-XX_OV_20_2K_LS_20160218_NBS_IOP_OV< / ContentTitleText>
< ContentKeysNotValidBefore> 2016-02-25T20:00:00-00:00< / ContentKeysNotValidBefore>
< ContentKeysNotValidAfter> 2016-02-25T21:00:00-00:00< / ContentKeysNotValidAfter>
< AuthorizedDeviceInfo>
< DeviceListIdentifier> urn:uuid:6fb7b1ef-1086-49b7-9f98-02333006fdfa< / DeviceListIdentifier>
< / DeviceList>< CertificateThumbprint> 2jmj7l5rSw0yVb / vlWAYkK / YBwk =< / CertificateThumbprint>< / DeviceList>
< / AuthorizedDeviceInfo>
< KeyIdList>< TypedKeyId>< KeyType> MDIK< / KeyType>< KeyId> urn:uuid:ac20730a-7172-4f5e-9f90-f9b8aac31a90< / KeyId>< / TypedKeyId>< TypedKeyId> ;<关键字类型> MDAK< /关键字类型>< KEYID>瓮:UUID:d93306ef-b0f8-43cc-ADFD-ac534cdf8412< / KEYID>< / TypedKeyId>< / KeyIdList>
< / KDMRequiredExtensions>
< / RequiredExtensions>< NonCriticalExtensions />
< / AuthenticatedPublic>
< / DCinemaSecurityMessage>

我需要显示 IssueDate,ContentTitleText,ContentKeysNotValidBefore,ContentKeysNotValidAfter,CompositionPlaylistId 来自这个XML的内容。我使用[ http://www.w3schools.com/xsl/xsl_client.asp ] [1] 链接进行解析。但我无法在同一页面上传xml文件。

任何人都可以帮我弄清楚是什么问题?
XSL文件,

 <?xml version =1.0encoding =UTF-8?> 
< xsl:template match =/>
< html>
< body>
< h2> Parsed Data< / h2>
< table border =1>
< tr bgcolor =#9acd32>
< th>标题< / th>
< th>创建日期< / th>
< th>从< / th>开始有效
< th>有效至< / th>
< th> UUID< / th>
< / tr>
< tr>
< td>< xsl:value-of select =/ DCinemaSecurityMessage / AuthenticatedPublic / RequiredExtensions / KDMRequiredExtensions / ContentTitleText/>< / td>
< td>< xsl:value-of select =/ DCinemaSecurityMessage / AuthenticatedPublic / IssueDate/>< / td>
< td>< xsl:value-of select =/ DCinemaSecurityMessage / AuthenticatedPublic / RequiredExtensions / KDMRequiredExtensions / ContentKeysNotValidBefore/>< / td>
< td>< xsl:value-of select =/ DCinemaSecurityMessage / AuthenticatedPublic / RequiredExtensions / KDMRequiredExtensions / ContentKeysNotValidAfter/>< / td>
< td>< xsl:value-of select =/ DCinemaSecurityMessage / AuthenticatedPublic / RequiredExtensions / KDMRequiredExtensions / CompositionPlaylistId/>< / td>
< / tr>
< / table>
< / body>
< / html>
< / xsl:template>
< / xsl:stylesheet>

没有错误信息,但表中没有来自xml的值

解决方案

这很可能是一个命名空间问题(

a>)。

对于第一次测试,尝试删除默认命名空间 xmlns =http://www.smpte-ra.org/schemas/430-3/2006/ ETM组成你的xml。

如果这样做,但不应该是最终的解决方案,您需要为xslt xmlns:x =http: //www.smpte-ra.org/schemas/430-3/2006/ETM并在xpaht中使用此前缀。



例如:

 < td> 
< xsl:value-of
select =/ x:DCinemaSecurityMessage / x:AuthenticatedPublic / x:IssueDate/>
< / td>

更新:

请考虑 KDMRequiredExtensions 也是默认的命名空间( xmlns =http://www.smpte-ra.org/schemas/430-1/2006/KDM
因此,使用 kdm:KDMRequiredExtensions

 < xsl:value-of 
select =/ x:DCinemaSecurityMessage
/ x:AuthenticatedPublic
/ x:RequiredExtensions
/ kdm:KDMRequiredExtensions
/ kdm:ContentTitleText/>

更新doe给评论中的其他问题:

问:但我们如何识别这个名称空间可以用于此元素吗?

每个xml节点都可以拥有自己的(新)默认名称空间 xmlns =/ url /。这个命名空间比用于这个节点和任何子节点(但可以再次改变)
在你的例子中,ContentTitleText没有默认的命名空间声明,因此paren t仍然有效。

在您的xslt中,您需要为每个这样的名称空间提供一个带有前缀的名称空间声明,并且您必须使用此前缀。


My requirement is like the following, 1. Upload one xml file in a webpage 2. Parse the uploaded xml using xsl file 3. Show the result in html table in the same webpage.

But I couldn't do the parsing with my xml file. I tried with simple xml file and it worked. And my xml file would look like the following,

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<DCinemaSecurityMessage xmlns="http://www.smpte-ra.org/schemas/430-3/2006/ETM" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:enc="http://www.w3.org/2001/04/xmlenc#">
   <AuthenticatedPublic Id="ID_AuthenticatedPublic"> 
      <MessageId>urn:uuid:3963b8fc-e989-4be2-a5d4-139d8561643c</MessageId>
      <MessageType>http://www.smpte-ra.org/430-1/2006/KDM#kdm-key-type</MessageType>
      <AnnotationText>NBS</AnnotationText>
      <IssueDate>2016-02-25T17:43:29-00:00</IssueDate>
      <Signer>
         <ds:X509IssuerName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">dnQualifier=vV59D4u5w9rnFOyDPugA9tpTE7Y=,OU=.Signature.DC.CA.DVS,O=.DC.CA.DVS,CN=.ClipsterDCI.Signature </ds:X509IssuerName>
         <ds:X509SerialNumber xmlns:ds="http://www.w3.org/2000/09/xmldsig#">139130018</ds:X509SerialNumber>
      </Signer>
      <RequiredExtensions>
         <KDMRequiredExtensions xmlns="http://www.smpte-ra.org/schemas/430-1/2006/KDM">
            <Recipient><X509IssuerSerial><ds:X509IssuerName xmlns:ds="http://www.w3.org/2000/09/xmldsig#">dnQualifier=vUlg/0Tl/y5rXEFbSb7xF76F/2U=,CN=.DC.DOLPHIN.DC2.SMPTE,OU=DC.DOREMILABS.COM,O=DC2.SMPTE.DOREMILABS.COM</ds:X509IssuerName><ds:X509SerialNumber xmlns:ds="http://www.w3.org/2000/09/xmldsig#">25149</ds:X509SerialNumber></X509IssuerSerial><X509SubjectName>dnQualifier=ejcAIJ/TlzuipjwSFRTGhPT/8go=,CN=LE SPB MD SM.DCP2000-200007-03.DC.DC2.SMPTE,OU=DC.DOREMILABS.COM,O=DC2.SMPTE.DOREMILABS.COM</X509SubjectName>
           </Recipient>
           <CompositionPlaylistId>urn:uuid:16e5f6c6-89b5-4c0e-a01d-79dcdf792daa</CompositionPlaylistId>
           <ContentTitleText>American_Pastoral_FTR-3-Temp_S_EN-XX_OV_20_2K_LS_20160218_NBS_IOP_OV</ContentTitleText>
           <ContentKeysNotValidBefore>2016-02-25T20:00:00-00:00</ContentKeysNotValidBefore>
           <ContentKeysNotValidAfter>2016-02-25T21:00:00-00:00</ContentKeysNotValidAfter>
           <AuthorizedDeviceInfo>
               <DeviceListIdentifier>urn:uuid:6fb7b1ef-1086-49b7-9f98-02333006fdfa</DeviceListIdentifier>
               <DeviceList><CertificateThumbprint>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</CertificateThumbprint></DeviceList>
          </AuthorizedDeviceInfo>
          <KeyIdList><TypedKeyId><KeyType>MDIK</KeyType><KeyId>urn:uuid:ac20730a-7172-4f5e-9f90-f9b8aac31a90</KeyId></TypedKeyId><TypedKeyId><KeyType>MDAK</KeyType><KeyId>urn:uuid:d93306ef-b0f8-43cc-adfd-ac534cdf8412</KeyId></TypedKeyId></KeyIdList>
        </KDMRequiredExtensions>
   </RequiredExtensions><NonCriticalExtensions/>
   </AuthenticatedPublic>
   </DCinemaSecurityMessage>

And I need to display IssueDate, ContentTitleText, ContentKeysNotValidBefore, ContentKeysNotValidAfter, CompositionPlaylistId contents from this xml. And I used [http://www.w3schools.com/xsl/xsl_client.asp][1] link for parsing. But I couldn't do it with uploading xml file in the same page.

Can anyone help me to figure out what is the issue? XSL file,

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:enc="http://www.w3.org/2001/04/xmlenc#" xmlns:kdm="http://www.smpte-ra.org/schemas/430-1/2006/KDM" >
    <xsl:template match="/">
    <html>
      <body>
         <h2>Parsed Data</h2>
         <table border="1">
           <tr bgcolor="#9acd32">
             <th>Title</th>
             <th>Created Date</th>
             <th>Valid from</th>
             <th>Valid to</th>
             <th>UUID</th>
          </tr>
          <tr>
             <td><xsl:value-of select="/DCinemaSecurityMessage/AuthenticatedPublic/RequiredExtensions/KDMRequiredExtensions/ContentTitleText" /></td>
             <td><xsl:value-of select="/DCinemaSecurityMessage/AuthenticatedPublic/IssueDate" /></td>
             <td><xsl:value-of select="/DCinemaSecurityMessage/AuthenticatedPublic/RequiredExtensions/KDMRequiredExtensions/ContentKeysNotValidBefore" /></td>
             <td><xsl:value-of select="/DCinemaSecurityMessage/AuthenticatedPublic/RequiredExtensions/KDMRequiredExtensions/ContentKeysNotValidAfter" /></td>
             <td><xsl:value-of select="/DCinemaSecurityMessage/AuthenticatedPublic/RequiredExtensions/KDMRequiredExtensions/CompositionPlaylistId" /></td>
         </tr>
        </table>
     </body>
    </html>
     </xsl:template>
     </xsl:stylesheet>

And there is no error message but the table have no values from the xml

解决方案

This is most likely a namespace issue (like).
For a first test try to remove the default namespace xmlns="http://www.smpte-ra.org/schemas/430-3/2006/ETM" form your xml.

If this works but should not be the final solution you need to add the namespace with an prefix to your xslt xmlns:x="http://www.smpte-ra.org/schemas/430-3/2006/ETM" and use this prefix in your xpaht.

For example:

<td>
  <xsl:value-of 
      select="/x:DCinemaSecurityMessage/x:AuthenticatedPublic/x:IssueDate" />
</td>

Update:
Please consider that KDMRequiredExtensions has also a default namespace (xmlns="http://www.smpte-ra.org/schemas/430-1/2006/KDM) Therefor use kdm:KDMRequiredExtensions

 <xsl:value-of 
            select="/x:DCinemaSecurityMessage
           /x:AuthenticatedPublic
           /x:RequiredExtensions
           /kdm:KDMRequiredExtensions
           /kdm:ContentTitleText" />

Update doe to additional question in comment:
Q: but how can we identify this namespace can be used for this element?
Each xml node can have it own (new) default namespace xmlns="/url/". This namespace is than uses for this node and any children node (but could be changed again) In your example ContentTitleText has no default namespace declaration therefore the one from the parent is still valid.
In your xslt you need to have a for each of this namespaces a namespace declaration with prefix and than you have to use this prefix.

这篇关于使用XSL将XML文件转换为HTML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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