使用XSLT根据属性值移动XML元素
我需要根据属性值将XML元素移动到相应的位置。当id的属性值匹配时,元素使用XSLT根据属性值移动XML元素,xml,xslt,Xml,Xslt,我需要根据属性值将XML元素移动到相应的位置。当id的属性值匹配时,元素应该从移动到相应的位置 示例XML: <m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML"> <m:front> <m:p>This is <m:footnote id="1"/>para</m:p> </m:front> <m:body> <m:p&
应该从
移动到相应的位置
示例XML:
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1"/>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2"/>para</m:p>
<m:p>This is <m:footnote id="3"/>para</m:p>
<m:p>This is <m:footnote id="4"/>para</m:p>
</m:body>
<m:endnote>
<m:footnote id="1">This is footnote 1</m:footnote>
<m:footnote id="2">This is footnote 2</m:footnote>
<m:footnote id="3">This is footnote 3</m:footnote>
<m:footnote id="4">This is footnote 4</m:footnote>
</m:endnote>
</m:chapter>
<?xml version='1.0' encoding='UTF-8' ?>
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2">This is footnote 2</m:footnote>para</m:p>
<m:p>This is <m:footnote id="3">This is footnote 3</m:footnote>para</m:p>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>>para</m:p>
</m:body>
</m:chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:chapter">
<xsl:copy>
<xsl:apply-templates/>
<xsl:for-each select="child::*/descendant::m:footnote">
<xsl:choose>
<xsl:when test="not(parent::m:endnote)">
<xsl:if test="string(self::m:footnote/@id) = string('ancestor::m:chapter/m:endnote/m:footnote/@id')">
<xsl:copy-of select="m:chapter/m:endnote/m:footnote"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="m:endnote"></xsl:template>
</xsl:stylesheet>
这是第
这是第
这是第
这是第
这是脚注1
这是脚注2
这是脚注3
这是脚注4
所需输出:
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1"/>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2"/>para</m:p>
<m:p>This is <m:footnote id="3"/>para</m:p>
<m:p>This is <m:footnote id="4"/>para</m:p>
</m:body>
<m:endnote>
<m:footnote id="1">This is footnote 1</m:footnote>
<m:footnote id="2">This is footnote 2</m:footnote>
<m:footnote id="3">This is footnote 3</m:footnote>
<m:footnote id="4">This is footnote 4</m:footnote>
</m:endnote>
</m:chapter>
<?xml version='1.0' encoding='UTF-8' ?>
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2">This is footnote 2</m:footnote>para</m:p>
<m:p>This is <m:footnote id="3">This is footnote 3</m:footnote>para</m:p>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>>para</m:p>
</m:body>
</m:chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:chapter">
<xsl:copy>
<xsl:apply-templates/>
<xsl:for-each select="child::*/descendant::m:footnote">
<xsl:choose>
<xsl:when test="not(parent::m:endnote)">
<xsl:if test="string(self::m:footnote/@id) = string('ancestor::m:chapter/m:endnote/m:footnote/@id')">
<xsl:copy-of select="m:chapter/m:endnote/m:footnote"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="m:endnote"></xsl:template>
</xsl:stylesheet>
这是脚注1
这是脚注2para
这是脚注3para
这是脚注4>第
XSLT已尝试:
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1"/>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2"/>para</m:p>
<m:p>This is <m:footnote id="3"/>para</m:p>
<m:p>This is <m:footnote id="4"/>para</m:p>
</m:body>
<m:endnote>
<m:footnote id="1">This is footnote 1</m:footnote>
<m:footnote id="2">This is footnote 2</m:footnote>
<m:footnote id="3">This is footnote 3</m:footnote>
<m:footnote id="4">This is footnote 4</m:footnote>
</m:endnote>
</m:chapter>
<?xml version='1.0' encoding='UTF-8' ?>
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2">This is footnote 2</m:footnote>para</m:p>
<m:p>This is <m:footnote id="3">This is footnote 3</m:footnote>para</m:p>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>>para</m:p>
</m:body>
</m:chapter>
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:m="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:chapter">
<xsl:copy>
<xsl:apply-templates/>
<xsl:for-each select="child::*/descendant::m:footnote">
<xsl:choose>
<xsl:when test="not(parent::m:endnote)">
<xsl:if test="string(self::m:footnote/@id) = string('ancestor::m:chapter/m:endnote/m:footnote/@id')">
<xsl:copy-of select="m:chapter/m:endnote/m:footnote"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="m:endnote"></xsl:template>
</xsl:stylesheet>
这应该可以做到:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.w3.org/1998/Math/MathML" exclude-result-prefixes="m">
<xsl:output method="xml" encoding="UTF-8" indent="no"/>
<xsl:key name="kEndnote" match="m:endnote/m:footnote" use="@id"/>
<xsl:key name="kFootnoteRef" match="m:p/m:footnote" use="@id"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:footnote">
<xsl:copy>
<xsl:apply-templates select="@*" />
<xsl:value-of select="key('kEndnote', @id)"/>
</xsl:copy>
</xsl:template>
<xsl:template match="m:endnote[not(m:footnote[not(key('kFootnoteRef', @id))])]" />
<xsl:template match="m:endnote/m:footnote[key('kFootnoteRef', @id)]" />
</xsl:stylesheet>
在样例输入上运行时,将生成:
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2">This is footnote 2</m:footnote>para</m:p>
<m:p>This is <m:footnote id="3">This is footnote 3</m:footnote>para</m:p>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>para</m:p>
</m:body>
</m:chapter>
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>para</m:p>
</m:body>
<m:endnote>
<m:footnote id="2">This is footnote 2</m:footnote>
<m:footnote id="3">This is footnote 3</m:footnote>
</m:endnote>
</m:chapter>
这是脚注1
这是脚注2para
这是脚注3para
这是脚注4para
在删除第二个和第三个
的情况下运行示例输入时,会产生:
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="2">This is footnote 2</m:footnote>para</m:p>
<m:p>This is <m:footnote id="3">This is footnote 3</m:footnote>para</m:p>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>para</m:p>
</m:body>
</m:chapter>
<m:chapter xmlns:m="http://www.w3.org/1998/Math/MathML">
<m:front>
<m:p>This is <m:footnote id="1">This is footnote 1</m:footnote>para</m:p>
</m:front>
<m:body>
<m:p>This is <m:footnote id="4">This is footnote 4</m:footnote>para</m:p>
</m:body>
<m:endnote>
<m:footnote id="2">This is footnote 2</m:footnote>
<m:footnote id="3">This is footnote 3</m:footnote>
</m:endnote>
</m:chapter>
这是脚注1
这是脚注4para
这是脚注2
这是脚注3
谢谢您的回答,另一件事是,若id的属性值不匹配,那个么应该保留id不匹配的“”元素。@siva2012更新了我的答案。