【问题标题】:How to insert CDATA into XML text markup exported from Access 2003?如何将 CDATA 插入从 Access 2003 导出的 XML 文本标记中?
【发布时间】:2012-07-31 13:46:38
【问题描述】:

我从 Access 2003 获得了 XML 导出,我尝试使用 XSLT 在文本字段(拉丁语...)上插入 CDATA 标记,但我在 XSLT 中非常糟糕...

这里是 XML 源代码:

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
      <export_x005F_xml_message>
       <libelle>h euismod tincidu </libelle>
       <price>300</price>
       <libelle2>h euirci tation ullamc</libelle2>
    </export_x005F_xml_message>
    <export_x005F_xml_message>
      <libelle>h euismod tincidunt ut lao</libelle>
      <price>200</price>
      <libelle2>h euirci tation ullamcorper</libelle2>
   </export_x005F_xml_message>
  </dataroot>

这是我的 XSLT 的开始...:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml"/>
     <xsl:template match='*[name() = "MESSAGES"]'>
           <xsl:text disable-output-escaping="yes">
             &lt;![CDATA[
           </xsl:text>
           <xsl:copy-of select="./node()"/>
           <xsl:text disable-output-escaping="yes">
             ]]&gt;
            </xsl:text>
     </xsl:template>
    </xsl:stylesheet>

我想得到这样的东西:

    <?xml version="1.0" encoding="UTF-8"?>
    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
      <export_x005F_xml_message>
       <libelle><![CDATA[h euismod tincidu ]]></libelle>
       <price>300</price>
       <libelle2><![CDATA[h euirci tation ullamc ]]></libelle>
    </export_x005F_xml_message>
    <export_x005F_xml_message>
      <libelle><![CDATA[h euismod tincidunt ut lao ]]></libelle2>
      <price>200</price>
      <libelle2><![CDATA[h euirci tation ullamcorper ]]></libelle2>
   </export_x005F_xml_message>
  </dataroot>

您能帮我创建正确的 XSLT 吗? 此 XML 来自 Access 2003,它不提供文本字段的 CDATA 选项...我确信通用模型可以帮助像我这样的其他开发人员:-)

【问题讨论】:

标签: ms-access xslt xslt-1.0 cdata


【解决方案1】:

正如这里已经回答的:Transform XML with XSLT and preserve CDATA (in Ruby),更好的答案是使用 xsl:output。比如……

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" cdata-section-elements="libelle libelle2" />

<xsl:template match="@*|node()">
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

</xsl:stylesheet>

【讨论】:

    【解决方案2】:

    这种身份转换

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"
      cdata-section-elements="libelle libelle2"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时(已纠正,因为它的格式严重错误):

    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd"
     generated="2012-07-31T13:25:46">
        <export_x005F_xml_message>
            <libelle>h euismod tincidu </libelle>
            <price>300</price>
            <libelle2>h euirci tation ullamc</libelle2>
        </export_x005F_xml_message>
        <export_x005F_xml_message>
            <libelle>h euismod tincidunt ut lao</libelle>
            <price>200</price>
            <libelle2>h euirci tation ullamcorper</libelle2>
        </export_x005F_xml_message>
    </dataroot>
    

    产生想要的正确结果

    <dataroot xmlns:od="urn:schemas-microsoft-com:officedata" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd" generated="2012-07-31T13:25:46">
       <export_x005F_xml_message>
          <libelle><![CDATA[h euismod tincidu ]]></libelle>
          <price>300</price>
          <libelle2><![CDATA[h euirci tation ullamc]]></libelle2>
       </export_x005F_xml_message>
       <export_x005F_xml_message>
          <libelle><![CDATA[h euismod tincidunt ut lao]]></libelle>
          <price>200</price>
          <libelle2><![CDATA[h euirci tation ullamcorper]]></libelle2>
       </export_x005F_xml_message>
    </dataroot>
    

    解释

    正确使用xsl:outputcdata-section-elements属性。

    【讨论】:

      【解决方案3】:

      嗯,我看到的唯一问题是 xsl 模板的调用。

      应该是这样的:

      <xsl:template name="MyTemplateName">
          <someTag>
              <xsl:text disable-output-escaping="yes">
                          &lt;![CDATA[
                  </xsl:text>
      
              <someOtherTag/>
      
              <xsl:text disable-output-escaping="yes">
                       ]]&gt;
                  </xsl:text>
          </someTag>
      </xsl:template>
      

      所以你的模板应该是这样的:

      <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet version="2.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="MESSAGES%20old.xsd">
      
      <xsl:template match="/">
          <dataroot xmlns:od="urn:schemas-microsoft-com:officedata"  generated="2012-07-31T13:25:46">
          <xsl:apply-templates select="export_x005F_xml_message"/>
          </dataroot>
      </xsl:template>
      
      <xsl:template match="export_x005F_xml_message">
          <export_x005F_xml_message>
              <libelle>
              <xsl:text disable-output-escaping="yes">
               &lt;![CDATA[
             </xsl:text>
                  <xsl:value-of select="libelle"/>
              </libelle>
              <xsl:text disable-output-escaping="yes">
              ]]&gt;
             </xsl:text>
             ...
          </export_x005F_xml_message>
      </xsl:template>
      
      </xsl:stylesheet>
      

      【讨论】:

      • 这只是一件小事,但请注意 OP 想要 XSLT 1.0 。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      相关资源
      最近更新 更多