XSL 问题:输出缺少根元素
XSL Trouble: Output is missing root element
我过去做过一些 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>
,但我不知道如何把它放到那里。谁能指出我正确的方向?
你真的需要这样转义输出吗?如果是,请尝试:
<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>
我过去做过一些 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>
,但我不知道如何把它放到那里。谁能指出我正确的方向?
你真的需要这样转义输出吗?如果是,请尝试:
<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>