简体   繁体   English

如何在Oxygen XML编辑器中使用XSLT 2.0将XHTML转换为XML? XSLT 1.0的解决方案不起作用

[英]How to transform XHTML to XML with XSLT 2.0 in Oxygen XML editor? Solution for XSLT 1.0 doesn't work

I'm having trouble converting XHMTL files to XML while using a xslt 2.0 stylesheet . 使用xslt 2.0样式表时,无法将XHMTL文件转换为XML。 I know the question was answered for XSLT 1.0 but it really doesn't work for XSLT 2.0! 我知道该问题已针对XSLT 1.0回答,但对于XSLT 2.0确实不起作用!

Example XHTML file: 示例XHTML文件:

<?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.). 我使用的是氧气XML编辑器(不幸的是,它使用的是旧版本,但对于所有XML转换而言,它都可以正常工作),并且尝试了不同的处理器(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. 我研究了这个问题,即XHTML到XML XSLT的转换 ,该 转换 引用了XSLT 2.0,并且还尝试用星号替换匹配项中的名称空间,但是编辑器不接受。

After the precious comment from @MartinHonnen I changed the XSLT to: 在@MartinHonnen提出宝贵意见之后,我将XSLT更改为:

 <?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. 现在可以从XHTML中提取各种元信息。 With the former stylesheet in the resulting XML there was just "something_" filled in as value for id-attribute, eg This is a short result example with the corrected stylesheet: 对于生成的XML中的前一个样式表,仅将“ something_”作为id属性的值填写,例如,这是一个经过纠正的样式表的简短结果示例:

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

Thanks again!!!! 再次感谢!!!!

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 . 如果使用名称空间,则必须一致使用它们,因此用于选择XHTML meta元素的路径meta必须为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 eg match="html/head" and select="meta" without any need to use a prefix. 请注意,在XSLT 2及更高版本中,您可以在xsl:stylesheet上使用xpath-default-namespace="http://www.w3.org/1999/xhtml"xpath-default-namespace="http://www.w3.org/1999/xhtml"您的操作,然后可以使用例如match="html/head"select="meta"而无需使用前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM