Transforming XML file to HTML file using XSL

2019-09-03 20:22发布

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

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-09-03 20:40

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.

查看更多
登录 后发表回答