我正在尝试使用xslt 1.0 / xslt 2.0将逗号替换为XML中的单个空格。 请帮忙,谢谢。
使用XML
<Root>
<Master>
<Worker_group>
<Emp_ID>1001</Emp_ID>
<E_Name>Worker1 , Jr </E_Name>
<Line1>2001 Mark ST,</LINE1>
<Line2>Building 2, Apt 101</LINE1>
<City> XYZ </City>
<state>NC</State>
</Worker_group>
</Master>
<Master>
<Worker_group>
<Emp_ID>1002</Emp_ID>
<E_Name>Worker2 , Sr</E_Name>
<Line1>1001 STACY ST,</LINE1>
<Line2>Building 11, Apt 112</LINE1>
<City> ABC </City>
<state>FL</State>
</Worker_group>
<Dependents_group>
<Dep_Emp_ID>1001</Dep_Emp_ID>
<D_Name>Dependent1, </D_Name>
<D_Line1>1001 STACY ST,</D_LINE1>
<D_Line2>Building 11, Apt 112</D_LINE2>
<D_City> ABC </D_City>
<D_state>FL</D_State>
</Dependents_group>
<Dependents_group>
<Dep_Emp_ID>1001</Dep_Emp_ID>
<D_Name>Dependent2 </D_Name>
<D_Line1>1001 STACY ST,</LINE1>
<D_Line2>Building 11, Apt 112</D_LINE2>
<D_City> ABC </D_City>
<D_state>FL</D_State>
</Dependents_group>
<Dependents_group>
<Dep_Emp_ID>1001</Dep_Emp_ID>
<D_Name>Dependent3 , Mr </D_Name>
<D_Line1>1001 STACY ST,</D_LINE1>
<D_Line2>Building 11, Apt 112</D_LINE2>
<D_City> ABC </D_City>
<D_state>FL</D_State>
</Dependents_group>
</Master>
<Master>
<Worker_group>
<Emp_ID>1003</Emp_ID>
<E_Name>Worker3, </E_Name>
<Line1>11 Hide ST</LINE1>
<Line2>Apt 234,</LINE1>
<City> ABC, </City>
<state>SC</State>
</Worker_group>
</Master>
</Root>
使用XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:template match="/Root">
<!-- header -->
<xsl:text>id,relationship,Name </xsl:text>
<!-- data -->
<xsl:for-each select="Master/*">
<xsl:value-of select="Emp_ID | Dep_Emp_ID"/>
<xsl:text>,</xsl:text>
<xsl:number count="*"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="E_Name | D_Name"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="Line1 | D_Line1"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="Line2 | D_Line2"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="City | D_City"/>
<xsl:text>,</xsl:text>
<xsl:value-of select="State | D_State"/>
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
id,relationship,Name,Line1,Line2,City,State
1001,1,Worker1 Jr,2001 Mark ST,Building 2 Apt 101,XYZ,NC
1002,1,Worker2 Sr,1001 STACY ST,Building 11 Apt 112,ABC,FL
1001,2,Dependent1,1001 STACY ST,Building 11 Apt 112,ABC,FL
1001,3,Dependent2,1001 STACY ST,Building 11 Apt 112,ABC,FL
1001,4,Dependent3 Mr,1001 STACY ST,Building 11 Apt 112,ABC,FL
1003,1,Worker3,11 Hide St,Apt 234,ABC,Sc
答案 0 :(得分:1)
使用translate
函数。
用任何字符替换,
。另外,您可以替换“。”与任何节点。
translate(.,',', ' ')
答案 1 :(得分:0)
替换:
<xsl:value-of select="E_Name | D_Name"/>
具有:
<xsl:value-of select="normalize-space(translate(E_Name | D_Name, ',', ' '))"/>
其他数据单元的依此类推。