Xml XSLT复制并重命名节点
我想用XSLT插入和重命名父节点和同级节点的一些元素,但无法完成这项工作 这是我的XML源代码:Xml XSLT复制并重命名节点,xml,xslt,Xml,Xslt,我想用XSLT插入和重命名父节点和同级节点的一些元素,但无法完成这项工作 这是我的XML源代码: <?xml version="1.0" encoding="UTF-16"?> <file> <student_id>163</student_id> <report> <final>false</final> <period>1</period> <year&g
<?xml version="1.0" encoding="UTF-16"?>
<file>
<student_id>163</student_id>
<report>
<final>false</final>
<period>1</period>
<year>2015</year>
<variant>
<type>Country</type>
<value>Netherlands</value>
</variant>
<grade>
<value>8</value>
<topic>french</topic>
</grade>
<grade>
<value>7</value>
<topic>dutch</topic>
</grade>
</report>
</file>
163
错误的
1.
2015
国
荷兰
8.
法语
7.
荷兰的
这是我想要的XML输出:
<?xml version="1.0" encoding="UTF-16"?><file>
<student_id>163</student_id>
<report>
<final>false</final>
<period>1</period>
<year>2015</year>
<variant>
<variant_type>Country</variant_type>
<variant_value>Netherlands</variant_value>
</variant>
<grade>
<student_id>163</student_id>
<final>false</final>
<period>1</period>
<year>2015</year>
<variant_type>Country</variant_type>
<variant_value>Netherlands</variant_value>
<value>8</value>
<topic>french</topic>
</grade>
<grade>
<student_id>163</student_id>
<final>false</final>
<period>1</period>
<year>2015</year>
<variant_type>Country</variant_type>
<variant_value>Netherlands</variant_value>
<value>7</value>
<topic>dutch</topic>
</grade>
</report>
</file>
163
错误的
1.
2015
国
荷兰
163
错误的
1.
2015
国
荷兰
8.
法语
163
错误的
1.
2015
国
荷兰
7.
荷兰的
请注意,文件/student/variant下的类型和值应重命名为variant_类型和variant_值,并复制到其兄弟年级。学生id、期末、学期和年份也应复制到年级
我试图通过使用以下XSLT实现这一点:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template><xsl:template match="variant/type">
<variant_type>
<xsl:apply-templates select="@* | node()"/>
</variant_type></xsl:template>
<xsl:template match="variant/value">
<variant_value>
<xsl:apply-templates select="@* | node()"/>
</variant_value></xsl:template>
<xsl:template match="grade">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:copy-of select="../../student_id"/>
<xsl:copy-of select="../final"/>
<xsl:copy-of select="../period"/>
<xsl:copy-of select="../year"/>
<xsl:copy-of select="../variant/variant_type"/>
<xsl:copy-of select="../variant/variant_value"/>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
使用上述XSLT和variant/value将被重命名。。但并没有这样插入年级。只包括学生id、期末考试、期间和年份。有人能帮我吗?谢谢 问题在于模板匹配
等级中的这些行中:
<xsl:copy-of select="../variant/variant_type"/>
<xsl:copy-of select="../variant/variant_value"/>
这样,您就可以在输入文件中找到元素。/variant/type
和。/variant/value
,并应用您已经定义的重命名逻辑。尝试以下方法:
XSLT1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" 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="variant/type">
<variant_type>
<xsl:apply-templates/>
</variant_type>
</xsl:template>
<xsl:template match="variant/value">
<variant_value>
<xsl:apply-templates/>
</variant_value>
</xsl:template>
<xsl:template match="grade">
<xsl:copy>
<xsl:copy-of select="../../student_id"/>
<xsl:copy-of select="../final"/>
<xsl:copy-of select="../period"/>
<xsl:copy-of select="../year"/>
<xsl:apply-templates select="../variant/*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
注意:
结果与您要求的结果略有不同,因为您的格式不正确
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" 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="variant/type">
<variant_type>
<xsl:apply-templates/>
</variant_type>
</xsl:template>
<xsl:template match="variant/value">
<variant_value>
<xsl:apply-templates/>
</variant_value>
</xsl:template>
<xsl:template match="grade">
<xsl:copy>
<xsl:copy-of select="../../student_id"/>
<xsl:copy-of select="../final"/>
<xsl:copy-of select="../period"/>
<xsl:copy-of select="../year"/>
<xsl:apply-templates select="../variant/*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>