Xml XSLT仅在不插入元素时插入元素';不存在
我有一个源文件:Xml XSLT仅在不插入元素时插入元素';不存在,xml,xslt,Xml,Xslt,我有一个源文件: <?xml version="1.0"?> <source> <ItemNotSubstituted/> <ItemToBeSubstituted Id='MatchId' /> </source> <?xml version="1.0"?> <source> <ItemNotSubstituted/> <ItemToBeSubstituted Id="Mat
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id='MatchId' />
</source>
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id="MatchId">
<Element3 Value="baz"/>
<Element1/>
<Element2 Value="foo"/>
</ItemToBeSubstituted>
</source>
我得到这个输出:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id="MatchId">
<Element3 Value="baz"/>
<Element1/>
<Element2 Value="foo"/>
<Element3 Value="bar"/>
</ItemToBeSubstituted>
</source>
我只想替换源文档中不存在的样式表中的元素。这是我在将样式表应用到第二个文档后寻找的输出,其中只有源文档中的
元素:
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id='MatchId' />
</source>
<?xml version="1.0"?>
<source>
<ItemNotSubstituted/>
<ItemToBeSubstituted Id="MatchId">
<Element3 Value="baz"/>
<Element1/>
<Element2 Value="foo"/>
</ItemToBeSubstituted>
</source>
使用XSL执行此操作的最佳方法是什么?样式表可能包含许多要替换的元素。因此,我不想使用一种需要在每个元素周围使用
的方法。有没有比使用一个样式表插入内容,然后使用第二个样式表删除重复元素更好的方法 我会用这样的东西:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" method="xml" omit-xml-declaration="no" version="1.0"/>
<xsl:preserve-space elements="//*"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ItemToBeSubstituted[@Id = 'MatchId']">
<xsl:variable name="node" select="." />
<xsl:copy>
<xsl:copy-of select="@*|*"/>
<xsl:for-each select="document('elements.xml')/elements/*">
<xsl:if test="not($node/*[name() = name(current())])">
<xsl:copy-of select="." />
</xsl:if>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
其中elements.xml是存储默认情况下要添加的元素的文件
<?xml version="1.0" encoding="utf-8" ?>
<elements>
<Element1/>
<Element2 Value="foo"/>
<Element3 Value="bar"/>
</elements>
使用
我们迭代默认元素,检查当前节点中是否有以该名称命名的元素作为子元素,如果没有,则添加该元素。此XSLT 1.0解决方案实现了您的目标:
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:subst="http://tempuri.org/mysubst"
>
<!-- expand this section to contain all your default elements/values -->
<subst:defaults>
<subst:element name="ItemToBeSubstituted" id="MatchId">
<subst:Element1/>
<subst:Element2 Value="foo"/>
<subst:Element3 Value="bar"/>
</subst:element>
</subst:defaults>
<!-- this makes the above available as a variable -->
<xsl:variable name="defaults" select="document('')/*/subst:defaults" />
<!-- identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- expand the match expression to contain all elements
names that need default values -->
<xsl:template match="ItemToBeSubstituted">
<xsl:copy>
<xsl:copy-of select="@*|*"/>
<xsl:call-template name="create-defaults" />
</xsl:copy>
</xsl:template>
<!-- this does all the heavy lifting -->
<xsl:template name="create-defaults">
<xsl:variable name="this" select="." />
<xsl:for-each select="
$defaults/subst:element[@name = name($this) and @id = $this/@Id]/*
">
<xsl:if test="not($this/*[name() = local-name(current())])">
<xsl:apply-templates select="." />
</xsl:if>
</xsl:for-each>
</xsl:template>
<!-- create the default nodes without namespaces -->
<xsl:template match="subst:*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="subst:*|@*" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
使用单独的名称空间(“subst”)使您能够在样式表中保留默认值。这是不是一件好事取决于你,至少你不必有两个文件
如果您希望将样式表与默认值分离,请将它们放在一个额外的文件中,并改用此行
<xsl:variable name="defaults" select="document('defaults.xml')/subst:defaults" />
一旦这样做,您就可以放弃所有额外的名称空间处理,最终或多或少会得到Josh Davis提出的解决方案。+1这是解决问题的一个良好开端。它可以使用一个更一般化的事实,即可能有更多的元素被替换,而不是
itemtobessubstituted[@Id='MatchId']
,但这并不太难做到。