【问题标题】:Concatenate fields using XML and XSLT in loop在循环中使用 XML 和 XSLT 连接字段
【发布时间】: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。

感谢任何帮助。

【问题讨论】:

    标签: xml xslt


    【解决方案1】:

    你有什么理由不能简单地做:

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:itk="http://www.taleo.com/ws/integration/toolkit/2005/07" 
    exclude-result-prefixes="itk">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/itk:Document">
        <xsl:apply-templates select="itk:Content/itk:ExportXML/itk:record"/>
    </xsl:template>
    
    <xsl:template match="itk:record">
        <Job>
            <Title>
                <xsl:value-of select="itk:field[@name='Title']" />
            </Title>
            <Position_ID>
                <xsl:value-of select="itk:field[@name='Position_ID']" />
            </Position_ID>
            <RequisitionNumber>
                <xsl:value-of select="itk:field[@name='RequisitionNumber']" />
            </RequisitionNumber>
            <Location>
                <xsl:value-of select="itk:field[@name='LocationCode']" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="itk:field[@name='LocationName']" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="itk:field[@name='LocationCountry']" />
                <xsl:text> </xsl:text>
                <xsl:value-of select="itk:field[@name='LocationCity']" />
            </Location>
        </Job>    
    </xsl:template>
    
    </xsl:stylesheet>
    

    或者,如果使用 XSLT 2.0,则更简单:

    <xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xpath-default-namespace="http://www.taleo.com/ws/integration/toolkit/2005/07" >
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>
    
    <xsl:template match="/Document">
        <xsl:apply-templates select="Content/ExportXML/record"/>
    </xsl:template>
    
    <xsl:template match="record">
        <Job>
            <xsl:for-each select="field[not(starts-with(@name, 'Location'))]">
                <xsl:element name="{@name}">
                    <xsl:value-of select="." />
                </xsl:element>
            </xsl:for-each>
            <Location>
                <xsl:value-of select="field[starts-with(@name, 'Location')]" />
            </Location>
        </Job>    
    </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • 我确实让它工作了,我猜想有点类似于这个解决方案。几分钟后我发布了一个答案,因此将其标记为答案。
    • 我可能遗漏了一些东西,但我在这里看不到任何依赖于 XSLT 2.0 的东西。
    • @MichaelKay 最后一个 xsl:value-of 检索 4 个带有分隔符的值。在 XSLT 1.0 中,它只会返回 LocationCode 的值。另外,XSLT 1.0 中没有xpath-default-namespace
    • 谢谢 - 没发现!
    猜你喜欢
    • 2020-11-10
    • 1970-01-01
    • 2021-12-23
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 2017-11-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多