Xml 如何从元素中提取部分值并将其作为属性分配给另一个元素
我正在通过XSLT进行XML到XML的转换 我想从元素中提取一个初始值,并将其作为属性分配给新元素 源XML:Xml 如何从元素中提取部分值并将其作为属性分配给另一个元素,xml,xslt,xslt-grouping,Xml,Xslt,Xslt Grouping,我正在通过XSLT进行XML到XML的转换 我想从元素中提取一个初始值,并将其作为属性分配给新元素 源XML: <content> <component> <aaa>HI <strong>[a_b_c]</strong> : More Information Needed <strong>[d_e_f]</
<content>
<component>
<aaa>HI
<strong>[a_b_c]</strong>
: More Information Needed
<strong>[d_e_f]</strong>XXX
</aaa>
</component>
<content>
你好
[a\u b\u c]
:需要更多信息
[d_e_f]XXX
目标XML:
<ddd>hi<dv name='a_b_c'/>: More Information Needed <dv name='d_e_f'/> XXX
</ddd>
hi:需要更多信息XXX
任何人都可以建议如何通过XSLT实现这一点
提前感谢您。XSLT1.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="aaa">
<ddd>
<xsl:value-of select="substring-before(.,'[')"/>
<dv name="{substring-before(substring-after(.,'['),']')}"/>
</ddd>
</xsl:template>
</xsl:stylesheet>
<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="aaa">
<ddd>
<xsl:value-of select="tokenize(.,'\[')[1]"/>
<dv name="{tokenize(tokenize(.,'\[')[2],'\]')[1]}"/>
</ddd>
</xsl:template>
</xsl:stylesheet>
或
XSLT2.0
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="aaa">
<ddd>
<xsl:value-of select="substring-before(.,'[')"/>
<dv name="{substring-before(substring-after(.,'['),']')}"/>
</ddd>
</xsl:template>
</xsl:stylesheet>
<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="aaa">
<ddd>
<xsl:value-of select="tokenize(.,'\[')[1]"/>
<dv name="{tokenize(tokenize(.,'\[')[2],'\]')[1]}"/>
</ddd>
</xsl:template>
</xsl:stylesheet>
编辑
<xsl:stylesheet version="1.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="aaa">
<ddd>
<xsl:apply-templates select="node()|@*"/>
</ddd>
</xsl:template>
<xsl:template match="content|component">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="strong">
<dv name="{normalize-space(.)}"/>
</xsl:template>
</xsl:stylesheet>
谢谢你的建议。.我已经编辑了这个问题,你能不能修改一下答案,比如如何递归。@muzimil-我不知道你说的递归是什么意思。您的新输入/输出示例根本不需要字符串操作。您只需删除内容
、组件
和强
元素,并重命名aaa
元素。不,我还删除了部分内容..如[a_b_c]等。我的意思是,需要重复执行多次(如果需要删除更多内容)@muzimil-该内容位于strong
元素中。请在我的更新中尝试样式表。抱歉,我上传了错误的问题。现在我已经编辑了excat one,请查看它。