我正在尝试转换具有 < 的 XML 消息。 >
在其中,我能够做到。但转换后,“数据类型”并未被填充。
我的请求 XML:
<?xml version="1.0"?>
<params>
<param dataType="java.lang.String">respID</param>
<param dataType="java.lang.Date">2015-11-04</param>
<param dataType="java.lang.String">
<XX_IL_OLM_COMP_ELEMENT_OBJ><P_OLM_COMP_ELEMENT>
<XX_IL_OLM_COMP_ELEMENT_OBJ>
<P_ACTION>CREATE</P_ACTION>
<p_activity_version_id>12</p_activity_version_id>
<p_learning_path_id>12</p_learning_path_id>
<p_certification_id>1006</p_certification_id>
<p_offering_id>3</p_offering_id>
<p_competence_id>43</p_competence_id>
<p_proficiency_level_id>34</p_proficiency_level_id>
<p_competence_element_id>64</p_competence_element_id>
<p_object_version_number>09</p_object_version_number>
<P_ERROR_CODE>43</P_ERROR_CODE>
<P_ERROR_MESSAGE/>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</P_OLM_COMP_ELEMENT>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</param>
<param dataType="java.lang.String">respID</param>
</params>
我的 XSLT:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="param">
<xsl:copy>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
转换后的输出:
<?xml version="1.0" encoding="UTF-8"?>
<params>
<param>respID</param>
<param>2015-11-04</param>
<param>
<XX_IL_OLM_COMP_ELEMENT_OBJ><P_OLM_COMP_ELEMENT>
<XX_IL_OLM_COMP_ELEMENT_OBJ>
<P_ACTION>CREATE</P_ACTION>
<p_activity_version_id>12</p_activity_version_id>
<p_learning_path_id>12</p_learning_path_id>
<p_certification_id>1006</p_certification_id>
<p_offering_id>3</p_offering_id>
<p_competence_id>43</p_competence_id>
<p_proficiency_level_id>34</p_proficiency_level_id>
<p_competence_element_id>64</p_competence_element_id>
<p_object_version_number>09</p_object_version_number>
<P_ERROR_CODE>43</P_ERROR_CODE>
<P_ERROR_MESSAGE/>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</P_OLM_COMP_ELEMENT>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</param>
<param>respID</param>
</params>
所需的输出应将数据类型作为属性。
<?xml version="1.0" encoding="UTF-8"?>
<params>
<param dataType="java.lang.String">respID</param>
<param dataType="java.lang.Date">2015-11-04</param>
<param dataType="java.lang.String">
<XX_IL_OLM_COMP_ELEMENT_OBJ><P_OLM_COMP_ELEMENT>
<XX_IL_OLM_COMP_ELEMENT_OBJ>
<P_ACTION>CREATE</P_ACTION>
<p_activity_version_id>12</p_activity_version_id>
<p_learning_path_id>12</p_learning_path_id>
<p_certification_id>1006</p_certification_id>
<p_offering_id>3</p_offering_id>
<p_competence_id>43</p_competence_id>
<p_proficiency_level_id>34</p_proficiency_level_id>
<p_competence_element_id>64</p_competence_element_id>
<p_object_version_number>09</p_object_version_number>
<P_ERROR_CODE>43</P_ERROR_CODE>
<P_ERROR_MESSAGE/>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</P_OLM_COMP_ELEMENT>
</XX_IL_OLM_COMP_ELEMENT_OBJ>
</param>
<param dataType="java.lang.String">respID</param>
</params>
谁能帮我纠正我的错误。提前致谢。干杯!!
最佳答案
您没有处理 param
中的属性模板。添加 <xsl:copy-of select="@*"/>
或<xsl:apply-templates select="@*"/>
到模板...
<xsl:template match="param">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:copy>
</xsl:template>
我会使用xsl:apply-templates
。这样,如果您将来需要更改这些属性,只需添加一个模板即可。如果您使用xsl:copy-of
,您必须添加模板并更改 xsl:copy-of
至xsl:apply-templates
无论如何。
关于xml - 使用 XSLT 转换其中包含 < > 的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33810019/