英文:
Parse XML with XSLT 1.0
问题
以下是您要的翻译部分:
<?xml version="1.0" encoding="UTF-8"?>
<node>
<left-node key="Network0">
<node key="@FR" value="tom"/>
</left-node>
<node key="Network1">
<node key="PortNumber" left-value="12000" right-value="12001"/>
</node>
<node key="Network2">
<node key="PortNumber" left-value="12000" right-value="12001"/>
<node key="Location">
<node key="Office" left-value="IT" right-value="HR"/>
<right-node key="GO" value="Yellow"/>
</node>
<right-node key="Try"/>
</node>
<right-node key="Network4">
<node key="@GB" value="tim"/>
</right-node>
</node>
您提供的XSLT 1.0代码已经可以成功移除没有带有属性right-value、left-value或value的节点。
英文:
With XSLT 1.0, I would like to remove all nodes tagged node if they have no children with the attribute right-value, left-value or value.
The initial file is :
<?xml version="1.0" encoding="UTF-8"?>
<node>
<left-node key="Network0">
<node key="@FR" value="tom"/>
</left-node>
<node key="Network1">
<node key="PortNumber" left-value="12000" right-value="12001"/>
<node key="Location"/>
</node>
<node key="Network2">
<node key="PortNumber" left-value="12000" right-value="12001"/>
<node key="Location">
<node key="Office" left-value="IT" right-value="HR"/>
<right-node key="GO" value="Yellow"/>
</node>
<right-node key="Try"/>
</node>
<node key="Network3">
<node key="Location">
<node key="Hardware"/>
<node key="Software"/>
</node>
</node>
<right-node key="Network4">
<node key="@GB" value="tim"/>
</right-node>
</node>
With the example, I would like to get
<?xml version="1.0" encoding="UTF-8"?>
<node>
<left-node key="Network0">
<node key="@FR" value="tom"/>
</left-node>
<node key="Network1">
<node key="PortNumber" left-value="12000" right-value="12001"/>
</node>
<node key="Network2">
<node key="PortNumber" left-value="12000" right-value="12001"/>
<node key="Location">
<node key="Office" left-value="IT" right-value="HR"/>
<right-node key="GO" value="Yellow"/>
</node>
<right-node key="Try"/>
</node>
<right-node key="Network4">
<node key="@GB" value="tim"/>
</right-node>
</node>
I am able to get the nodes tagged left-node or right-node but for the nodes tagged node I have problems.
`<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="*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node[not(@value!='' or @right-value!='' or @left-value!='') ]"/>
</xsl:stylesheet>`
答案1
得分: 0
你正在寻找descendant-or-self
轴。因此,请使用以下排除模板:
<xsl:template match="node[not(descendant-or-self::*[@right-value or @left-value or @value])]" />
输出应该符合预期。
英文:
You are looking for the descendant-or-self
axis.
So use this excluding template:
<xsl:template match="node[not(descendant-or-self::*[@right-value or @left-value or @value])]" />
The output should be as desired.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论