XSLT 复制并重命名节点
XSLT copy and rename a node
我想使用 XSLT 插入和重命名父节点和同级节点的某些元素,但无法完成工作。
这是我的 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>
<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>
这是我想要的 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>
请注意,file/student/variant 下的类型和值应重命名为 variant_type 和 variant_value,并复制到它们的兄弟级。 Student_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 已重命名..但未插入成绩中。仅包括 student_id、final、period 和 year。谁能帮我吗?谢谢!
问题出在模板匹配中的这些行 grade
:
<xsl:copy-of select="../variant/variant_type"/>
<xsl:copy-of select="../variant/variant_value"/>
您正在尝试复制元素 ../variant/variant_type
和 ../variant/variant_value
,但是 这些元素不存在于您的输入文件中;因为您想复制 您创建的重命名元素,您必须改用:
<xsl:apply-templates select="../variant/type"/>
<xsl:apply-templates select="../variant/value"/>
以便您将在输入文件中找到元素 ../variant/type
和 ../variant/value
,并应用您已经定义的重命名逻辑。
这样试试:
XSLT 1.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>
注:
结果与您请求的略有不同,因为您的格式不正确。
我想使用 XSLT 插入和重命名父节点和同级节点的某些元素,但无法完成工作。
这是我的 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>
<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>
这是我想要的 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>
请注意,file/student/variant 下的类型和值应重命名为 variant_type 和 variant_value,并复制到它们的兄弟级。 Student_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 已重命名..但未插入成绩中。仅包括 student_id、final、period 和 year。谁能帮我吗?谢谢!
问题出在模板匹配中的这些行 grade
:
<xsl:copy-of select="../variant/variant_type"/>
<xsl:copy-of select="../variant/variant_value"/>
您正在尝试复制元素 ../variant/variant_type
和 ../variant/variant_value
,但是 这些元素不存在于您的输入文件中;因为您想复制 您创建的重命名元素,您必须改用:
<xsl:apply-templates select="../variant/type"/>
<xsl:apply-templates select="../variant/value"/>
以便您将在输入文件中找到元素 ../variant/type
和 ../variant/value
,并应用您已经定义的重命名逻辑。
这样试试:
XSLT 1.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>
注:
结果与您请求的略有不同,因为您的格式不正确。