我需要转换XML并遇到一些问题...
当前的XML:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<ManagerFirstName>Joe</ManagerFirstName>
<ManagerLastName>Schmoe</ManagerLastName>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
所需输出:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<supervisorName>Schmoe, Joe</supervisorName>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
当前的XSL:
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" >
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="node()">
<xsl:copy><xsl:apply-templates select="node()"/></xsl:copy>
</xsl:template>
<xsl:template match="ManagerFirstName">
<supervisorName>
<xsl:apply-templates select="node()"/>
<xsl:value-of select="/ManagerLastName"/>
<xsl:text>, </xsl:text>
<xsl:value-of select="/ManagerFirstName"/>
</supervisorName>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
这是行不通的,我无法弄清楚。目前正在输出的XML如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<supervisorName>Joe, </supervisorName>
<ManagerLastName>Schmoe/ManagerLastName>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
我觉得我好近...
更新 如何确保如果ManagerFirstName和ManagerLastName为空白,那么supervisorName内没有逗号?
更新2
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/> <xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee">
<tbl_EmployeeList><xsl:apply-templates select="@*|node()"/></tbl_EmployeeList>
</xsl:template>
<xsl:template match="tbl_EmployeeList">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<supervisorName>
<xsl:value-of select="(ManagerLastName,ManagerFirstName)" separator=", "/>
</supervisorName>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
由于您使用的是XSLT 2.0,因此可以separator在xsl:value-of...中使用该属性。
XML输入
<Employees>
<Employee>
<ManagerFirstName>Joe</ManagerFirstName>
<ManagerLastName>Schmoe</ManagerLastName>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
XSLT 2.0
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Employee">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<supervisorName>
<xsl:value-of select="(ManagerLastName,ManagerFirstName)" separator=", "/>
</supervisorName>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
XML输出
<Employees>
<Employee>
<supervisorName>Schmoe, Joe</supervisorName>
</Employee>
</Employees>
Run Code Online (Sandbox Code Playgroud)
注意:如果没有ManagerLastName或ManagerFirstName,则不会输出任何分隔符。
| 归档时间: |
|
| 查看次数: |
8942 次 |
| 最近记录: |