提取外部HTML内容并使用XSL创建XHTML

提取外部HTML内容并使用XSL创建XHTML,html,xml,xslt,xslt-2.0,Html,Xml,Xslt,Xslt 2.0,我需要通过从外部html文件获取信息,使用xslt转换创建html: 我的输入XML文件: <topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:r="http://www.Corecms.com/Core/ns/metadata" class="- topic/topic " ditaarch:DITAArchVersion="1.2" domains="(topic hi-d) (topi

我需要通过从外部html文件获取信息,使用xslt转换创建html:

我的输入XML文件:

<topic xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/" xmlns:r="http://www.Corecms.com/Core/ns/metadata" class="- topic/topic " ditaarch:DITAArchVersion="1.2" domains="(topic hi-d) (topic indexing-d) (topic d4p_formatting-d) a(props d4p_renditionTarget) (topic d4p_math-d) (topic d4p_variables-d) (topic d4p_verse-d) (topic learningInteractionBase2-d learning2-d+learning-d) (topic learningBase+learningInteractionBase-d) (topic learningInteractionBase-d) (topic learningInteractionBase2-d) (topic xml-d) a(base CoreIdAtt) (topic sdClassification-d) a(base contentstore) " id="T1" outputclass="interactive" r:CoreId="4567">
       <title class="- topic/title "/>
       <body class="- topic/body ">
              <bodydiv class="- topic/bodydiv ">
                     <xref class="- topic/xref " format="html" href="1234.html" scope="external"/>
              </bodydiv>
       </body>
</topic>

我使用元素调用了html文件。相应的1234.html文件代码为:

  <html>
    <head></head>
    <body>
       <h1>New HTML test with XSL transformation</h1>
        <p>this is a new test about rsuite-edit opening HTML files</p>
    </body>
  </html>

使用XSL转换的新HTML测试
这是一个关于rsuite编辑打开HTML文件的新测试

XSL我已尝试如下所示:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
        xmlns:r="http://www.Corecms.com/Core/ns/metadata" xmlns:exsl="http://exslt.org/common"
        xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ditaarch="http://dita.oasis-open.org/architecture/2005/"
        xmlns:df="http://dita2indesign.org/dita/functions"
        exclude-result-prefixes="xs xd r exsl xhtml ditaarch df" version="2.0">

        <xsl:output method="xml" indent="yes"/>

        <xsl:function name="df:class" as="xs:boolean">
            <xsl:param name="elem" as="element()"/>
            <xsl:param name="classSpec" as="xs:string"/>

            <xsl:variable name="normalizedClassSpec" as="xs:string" select="normalize-space($classSpec)"/>
            <xsl:variable name="result"
                select="matches($elem/@class, concat(' ', $normalizedClassSpec, ' | ', $normalizedClassSpec, '$'))"
                as="xs:boolean"/>

            <xsl:sequence select="$result"/>
        </xsl:function>

        <xsl:template match="/">
            <xsl:variable name="html">
                <xsl:apply-templates/>
            </xsl:variable>
            <xsl:copy-of select="$html"/>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/topic')]">
            <div>
            <xsl:attribute name="contenteditable">true</xsl:attribute>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
                <xsl:choose>
                    <xsl:when test="not(ancestor::*[df:class(., 'topic/topic')])">

                        <xsl:apply-templates select="." mode="generate-comments"/>

                    </xsl:when>
                </xsl:choose>
            </div>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/title')][parent::*[df:class(., 'topic/topic')]]">
            <xsl:variable name="headingLevel" select="count(ancestor::*[df:class(., 'topic/topic')])"
                as="xs:integer"/>
            <xsl:element name="h{$headingLevel}">
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </xsl:element>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/body')]">
            <div>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </div>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/bodydiv')]">
            <div>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </div>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/p')]">
            <p>
                <xsl:apply-templates select="@*"/>
                <xsl:apply-templates/>
            </p>
        </xsl:template>

        <xsl:template match="*[df:class(., 'topic/xref')]">
            <xsl:choose>
                <xsl:when test=". != ''">
                    <a>
                        <xsl:apply-templates select="@*"/>
                        <xsl:apply-templates/>
                    </a>
                </xsl:when>
                <xsl:otherwise>
                    <a>
                        <xsl:apply-templates select="@*"/>
                        NO URL PROVIDED
                    </a>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>

    </xsl:stylesheet>

真的

没有提供URL
我希望输出如下所示:

<div contenteditable="true" data-coreid="4567" html-coreid="1234"> <!-- Where 1234 is the core ID of the HTML file and 4567 is the DITA topic core ID.  -->
      <article>
           <h1>New</h1>
           <p>this is a new test</p>
      </article>
</div>

新的
这是一个新的测试


我需要获取外部html内容(1234.html)并使用XSLT创建新的xhtml文件。我是XSL新手。你的帮助将是可贵的。提前感谢

通常使用
document()
检索外部节点。外部文档必须是可解析的。在我看来,样式表中有很多部分没有多大意义,例如,您使用
来处理属性,但由于您没有属性模板,因此只提取属性的文本节点。

考虑到1234.html是可解析的,如Ferestes等人在回答中所述

<article>
   <h1>New</h1>
   <p>this is a new test</p>
</article>
输出:

<div xmlns="http://www.w3.org/1999/xhtml" contenteditable="true" data-coreid="4567" html-coreid="1234">
    <html xmlns="">
        <head/>
        <body>
            <h1>New HTML test with XSL transformation</h1>
            <p>this is a new test about rsuite-edit opening HTML files</p>
        </body>
    </html>
</div>

使用XSL转换的新HTML测试
这是一个关于rsuite编辑打开HTML文件的新测试


谢谢@ferestes。我已尝试文档()不起作用。或者将外部html更改为有效html,然后尝试解决此问题。请给我一些建议,谢谢@zx485。我已将我的html编辑为可解析的。但他没有在那个模板上工作。请给我一些其他建议。通过编辑,您更改了1234.html的内容,但这并不重要,它仍然可以正常工作。但是现在它与您想要的输出不同步。谢谢@zx485。工作正常。但是如何在div的属性中获取html coreid=“1234”
<xsl:template match="*[df:class(., 'topic/xref')]">
  <div contenteditable="true" data-coreid="{ancestor::topic/@r:CoreId}" html-coreid="{substring-before(@href,'.')}">
    <xsl:copy-of select="document(@href)"/>
  </div>
</xsl:template>
<div xmlns="http://www.w3.org/1999/xhtml" contenteditable="true" data-coreid="4567" html-coreid="1234">
    <html xmlns="">
        <head/>
        <body>
            <h1>New HTML test with XSL transformation</h1>
            <p>this is a new test about rsuite-edit opening HTML files</p>
        </body>
    </html>
</div>