xslt复制子注释并添加额外节点
对于以下xml:xslt复制子注释并添加额外节点,xslt,xpath,copy,Xslt,Xpath,Copy,对于以下xml: <root> <employees> <employee> <Name>ABC</Name> <Dept>CS</Dept> <Designation>sse</Designation> </employee> </employees&
<root>
<employees>
<employee>
<Name>ABC</Name>
<Dept>CS</Dept>
<Designation>sse</Designation>
</employee>
</employees>
</root>
基础知识
反恐精英
上海证券交易所
我使用以下xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Name">
<xsl:copy-of select="."/>
<Age>34</Age>
</xsl:template>
<xsl:template match="Dept">
<xsl:copy-of select="."/>
<Domain>Insurance</Domain>
</xsl:template>
</xsl:stylesheet>
34
保险
我希望实现以下输出
<employee>
<Name>ABC</Name>
<Age>34</Age>
<Dept>CS</Dept>
<Domain>Insurance</Domain>
<Designation>sse</Designation>
</employee>
基础知识
34
反恐精英
保险
上海证券交易所
我不知道应该使用什么xpath将结果仅限于employee节点。好的,处理从文档节点(根节点)开始,由
/
表示,因此如果您编写模板
<xsl:template match="/">
<xsl:apply-templates select="//employee"/>
</xsl:template>
看起来很好,我还应该添加什么来让输出被节点包围(希望避免硬编码)。您可以将我发布的模板添加到您的样式表中,然后添加
和
,这足以得到您想要的结果。不确定要避免在何处使用元素名employee
。
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Identity transform -->
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Name">
<xsl:copy-of select="."/>
<Age>34</Age>
</xsl:template>
<xsl:template match="Dept">
<xsl:copy-of select="."/>
<Domain>Insurance</Domain>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates select="//employee"/>
</xsl:template>
</xsl:stylesheet>