感谢这里成员的慷慨反馈,我的 XML 到 XML 项目取得了一些重大进展。
也就是说,我在项目的最终版本中遇到了两个问题,并希望更好地了解为什么会出现这些问题。
首先,我需要创建一个由多个值组成的串联元素值。我使用以下代码进行此工作:
<xsl:template match="estimate/JobParts/JobPart/description">
<description>
<xsl:value-of select="concat(estimate/description,'_QTY-',estimate/JobParts/JobPart/qtyOrdered,'_',estimate/JobParts/JobPart/itemTemplate)"/>
</description>
</xsl:template>
这会找到 的所有匹配estimate/JobParts/JobPart/description
项,但在所有情况下,在替换描述值时仅使用第一个匹配的兄弟元素值。
在这种情况下,我需要使用密钥来管理迭代吗?如果是这样,我应该如何创建一个?是否有另一种使用应用模板处理此问题的方法,以便可以自动处理迭代管理,如果是这样,将如何构建?
我遇到的另一个问题是我需要手动将 CDATA 指定添加到元素,并且无法将该元素添加到 cdata-section-elements 的列表中,因为在文档结构中嵌套了另一个同名的元素不需要 CDATA 指定。
这是我试图用来手动添加 CDATA 标志的代码:
<xsl:template match="estimate/description">
<description>
<xsl:text disable-output-escaping="yes">
<![CDATA[
</xsl:text>
<xsl:value-of select="estimate/description"/>
<xsl:text disable-output-escaping="yes">
]]>
</xsl:text>
</description>
</xsl:template>
有趣的是,这对目标描述元素没有任何影响。
如果有人知道为什么这些转换中的任何一个(或两个)都不能正常工作,我渴望学习和理解。
提前为大量代码道歉,但这里是原始 XML:
<?xml version="1.0"?>
<PODOrderSheet_Main>
<estimate>
<customer>LINFNC</customer>
<newJob>Y</newJob>
<incrementJobVersion>Y</incrementJobVersion>
<description><![CDATA[409511]]></description>
<billPartsTogether>1</billPartsTogether>
<dateSetup><![CDATA[4/24/2013]]></dateSetup>
<promiseDate><![CDATA[4/24/2013]]></promiseDate>
<scheduledShipDate><![CDATA[4/24/2013]]></scheduledShipDate>
<adminStatus>O</adminStatus>
<shipVia>1</shipVia>
<jobType>5020</jobType>
<poNum><![CDATA[409511]]></poNum>
<itemTemplate><![CDATA[33503MN_0212]]></itemTemplate>
<JobParts>
<JobPart>
<jobPart>01</jobPart>
<contactNum/>
<description><![CDATA[REPLACEMENT OF LIFE INSURANCE OR ANNUITIES MINNESOTA]]></description>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[3]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[33503MN_0212]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>01</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
</JobParts>
</estimate>
<estimate>
<customer>LINFNC</customer>
<newJob>Y</newJob>
<incrementJobVersion>Y</incrementJobVersion>
<description><![CDATA[409511]]></description>
<billPartsTogether>1</billPartsTogether>
<dateSetup><![CDATA[4/24/2013]]></dateSetup>
<promiseDate><![CDATA[4/24/2013]]></promiseDate>
<scheduledShipDate><![CDATA[4/24/2013]]></scheduledShipDate>
<adminStatus>O</adminStatus>
<shipVia>1</shipVia>
<jobType>5020</jobType>
<poNum><![CDATA[409511]]></poNum>
<itemTemplate><![CDATA[AL-FSC-FST068_Z08]]></itemTemplate>
<JobParts>
<JobPart>
<jobPart>01</jobPart>
<contactNum/>
<description><![CDATA[AMERICAN LEGACY III C SHARE FACT SHEET ]]></description>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[1]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[AL-FSC-FST068_Z08]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>01</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
</JobParts>
</estimate>
<estimate>
<customer>LINFNC</customer>
<newJob>Y</newJob>
<incrementJobVersion>Y</incrementJobVersion>
<description><![CDATA[409511]]></description>
<billPartsTogether>1</billPartsTogether>
<dateSetup><![CDATA[4/24/2013]]></dateSetup>
<promiseDate><![CDATA[4/24/2013]]></promiseDate>
<scheduledShipDate><![CDATA[4/24/2013]]></scheduledShipDate>
<adminStatus>O</adminStatus>
<shipVia>1</shipVia>
<jobType>5020</jobType>
<poNum><![CDATA[409511]]></poNum>
<itemTemplate><![CDATA[AN06819-AL3C_1012]]></itemTemplate>
<JobParts>
<JobPart>
<jobPart>01</jobPart>
<contactNum/>
<description><![CDATA[AMERICAN LEGACY III C SHARE APPLICATION SUPPLEMENT - MULTI-STATE]]></description>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[1]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[AN06819-AL3C_1012]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>01</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
</JobParts>
</estimate>
</PODOrderSheet_Main>
这是所需的输出:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE estimate>
<estimate>
<customer>LINFNC</customer>
<newJob>Y</newJob>
<incrementJobVersion>Y</incrementJobVersion>
<description><![CDATA[409511]]></description>
<billPartsTogether>1</billPartsTogether>
<dateSetup><![CDATA[4/24/2013]]></dateSetup>
<promiseDate><![CDATA[4/24/2013]]></promiseDate>
<scheduledShipDate><![CDATA[4/24/2013]]></scheduledShipDate>
<adminStatus>O</adminStatus>
<shipVia>1</shipVia>
<jobType>5020</jobType>
<poNum><![CDATA[409511]]></poNum>
<itemTemplate><![CDATA[33503MN_0212]]></itemTemplate>
<JobParts>
<JobPart>
<jobPart>01</jobPart>
<contactNum/>
<description>409511_QTY-3_33503MN_0212</description>
<priority>1</priority>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[3]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[33503MN_0212]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>01</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
<JobPart>
<jobPart>02</jobPart>
<contactNum/>
<description>409511_QTY-1_AL-FSC-FST068_Z08</description>
<priority>1</priority>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[1]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[AL-FSC-FST068_Z08]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>02</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
<JobPart>
<jobPart>03</jobPart>
<contactNum/>
<description>409511_QTY-1_AN06819-AL3C_1012</description>
<priority>1</priority>
<productionStatus>O</productionStatus>
<qtyOrdered><![CDATA[1]]></qtyOrdered>
<shipToContact/>
<itemTemplate><![CDATA[AN06819-AL3C_1012]]></itemTemplate>
<JobNotes>
<JobNote>
<department>001</department>
<jobPart>03</jobPart>
<note><![CDATA[Rush Order]]></note>
</JobNote>
</JobNotes>
</JobPart>
</JobParts>
</estimate>
这是现在的 XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes" encoding="UTF-8" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:output cdata-section-elements="dateSetup promiseDate scheduledShipDate poNum qtyOrdered itemTemplate note"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="/PODOrderSheet_Main">
<estimate>
<xsl:copy-of select="estimate[1]/customer"/>
<xsl:copy-of select="estimate[1]/newJob"/>
<xsl:copy-of select="estimate[1]/incrementJobVersion"/>
<xsl:copy-of select="estimate[1]/description"/>
<xsl:copy-of select="estimate[1]/billPartsTogether"/>
<xsl:copy-of select="estimate[1]/dateSetup"/>
<xsl:copy-of select="estimate[1]/promiseDate"/>
<xsl:copy-of select="estimate[1]/scheduledShipDate"/>
<xsl:copy-of select="estimate[1]/adminStatus"/>
<xsl:copy-of select="estimate[1]/shipVia"/>
<xsl:copy-of select="estimate[1]/jobType"/>
<xsl:copy-of select="estimate[1]/poNum"/>
<xsl:copy-of select="estimate[1]/itemTemplate"/>
<JobParts>
<xsl:apply-templates select="estimate/JobParts/JobPart"/>
</JobParts>
</estimate>
</xsl:template>
<xsl:template match="estimate/description">
<description>
<xsl:text disable-output-escaping="yes">
<![CDATA[
</xsl:text>
<xsl:value-of select="estimate/description"/>
<xsl:text disable-output-escaping="yes">
]]>
</xsl:text>
</description>
</xsl:template>
<xsl:template match="//estimate/JobParts/JobPart/description">
<description>
<xsl:value-of select="concat(//estimate/description,'_QTY-',//JobPart/qtyOrdered,'_',//JobPart/itemTemplate)"/>
</description>
</xsl:template>
<xsl:template match="jobPart">
<xsl:copy><xsl:number count="JobPart" level="any" format="01"/></xsl:copy>
</xsl:template>
</xsl:stylesheet>
非常感谢...