我过去做了一些XSL转换,但我似乎对此不太深入。通过反复试验,我设法得到了我正在寻找的输出,但我无法弄清楚如何在输出中获得根元素。
以下是XML文件输入的示例。我需要清除一些不必要的信息并进入CDATA。
<SI_RESOURCES>
<SCHEDULES>
<SCHEDULE>
<SCHEDULE_TYPE>2</SCHEDULE_TYPE>
<ASSOCIATED_BP_NAME>AFTPurgeArchiveMailboxes</ASSOCIATED_BP_NAME>
<ASSOCIATED_SERVICE_NAME>AFTPurgeArchiveMailboxes</ASSOCIATED_SERVICE_NAME>
<SCHEDULE_PARAMS/>
<SCHEDULE_ONSTARTUP>1</SCHEDULE_ONSTARTUP>
<SCHEDULE_EXECUTION_COUNT>-1</SCHEDULE_EXECUTION_COUNT>
<SCHEDULE_EXECUTION_CURRENT_COUNT>0</SCHEDULE_EXECUTION_CURRENT_COUNT>
<SCHEDULE_EXECUTION_STATUS>WAIT</SCHEDULE_EXECUTION_STATUS>
<SCHEDULE_STATUS>ACTIVE</SCHEDULE_STATUS>
<SCHEDULE_SYSTEMNAME>node1</SCHEDULE_SYSTEMNAME>
<SCHEDULE_USERID>admin</SCHEDULE_USERID>
<SCHEDULE_TIMINGXML><![CDATA[<timingxml><days><day ofWeek="-1"><times><time>0400</time></times></day></days><excludedDates></excludedDates></timingxml>]]></SCHEDULE_TIMINGXML>
</SCHEDULE>
<SCHEDULE>
<SCHEDULE_TYPE>1</SCHEDULE_TYPE>
<ASSOCIATED_BP_NAME>Schedule_AssociateBPsToDocs</ASSOCIATED_BP_NAME>
<ASSOCIATED_SERVICE_NAME>AssociateBPsToDocs</ASSOCIATED_SERVICE_NAME>
<SCHEDULE_PARAMS/>
<SCHEDULE_ONSTARTUP>0</SCHEDULE_ONSTARTUP>
<SCHEDULE_EXECUTION_COUNT>-1</SCHEDULE_EXECUTION_COUNT>
<SCHEDULE_EXECUTION_CURRENT_COUNT>0</SCHEDULE_EXECUTION_CURRENT_COUNT>
<SCHEDULE_EXECUTION_STATUS>WAIT</SCHEDULE_EXECUTION_STATUS>
<SCHEDULE_STATUS>ACTIVE</SCHEDULE_STATUS>
<SCHEDULE_SYSTEMNAME>node1</SCHEDULE_SYSTEMNAME>
<SCHEDULE_USERID>admin</SCHEDULE_USERID>
<SCHEDULE_TIMINGXML><![CDATA[<timingxml><days><day ofWeek="-1"><times><timeRange><range>0000-2359</range><interval>30</interval><onMinute>0</onMinute></timeRange></times></day></days><excludedDates></excludedDates></timingxml>]]></SCHEDULE_TIMINGXML>
</SCHEDULE>
</SCHEDULES>
</SI_RESOURCES>
到目前为止,这是我的XSL。它很乱,我不完全确定它是如何工作的。当我使用<xsl:template match="/">
时我没有输出,我不知道为什么。我唯一的猜测是它与CDATA有关。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|@*">
<xsl:for-each select="SCHEDULE">
<schedule>
<bp_name><xsl:value-of select="ASSOCIATED_BP_NAME"/></bp_name>
<run_on_startup><xsl:value-of select="SCHEDULE_ONSTARTUP"/></run_on_startup>
<status><xsl:value-of select="SCHEDULE_STATUS"/></status>
<user><xsl:value-of select="SCHEDULE_USERID"/></user>
<xsl:value-of select="SCHEDULE_TIMINGXML"/>
</schedule>
</xsl:for-each>
<xsl:apply-templates/>
</xsl:template>
我可以使用结果输出,如果它有一个根元素,即<schedules>
,但我无法弄清楚如何在那里得到它。有人能指出我正确的方向吗?
答案 0 :(得分:0)
你真的需要像那样转义输出吗?如果是,请尝试:
<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:template match="/SI_RESOURCES">
<schedules>
<xsl:for-each select="SCHEDULES/SCHEDULE">
<schedule>
<bp_name><xsl:value-of select="ASSOCIATED_BP_NAME"/></bp_name>
<run_on_startup><xsl:value-of select="SCHEDULE_ONSTARTUP"/></run_on_startup>
<status><xsl:value-of select="SCHEDULE_STATUS"/></status>
<user><xsl:value-of select="SCHEDULE_USERID"/></user>
<xsl:value-of select="SCHEDULE_TIMINGXML"/>
</schedule>
</xsl:for-each>
</schedules>
</xsl:template>
</xsl:stylesheet>