簡體   English   中英

復制 XML 文件,包括文檔類型、實體和符號 XSLT 3

[英]Copying an XML file including doctype, entities and notations XSLT 3

我有一系列 XML 文檔從一個文件夾復制到另一個文件夾,並使用 msxsl.exe 1.1.0.1 和 XSLT 1.0 樣式表進行轉換,然后復制回原始文件夾。 我不知道為什么沒有復制文檔類型、實體和符號,但目前它們正在與樣式表中的 javascript 一起插入。 我必須用 XSLT 3.0 替換 javascript,這樣它才能與撒克遜 HE11 一起使用。

doctype是XML中最高的元素,這也是我想要的output:

  <!DOCTYPE dmodule [
  <!ENTITY ICN-XXX12-001-01 SYSTEM "ICN-XXX12-001-01.SWF" NDATA swf >
  <!ENTITY ICN-XXX49-001-01 SYSTEM "ICN-XXX49-001-01 SYSTEM.CGM" NDATA cgm >
  <!ENTITY ICN-AAA235-000000-0-A-001-01 SYSTEM "ICN-AAA235-000000-0-A-001-01.wrlzip" NDATA WRLZIP>
  <!NOTATION cgm PUBLIC "-//USA-DOD//NOTATION Computer Graphics Metafile//EN" >
  <!NOTATION swf PUBLIC "-//S1000D//NOTATION X-SHOCKWAVE-FLASH 3D Models Encoding//EN" >
  <!NOTATION WRLZIP SYSTEM "WRLZIP">
]>
<dmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:dc="http://www.purl.org/dc/elements/1.1/"
         xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xsi:noNamespaceSchemaLocation="../schema/proced.xsd">
         <content>
            <figure>
              <title/>
                <graphic infoEntityIdent="ICN-XXX49-001-01"/>
            </figure>
            <proceduralStep>
                <para>Check the brake system function.</para>
                <multimedia>
                   <title>Brake function</title>
                   <multimediaObject autoPlay="1" fullScreen="0" infoEntityIdent="ICN-XXX12-001-01" multimediaType="other"/>
                </multimedia>
             </proceduralStep>
             <multimedia>
                 <multimediaObject infoEntityIdent="ICN-AAA235-000000-0-A-001-01"
                        multimediaType="3D"
                        xlink:href="ICN-AAA235-000000-0-A-001-01.wrlzip"
                        xlink:type="simple"/>
                </multimedia>
         </content>
</dmodule>

這些實體在@infoEntityIdent上從各種元素中引用,但並不總是指示文件類型:

    <graphic infoEntityIdent="ICN-XXX49-001-01"/>
    <multimediaObject autoPlay="1" fullScreen="0" infoEntityIdent="ICN-XXX12-001-01"
                                           multimediaType="other"/>
    <multimediaObject infoEntityIdent="ICN-AAA235-000000-0-A-001-01"
                        multimediaType="3D" xlink:href="ICN-AAA235-000000-0-A-001-01.wrlzip"
 xlink:type="simple"/>

我可以正確插入文檔類型,但我不知道如何訪問實體和符號:

<xsl:template match="/">
    <xsl:text>&#xA;</xsl:text>
    <xsl:text disable-output-escaping="yes">&lt;!DOCTYPE </xsl:text>
    <xsl:value-of select="local-name(child::*)"/>
    <xsl:text> [</xsl:text> 
    <!-- entities and notations here -->  
    <xsl:text disable-output-escaping="yes">]&gt;</xsl:text>
    <xsl:text>&#xA;</xsl:text>
    <xsl:copy> 
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
</xsl:template>

當前 output:

<!DOCTYPE dmodule []>
<dmodule xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:dc="http://www.purl.org/dc/elements/1.1/"
     xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     xsi:noNamespaceSchemaLocation="../schema/proced.xsd">
     <content>
        <figure>
          <title/>
            <graphic infoEntityIdent="ICN-XXX49-001-01"/>
        </figure>
        <proceduralStep>
            <para>Check the brake system function.</para>
            <multimedia>
               <title>Brake function</title>
               <multimediaObject autoPlay="1" fullScreen="0" infoEntityIdent="ICN-XXX12-001-01" multimediaType="other"/>
            </multimedia>
         </proceduralStep>
         <multimedia>
           <multimediaObject infoEntityIdent="ICN-AAA235-000000-0-A-001-01"
                    multimediaType="3D"
                    xlink:href="ICN-AAA235-000000-0-A-001-01.wrlzip"
                    xlink:type="simple"/>
            </multimedia>
     </content>

這是樣式表中繼承的 javascript,它確實給出了預期的結果:

<msxsl:script language="JavaScript" implements-prefix="js">

    <![CDATA[
function doctype(root) {
    var fso = new ActiveXObject('Scripting.FileSystemObject');
    var basepath = unescape(
        root
            .item(0)
            .url
            .replace(/^file:\/{3,}/, '')
            .replace(/^file:/, '')
            .replace(/[^\/]+$/, '')
            .replace(/\//g, '\\')
    );
    var entities = [];
    var notations = [];
    var needSVGNotations = false;
    if (root.item(0).doctype) {
        entities = root.item(0).doctype.entities;
        notations = root.item(0).doctype.notations;
    }
    var syntax = '\n<!DOCTYPE ' + root.item(0).documentElement.nodeName + ' [\n';
    for (var i = 0; i < entities.length; i++) {
        var entity = entities.item(i);

            var s = entity.xml;
            syntax += s + '\n';
    }
    for (var i = 0; i < notations.length; i++) {
        var s = notations.item(i).xml;
        syntax += s + '\n';
    }
    syntax += ']>\n';
    return syntax;
}
    ]]>
  </msxsl:script>

這是使用 javascript 的模板:

<xsl:template match="/">
    <xsl:value-of select="js:doctype(.)" disable-output-escaping="yes"/>
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

The information in the DTD/DOCTYPE isn't part of the XSLT data model, so it's not passed from the XML parser to the XSLT processor -- which means you can't do this in pure XSLT.

有一個來自 Andrew Welch 的名為 LEXEV 的實用程序,它預處理 XML 文檔以根據元素和屬性創建 DTD 的表示,然后您可以使用 XSLT 以正常方式轉換(或保持不變)。 最后被后處理回 DTD 語法。 我已經很多年沒有使用它了,但我希望它仍然有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM