gpt4 book ai didi

xslt - XSLT:有效测试“表兄弟”节点是否存在

转载 作者:行者123 更新时间:2023-12-03 16:20:11 27 4
gpt4 key购买 nike

我有以下XML:

<types>
<type>
<name>derived</name>
<superType>base</superType>
<properties>
<property>
<name>B1</name>
</property>
<property>
<name>D1</name>
</property>
</properties>
</type>
<type>
<name>base</name>
<properties>
<property>
<name>B1</name>
</property>
</properties>
</type>
</types>


我想转换为以下输出:

derived
D1

base
B1


请注意,由于节点 /types/type[name='derived']/properties/property[name='B1']在基本类型中以以下形式存在,因此已被跳过: /types/type[name='base']/properties/property[name='B1']

我想出了这个XSLT:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes"/>
<xsl:strip-space elements="*"/>

<!-- Do nothing for base class properties -->
<!-- Wouldn't be necessary if the match criteria could be applied in the select -->
<xsl:template match="property"/>

<xsl:template match="property[not(//type[name=current()/../../superType]/properties/property[name=current()/name])]">
<xsl:text>&#x09;</xsl:text>
<xsl:value-of select="name"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="types/type">
<xsl:value-of select="name"/>
<xsl:text>&#10;</xsl:text>
<xsl:apply-templates select="./properties"/>
<xsl:text>&#10;</xsl:text>
</xsl:template>

</xsl:stylesheet>


这可以工作(使用Notepad ++中的XML Tools插件),但是 not(//type[name=current()/../../superType]/properties/property[name=current()/name]) XPath表达式效率极低:应用于200K行XML文件时,转换过程需要280秒。没有此XPath表达式,转换仅需2秒。

有什么办法可以加快速度吗?

最佳答案

测量速度...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>
<xsl:strip-space elements="*"/>

<xsl:key name="kPropertyByName" match="property" use="name" />

<xsl:template match="property">
<xsl:variable name="supertype" select="../../superType/text()" />
<xsl:if test="($supertype = '') or not ( key('kPropertyByName',name)/../../name[.=$supertype])">
<xsl:value-of select="concat('&#x09;',name,'&#x0A;')" />
</xsl:if>
</xsl:template>

<xsl:template match="type">
<xsl:value-of select="concat(name,'&#x0A;')" />
<xsl:apply-templates select="properties" />
<xsl:text>&#x0A;</xsl:text>
</xsl:template>

</xsl:stylesheet>

关于xslt - XSLT:有效测试“表兄弟”节点是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13389163/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com