输入XML
<Employer>
<Employeedetail>Sam SR Engineer 10%</ Employeedetail >
<Employeedetail>Sam SR 10%</ Employeedetail >
<Employeedetail>Sam SR</ Employeedetail >
</ Employer >
以下是输出输入XML
<Employer>
< Employeedetails >
< Employeedetail1>
< Employeedetail >
<Name>Sam</ Name >
<Grade> SR Engineer </ Grade >
<Experience>10</ Experience >
</ Employeedetail >
</ Employeedetail1>
</ Employeedetails>
</ Employer >
<Employer>
< Employeedetails >
< Employeedetail1>
< Employeedetail >
<Name>Sam</ Name >
<Grade>SR</ Grade >
<Experience>10</ Experience >
</ Employeedetail >
</ Employeedetail1>
</ Employeedetails>
</ Employer >
<Employer>
< Employeedetails >
< Employeedetail1>
< Employeedetail >
<Name>Sam</ Name >
<Grade>SR</ Grade >
<Experience>10</ Experience >
</ Employeedetail >
</ Employeedetail1>
</ Employeedetails>
</ Employer >
如果我们有三个空间扩展节点我们将得到输出
,他们已经帮助堆栈溢出但是如果你看一下这个级别的高级工程师会有一个轻微的转折,但是我所拥有的代码会将SR显示在Grade元素中,而Engineer和10(Experience)将会出现在体验中元件。
如果没有经验值,默认情况下为10。
在上面的输入XML中,将会有许多员工详细信息。
Here is the link for that xslt
请帮助解决上述问题
提前致谢
答案 0 :(得分:1)
查看xsl:analyze-string
http://www.w3.org/TR/xslt20/#analyze-string,例如
<xsl:template match="Employeedetail">
<Employeedetail>
<xsl:analyze-string="." regex="(\w+)\s+(\w+(\s\w+)?)\s+(\d+)">
<xsl:matching-substring>
<Name><xsl:value-of select="regex-group(1)"/></Name>
<Grade><xsl:value-of select="regex-group(2)"/></Grade>
<Experience><xsl:value-of select="regex-group(4)"/></Experience>
</xsl:matching-substring>
</xsl:analyze-string>
</Employeedetail>
</xsl:template>
未经测试,但应指出正确的方向。
答案 1 :(得分:0)
以下是几个起点:
答案 2 :(得分:0)
此XSLT 1.0转换:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="Employeedetail">
<xsl:variable name="vExp" select=
"substring(., string-length()-2, 2)"/>
<Employer>
<Employeedetails>
<Employeedetail1>
<Employeedetail>
<name><xsl:value-of select="substring-before(., ' ')"/></name>
<grade>
<xsl:value-of select=
"substring-before(substring-after(., ' '), $vExp)"/>
</grade>
<experience><xsl:value-of select="$vExp"/></experience>
</Employeedetail>
</Employeedetail1>
</Employeedetails>
</Employer>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档时:
<Employer>
<Employeedetail>Sam SR Engineer 10%</Employeedetail>
</Employer>
生成想要的正确结果:
<Employer>
<Employeedetails>
<Employeedetail1>
<Employeedetail>
<name>Sam</name>
<grade>SR Engineer </grade>
<experience>10</experience>
</Employeedetail>
</Employeedetail1>
</Employeedetails>
</Employer>