我从Access 2003获得了XML导出,并尝试使用XSLT在文本字段(latin ...)上插入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">
<![CDATA[
</xsl:text>
<xsl:copy-of select="./node()"/>
<xsl:text disable-output-escaping="yes">
]]>
</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选项......我确信通用模型可以帮助像我这样的其他开发人员: - )
答案 0 :(得分:3)
如前所述: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>
答案 1 :(得分: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:output
的cdata-section-elements
属性。
答案 2 :(得分:1)
嗯,我看到的唯一问题是调用xsl-template。
它应该是这样的:
<xsl:template name="MyTemplateName">
<someTag>
<xsl:text disable-output-escaping="yes">
<![CDATA[
</xsl:text>
<someOtherTag/>
<xsl:text disable-output-escaping="yes">
]]>
</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">
<![CDATA[
</xsl:text>
<xsl:value-of select="libelle"/>
</libelle>
<xsl:text disable-output-escaping="yes">
]]>
</xsl:text>
...
</export_x005F_xml_message>
</xsl:template>
</xsl:stylesheet>