XSLT复制除一个节点外的所有XML节点,并有条件地修改它
我有以下XML(简化):XSLT复制除一个节点外的所有XML节点,并有条件地修改它,xml,xslt,Xml,Xslt,我有以下XML(简化): 1. 彼得 100 29 2. 奥利弗 500 13 我需要将此xml转换为以下格式: <another-root> <insert> <info> <name>Peter</name> <salary>0</salary> <age>29</age>
1.
彼得
100
29
2.
奥利弗
500
13
我需要将此xml转换为以下格式:
<another-root>
<insert>
<info>
<name>Peter</name>
<salary>0</salary>
<age>29</age>
</info>
</insert>
<insert>
<info>
<name>Oliver</name>
<salary>500</salary>
<age>13</age>
</info>
</insert>
</another-root>
彼得
0
29
奥利弗
500
13
注意以下几点:
1-我有许多其他节点(person的子节点)
2-所以我需要复制个人的所有孩子
3-除了一个孩子的id
4-如果id=1,我需要将工资设置为0
我尝试了以下xslt:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<another-root>
<xsl:for-each select="root/person">
<Insert>
<xsl:apply-templates/>
</Insert>
</xsl:for-each>
</another-root>
</xsl:template>
<xsl:template match="details">
<info>
<xsl:copy-of select="./node()[not(self::id)]" />
</info>
</xsl:template>
<xsl:template match="details[id=1]/salary">
<salary>0</salary>
</xsl:template>
</xsl:stylesheet>
0
它正确地完成了工作,除了一件事,那就是:如果id=1,我需要将工资设置为0
请提供帮助。我认为最好的方法是从标识转换开始,然后为要转换的每个元素编写一个模板,要删除的元素会得到一个空模板:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<another-root>
<xsl:apply-templates/>
</another-root>
</xsl:template>
<xsl:template match="person">
<insert>
<xsl:apply-templates/>
</insert>
</xsl:template>
<xsl:template match="details">
<info>
<xsl:apply-templates/>
</info>
</xsl:template>
<xsl:template match="details/id"/>
<xsl:template match="details[id = 1]/salary">
<xsl:copy>0</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<another-root>
<xsl:apply-templates/>
</another-root>
</xsl:template>
<xsl:template match="person">
<insert>
<xsl:apply-templates/>
</insert>
</xsl:template>
<xsl:template match="details">
<info>
<xsl:apply-templates/>
</info>
</xsl:template>
<xsl:template match="details/id"/>
<xsl:template match="details[id = 1]/salary">
<xsl:copy>0</xsl:copy>
</xsl:template>
</xsl:stylesheet>