我需要跳过第一个Worker节点,然后使用XSLT3.0复制其余的节点
我的源XML是
<?xml version="1.0" encoding="UTF-8"?>
<Workers>
<Worker>
<Employee>Emp</Employee>
<ID>Identifier</ID>
</Worker>
<Worker>
<Employee>12344</Employee>
<ID>1245599</ID>
</Worker>
<Worker>
<Employee>25644</Employee>
<ID>7823565</ID>
</Worker>
</Workers>所期望的输出是
<?xml version="1.0" encoding="utf-8"?>
<Workers>
<Worker>
<Employee>12344</Employee>
<ID>1245599</ID>
</Worker>
<Worker>
<Employee>25644</Employee>
<ID>7823565</ID>
</Worker>
</Workers>我拥有的XSLT是
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<xsl:mode on-no-match="shallow-copy"/>
<xsl:template match="Worker[position() = 1]"/>
</xsl:stylesheet>上面的XSLT产生了我期望的输出,但我想看看是否有更好的方法不使用postion()跳过第一个节点,因为我不确定当前代码处理大型文件的效率(大约为800 MB)
我不得不使用以下方法从我的结果XML中删除空格
<xsl:strip-space elements="*"/>有谁能检查我的代码,并提供任何建议,以即兴我的代码吗?
===============
有了Michael的建议,我的代码如下所示
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" indent="yes"/>
<!-- <xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template> -->
<!-- Removed above Identity Transformation -->
<xsl:mode streamable="yes" on-no-match="shallow-copy"/>
<xsl:template match="Workers">
<xsl:copy>
<xsl:apply-templates select="tail(Worker)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>发布于 2022-05-20 20:32:53
我会写的
<xsl:template match="Worker[1]"/>为了可读性,但都是一样的。
与位置谓词匹配模式可能会执行得很糟糕,所以您应该谨慎行事,但像这样的简单模式应该可以。实际上,主要的不利后果可能是Saxon将在TinyTree中分配前面的同级指针,以便计算节点的同级位置。
Saxon有效地将其实现为
<xsl:template match="Worker[not(preceding-sibling::Worker)]"/>你可能更喜欢这样写。然而,这两种形式都是可流的。
为了使其流化,您可以通过不选择不需要的节点来删除它们:
<xsl:template match="Workers">
<xsl:copy>
<xsl:apply-templates select="tail(Worker)"/>
</xsl:copy>
</xsl:template>在非流的情况下,这也可能要快一些;而且它节省内存,因为不需要前面的同级指针。
https://stackoverflow.com/questions/72320969
复制相似问题