Xml XSLT-将最后一个元素移到指定元素上方
我的意见是Xml XSLT-将最后一个元素移到指定元素上方,xml,xslt,Xml,Xslt,我的意见是 <meta> ................... ................... <doctitle>Pocket Atlas of Sectional Anatomy</doctitle> <docsubtitle>Computed Tomography and Magnetic Resonance Imaging</docsubtitle> <docdate type="released">201
<meta>
...................
...................
<doctitle>Pocket Atlas of Sectional Anatomy</doctitle>
<docsubtitle>Computed Tomography and Magnetic Resonance Imaging</docsubtitle>
<docdate type="released">2017-10-27</docdate>
<relatedobjects>
<relpdfio/>
</relatedobjects>
<publisher>
<address>
<street>333 Seventh Ave.</street>
......
</address>
</publisher>
<version type="print">4th Edition</version>
</meta>
...................
...................
袖珍断层解剖学图谱
计算机断层扫描和磁共振成像
2017-10-27
第七大道333号。
......
第四版
产出应该是,
<meta>
...................
...................
<doctitle>Pocket Atlas of Sectional Anatomy</doctitle>
<docsubtitle>Computed Tomography and Magnetic Resonance Imaging</docsubtitle>
<docdate type="released">2017-10-27</docdate>
<version type="print">4th Edition</version>
<relatedobjects>
<relpdfio/>
</relatedobjects>
<publisher>
<address>
<street>333 Seventh Ave.</street>
......
</address>
</publisher>
</meta>
...................
...................
袖珍断层解剖学图谱
计算机断层扫描和磁共振成像
2017-10-27
第四版
第七大道333号。
......
我们希望将“version”元素移到“docdate”元素之后。在这种情况下,我很难编写XSLT。请您指导我们。可能有比下面发布的更好的解决方案,但以下是您可以尝试的。如果有更多的元素,那么按照所需的顺序排列它们将变得复杂,因为顺序是在变量中预定义的
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:variable name="elementOrder" select="'|doctitle|docsubtitle|docdate|version|relatedobjects|publisher|'" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="meta">
<xsl:copy>
<xsl:apply-templates select="*">
<xsl:sort select="substring-before($elementOrder, concat('|',name(),'|'))" />
</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" indent="yes" />
<xsl:strip-space elements="*" />
<xsl:variable name="elementOrder" select="'|doctitle|docsubtitle|docdate|version|relatedobjects|publisher|'" />
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="meta">
<xsl:copy>
<xsl:apply-templates select="*">
<xsl:sort select="substring-before($elementOrder, concat('|',name(),'|'))" />
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
所以您只想将该节点向上移动,并且每个元始终有一个版本? 在这种情况下,请尝试以下方法:
<xsl:template match="@*|node()[local-name() != 'version']">
<xsl:choose>
<xsl:when test="local-name() = 'docdate'">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<xsl:copy>
<xsl:value-of select="following::version"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我不完全确定它是否会复制属性。所以你只想将该节点向上移动,并且每个元总是有一个版本? 在这种情况下,请尝试以下方法:
<xsl:template match="@*|node()[local-name() != 'version']">
<xsl:choose>
<xsl:when test="local-name() = 'docdate'">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
<xsl:copy>
<xsl:value-of select="following::version"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我不完全确定它是否会复制属性。这里是另一种方法 从标识模板开始
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
然后,您只需确保版本不会在其原始位置被复制出来
<xsl:template match="version" />
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="version" />
<xsl:template match="docdate">
<xsl:next-match />
<xsl:copy-of select="../version" />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这里是另一种方法
从标识模板开始
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
然后,您只需确保版本不会在其原始位置被复制出来
<xsl:template match="version" />
试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="version" />
<xsl:template match="docdate">
<xsl:next-match />
<xsl:copy-of select="../version" />
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
感谢您的回复Ankit…我们有更多元素出现在“doctitle”上方,因此上述XSLT将无法工作。所有其他元素是否处于同一级别?在尝试此输入97831317 Pocket CT 2017-10-27 Di 3 Sev Ave时。。。。第四,感谢您的回复Ankit…我们有更多的元素出现在“doctitle”上方,因此上述XSLT将不起作用。所有其他元素是否处于同一级别?在尝试输入97831317 Pocket CT 2017-10-27 Di 3 Sev Ave时。。。。第四