0

I'm having trouble converting XHMTL files to XML while using a xslt 2.0 stylesheet. I know the question was answered for XSLT 1.0 but it really doesn't work for XSLT 2.0!

Example XHTML file:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="de" xml:lang="de" xmlns="http://www.w3.org/1999/xhtml">
  <head>
     <meta name="something" content="content"/>
     (...)
  </head>
  <body onload="...">
     (...)
  </body>
</html>

Example Stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<xsl:template match="xhtml:html/xhtml:head">    
     <section role="example">
        <xsl:attribute name="id" select="concat('something', meta[@name='something']/@content)"/>    
         (...)
      </section>
</xsl:stylesheet>

I'm unable to call the nodes. I'm using oxygen XML editor (older version unfortunately but for all XML transformation it worked just fine) and I tried different processors (Saxon-EE 9.4.0.6,Saxon HE 9.4.0.6, Saxon-PE9.4.0.6.). I looked at this issue XHTML to XML XSLT conversion which makes reference to XSLT 2.0 and also tried to replace the namespace in the match with an asterisk, but the editor doesn't accept it.

After the precious comment from @MartinHonnen I changed the XSLT to:

 <?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xhtml="http://www.w3.org/1999/xhtml" exclude-result-prefixes="xhtml">
<xsl:template match="xhtml:html/xhtml:head">    
     <section role="example">
        <xsl:attribute name="id" select="concat('something', xhtml:meta[@name='something']/@content)"/>  
         (...)
      </section>
</xsl:stylesheet>

This small change did the trick. All kinds of meta information can now be extracted from XHTML. With the former stylesheet in the resulting XML there was just "something_" filled in as value for id-attribute, e.g. This is a short result example with the corrected stylesheet:

    <?xml version="1.0" encoding="UTF-8"?>
  <section role="example" id="something_content">
      (...)
  </section>

Thanks again!!!!

3
  • What does "I'm unable to call the nodes" mean exactly? Post the result you want and the result oXygen gives you, or any exact error message. In general if you work with namespaces then you need to use them consistently so the meta path should be xhtml:meta as well. Commented Nov 28, 2017 at 12:06
  • Thanks for your comment @MartinHonnen. I changed meta to xhtml:meta and it worked. I understood from the other issue that I have to add it only in match attribute value. This actually answered already my question. Can I somehow vote for you? I'm going to add the correction in XSTL and correct result example to the issue description. Commented Nov 28, 2017 at 12:30
  • I have added the suggestion of the previous comment as an answer together with a suggestion to ease the task of matching and selecting elements in a namespace with XSLT 2 or later, that way you can mark the problem as solved. Commented Nov 28, 2017 at 12:38

1 Answer 1

1

If you work with namespaces then you have to use them consistently so the path meta to select the XHTML meta elements needs to be xhtml:meta.

Note that with XSLT 2 and later you can make your like easier using xpath-default-namespace="http://www.w3.org/1999/xhtml" on your xsl:stylesheet as then you can use e.g. match="html/head" and select="meta" without any need to use a prefix.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Thanks for your answer with the further tip!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.