Xml 将多个属性值更改为新值
这个问题是从使用答案来解决我的情况演变而来的,但发现它不起作用 以下是我的xml:Xml 将多个属性值更改为新值,xml,xslt,Xml,Xslt,这个问题是从使用答案来解决我的情况演变而来的,但发现它不起作用 以下是我的xml: <root> <Element1 id="VEH1"> <Element2 /> </Element1> <Element1 id="VEH2"> <Element2 /> </Element1> </root> 以下是我的xsl转换: <xsl:stylesheet version="1.
<root>
<Element1 id="VEH1">
<Element2 />
</Element1>
<Element1 id="VEH2">
<Element2 />
</Element1>
</root>
以下是我的xsl转换:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="@id">
<xsl:attribute name="id">
<xsl:choose>
<xsl:when test=". = VEH1">
<xsl:text>VEH01</xsl:text>
</xsl:when>
<xsl:when test=". = VEH2">
<xsl:text>VEH02</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
</xsl:template>
</xsl:stylesheet>
VEH01
VEH02
我希望输出看起来像:
<?xml version="1.0" encoding="UTF-8"?><root>
<Element1 id="VEH01">
<Element2/>
</Element1>
<Element1 id="VEH02">
<Element2/>
</Element1>
</root>
但是属性值保持不变,至少我不知道为什么。谢谢
<xsl:when test=". = VEH1">
<xsl:text>VEH01</xsl:text>
</xsl:when>
<xsl:when test=". = VEH2">
<xsl:text>VEH02</xsl:text>
</xsl:when>
VEH01
VEH02
文本'VEH1'
和'VEH2'
VEH01
VEH02
在文本
'VEH1'
和'VEH2'
周围缺少单引号。这个简单、简短而复杂的转换信息没有使用任何明确的XSLT条件指令:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Element1[starts-with(@id,'VEH')]">
<Element1 id="VEH0{substring-after(@id,'VEH')}">
<xsl:apply-templates select="@*[not(name()='id')]|node()"/>
</Element1>
</xsl:template>
</xsl:stylesheet>
<root>
<Element1 id="VEH1">
<Element2 />
</Element1>
<Element1 id="VEH2">
<Element2 />
</Element1>
</root>
<root>
<Element1 id="VEH01">
<Element2/>
</Element1>
<Element1 id="VEH02">
<Element2/>
</Element1>
</root>
将此转换应用于此XML文档时:
<root>
<Element1 id="VEH1">
<Element2 />
</Element1>
<Element1 id="VEH2">
<Element2 />
</Element1>
<Element1 id="VEH12">
<Element3 />
</Element1>
</root>
生成所需的、正确的(关于新添加的需求)结果:
<root>
<Element1 id="VEH01">
<Element2/>
</Element1>
<Element1 id="VEH02">
<Element2/>
</Element1>
<Element1 id="VEH12">
<Element3/>
</Element1>
</root>
这个简单、简短而复杂的转换信息不使用任何明确的XSLT条件指令:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Element1[starts-with(@id,'VEH')]">
<Element1 id="VEH0{substring-after(@id,'VEH')}">
<xsl:apply-templates select="@*[not(name()='id')]|node()"/>
</Element1>
</xsl:template>
</xsl:stylesheet>
<root>
<Element1 id="VEH1">
<Element2 />
</Element1>
<Element1 id="VEH2">
<Element2 />
</Element1>
</root>
<root>
<Element1 id="VEH01">
<Element2/>
</Element1>
<Element1 id="VEH02">
<Element2/>
</Element1>
</root>
将此转换应用于此XML文档时:
<root>
<Element1 id="VEH1">
<Element2 />
</Element1>
<Element1 id="VEH2">
<Element2 />
</Element1>
<Element1 id="VEH12">
<Element3 />
</Element1>
</root>
生成所需的、正确的(关于新添加的需求)结果:
<root>
<Element1 id="VEH01">
<Element2/>
</Element1>
<Element1 id="VEH02">
<Element2/>
</Element1>
<Element1 id="VEH12">
<Element3/>
</Element1>
</root>
有一种相当简单的方法可以做到这一点-只需使用此模板:
<xsl:template match="@id[starts-with(.,'VEH')]">
<xsl:attribute name="id">
<xsl:value-of select="concat('VEH',format-number(substring(.,4),'00'))" />
</xsl:attribute>
</xsl:template>
如果需要,您可以进一步限制匹配上的谓词-添加
和(floor(substring(,4))=substring(,4))
,前提是VEH
后面有数字以外的任何内容。有一种相当简单的方法-只需使用此模板:
<xsl:template match="@id[starts-with(.,'VEH')]">
<xsl:attribute name="id">
<xsl:value-of select="concat('VEH',format-number(substring(.,4),'00'))" />
</xsl:attribute>
</xsl:template>
如果需要,您可以进一步限制匹配上的谓词-添加
和(floor(substring(,4))=substring(,4))
如果VEH
后面可能有数字以外的任何内容。如果OP的@id
s的值大于VEH9
,那么这将输出类似VEH010
的值,VEH011
等-在两位数字前加上一个额外的0
。这可能不是理想的输出。@wst,当您说“可能”时,这意味着您不确定。当你不确定的时候,最好不要投反对票!根据OP的两位数编号方案,他很可能打算使用带有两位数的@id
s。你的答案对两位数有负面影响。如果这件事解决了,我会投赞成票。@wst,你怀疑这件事做不到吗?在我更新了解决方案之后,下一个“需求”是什么?这是适合作为新的、单独的问题提出的。没有要求。您的第一个解决方案只是针对一个非常明显和可能的用例。当然,我认为这是可以做到的,你做到了,所以每个人都赢了!如果OP的@id
s的值大于VEH9
,则这将输出VEH010
、VEH011
等值-在2位数字前加上一个0
。这可能不是理想的输出。@wst,当您说“可能”时,这意味着您不确定。当你不确定的时候,最好不要投反对票!根据OP的两位数编号方案,他很可能打算使用带有两位数的@id
s。你的答案对两位数有负面影响。如果这件事解决了,我会投赞成票。@wst,你怀疑这件事做不到吗?在我更新了解决方案之后,下一个“需求”是什么?这是适合作为新的、单独的问题提出的。没有要求。您的第一个解决方案只是针对一个非常明显和可能的用例。当然,我认为这是可以做到的,你做到了,所以每个人都赢了!这些都是很好的答案,很难选出最好的。我真的很喜欢最后一个解决方案的简单性,所以我选择了它。真诚的谢意再加上这些都是很好的答案,很难选出最好的。我真的很喜欢最后一个解决方案的简单性,所以我选择了它。再次衷心感谢