【发布时间】:2018-11-19 04:07:06
【问题描述】:
我有一个源 XML 文件,我想将它转换成另一个格式不同的 XML 文件。我的源 XML 文件使用属性(我想,如果我在这里错了,请纠正我)并且目标 XML 格式应该是纯父子 XML 标记。我对直截了当的方法没有任何问题。
但是,我需要找到一种使用 XSLT 将 2 个或更多字段连接成单个元素的方法。我尝试使用变量然后将它们连接起来,但是我遇到了一些变量问题,它们只能分配一次。
我的 XML 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:ns1="http://www.taleo.com/ws/integration/toolkit/2005/07" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Attributes><Attribute name="count">1</Attribute><Attribute name="duration">0:00:00.109</Attribute><Attribute name="entity">SourcingRequest</Attribute><Attribute name="mode">XML</Attribute><Attribute name="version">http://www.taleo.com/ws/tee800/2009/01</Attribute></Attributes><Content>
<ExportXML xmlns="http://www.taleo.com/ws/integration/toolkit/2005/07">
<record>
<field name="Title">Project Analyst</field>
<field name="Position_ID">64057</field>
<field name="RequisitionNumber">180767</field>
<field name="LocationCode">HQ</field>
<field name="LocationName">Headquarters</field>
<field name="LocationCountry">Country</field>
<field name="LocationCity">City</field>
</record>
</ExportXML></Content></Document>
那么我的 XSL 是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:itk="http://www.taleo.com/ws/integration/toolkit/2005/07">
<xsl:output method="xml" encoding="UTF-8" omit-xml-declaration="yes" cdata-section-elements="DescriptionInternal DescriptionExternal QualificationExternal QualificationInternal"/>
<xsl:template match="text()"/>
<xsl:template match="itk:record">
<xsl:element name="Job">
<xsl:for-each select="itk:field">
<!-- Set variables -->
<xsl:variable name="location_code">
<xsl:if test="@name='LocationCode'">
<xsl:value-of select="." />
</xsl:if>
</xsl:variable>
<xsl:variable name="location_name">
<xsl:if test="@name='LocationName'">
<xsl:value-of select="." />
</xsl:if>
</xsl:variable>
<xsl:variable name="location_country">
<xsl:if test="@name='LocationCountry'">
<xsl:value-of select="." />
</xsl:if>
</xsl:variable>
<xsl:variable name="location_city">
<xsl:if test="@name='LocationCity'">
<xsl:value-of select="." />
</xsl:if>
</xsl:variable>
<xsl:choose>
<xsl:when test="@name='LocationCode' or @name='LocationName' or @name='LocationCountry' or @name='LocationCity'">
<xsl:if test="@name='LocationCity'">
<xsl:element name="Location">
<xsl:value-of select="concat($location_code,$location_name,$location_country,$location_city)" />
</xsl:element>
<xsl:element name="LocationCode">
<xsl:value-of select="$location_code" />
</xsl:element>
<xsl:element name="LocationName">
<xsl:value-of select="$location_code" />
</xsl:element>
</xsl:if>
</xsl:when>
<xsl:otherwise>
<!-- Set variables. -->
<xsl:variable name="NodeName" select="@name"/>
<xsl:element name="{@name}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
但是,我的输出是这样的:
<Job>
<Title>Project Analyst</Title>
<Position_ID>64057</Position_ID>
<RequisitionNumber>180767</RequisitionNumber>
<Location>City</Location>
</Job>
但我想要的输出应该是这样的:
<Job>
<Title>Project Analyst</Title>
<Position_ID>64057</Position_ID>
<RequisitionNumber>180767</RequisitionNumber>
<Location>HQ Headquarters Country City</Location>
</Job>
如果我的猜测是正确的,我认为循环会重置变量的值。所以最后的就是价值。
有没有其他方法可以实现所需的输出。尽管我的源文件可以更改为 CSV 格式,但我的选项仅限于 XSL。
感谢任何帮助。
【问题讨论】: