Xml 删除与特定节点关联的所有子元素,并替换为单个子元素
我是XSLT新手,但我为工作做了很多XML修改,所以我正在尝试学习。我输入了以下格式的XML(上面和下面都有XML):Xml 删除与特定节点关联的所有子元素,并替换为单个子元素,xml,xslt,xslt-2.0,Xml,Xslt,Xslt 2.0,我是XSLT新手,但我为工作做了很多XML修改,所以我正在尝试学习。我输入了以下格式的XML(上面和下面都有XML): 可以存在数量不限的子元素,其值为0-1之间的任意值 ... 我需要以下格式的父/子节点输出XML: <Parent Value="x"> <Child Value="0.3"/> </Parent> 其中只有一个子元素,且其值恒定为0.3 我的当前XSLT作品类型: <?xml version="1.0" encodi
可以存在数量不限的子元素,其值为0-1之间的任意值
...
我需要以下格式的父/子节点输出XML:
<Parent Value="x">
<Child Value="0.3"/>
</Parent>
其中只有一个子元素,且其值恒定为0.3
我的当前XSLT作品类型:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Parent">
<Parent>
<xsl:apply-templates select="@*|*"/>
<Child Value="0.3"/>
</Parent>
</xsl:template>
<xsl:template match="Child[@Value!=0.3]"/>
我试图在所有文件中创建所需的子元素(以防它不存在)
然后仅当其值为0.3时输出子元素。然而,这有时会导致子节点的重复,我觉得我可能没有以正确的方式实现这一点。此外,xmlns:xs头显示在父元素之后,而不是输出文档的顶部。任何指导都将不胜感激
电流输出示例:
<Parent Value="x" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<Child Value="0.30" />
<Child Value="0.3" />
</Parent>
基于身份的转换走上了正确的道路,但您可能需要在父项的模板中创建单个子项:
只有两件事需要修改: 1在此处使用单引号:
<xsl:template match="Child[@Value!='0.3']"/>
XML输入
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Parent">
<Parent>
<xsl:apply-templates select="@*|*"/>
<xsl:if test="not(Child[@Value = '0.3'])">
<Child Value="0.3"/>
</xsl:if>
</Parent>
</xsl:template>
<xsl:template match="Child[@Value!='0.3']"/>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Parent Value="x">
<Child Value="0.30"/>
<Child Value="1"/>
<Child Value="0.4"/>
<Child Value="0.3">The right one</Child>
</Parent>
<Parent xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
Value="x">
<Child Value="0.3">The right one</Child>
</Parent>
我不能告诉你这有多大帮助!这完全有道理。非常感谢你!
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
<xsl:output method="xml" version="1.0" encoding="UTF-8" omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Parent">
<Parent>
<xsl:apply-templates select="@*|*"/>
<xsl:if test="not(Child[@Value = '0.3'])">
<Child Value="0.3"/>
</xsl:if>
</Parent>
</xsl:template>
<xsl:template match="Child[@Value!='0.3']"/>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8"?>
<Parent Value="x">
<Child Value="0.30"/>
<Child Value="1"/>
<Child Value="0.4"/>
<Child Value="0.3">The right one</Child>
</Parent>
<Parent xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes"
Value="x">
<Child Value="0.3">The right one</Child>
</Parent>
<Parent Value="x">
<Child Value="0.3">The right one</Child>
</Parent>