我有以下XML:
<-一个或多个->
<Supplement>
<ID>321
<SupplementType>
<Glass>31DK</Glass>
</SupplementType>
</ID>
</Supplement>
当我使用当前元素的选择值时,它得到32131DK(“ ID”和“ Glass”元素的值)
在我的输出中,我只想在“ ID”元素之后获取数字值(321)
xml输入无法更改,因为它是制造商的原样。
我的XSLT:
<xsl:element name="ProfileSpecification">
<xsl:for-each select="Supplement/ID">
<xsl:value-of select="."/>
</xsl:for-each> </element>
我得到的输出:
<ProfileSpecification>32131DK</ProfileSpecification>
我想要的输出:
<ProfileSpecification>321</ProfileSpecification>
您的方法无效,因为
<xsl:value-of select="."/>
返回上下文元素的字符串值。字符串值是所有后代文本节点(不仅是直接子代)的串联。
您不应该简单地匹配/
(我想您可以匹配)并将所有代码放入此单个模板中。而是,为重要元素定义单独的模板匹配,并用于apply-templates
在文档中移动。
for-each
没有正当理由,请勿使用。同样适用xsl:element
-如果元素名称是静态已知的,则不要使用它,而应使用文字结果元素。
XML输入
假设格式正确的(单个根元素)和代表的(多个Supplement
元素,如您在问题文本中所述)输入XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Supplement>
<ID>321
<SupplementType>
<Glass>31DK</Glass>
</SupplementType>
</ID>
</Supplement>
<Supplement>
<ID>425
<SupplementType>
<Glass>444d</Glass>
</SupplementType>
</ID>
</Supplement>
</root>
XSLT样式表
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes" />
<xsl:template match="/root">
<xsl:copy>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
<xsl:template match="Supplement">
<ProfileSpecification>
<xsl:value-of select="normalize-space(ID/text()[1])"/>
</ProfileSpecification>
</xsl:template>
</xsl:transform>
XML输出
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ProfileSpecification>321</ProfileSpecification>
<ProfileSpecification>425</ProfileSpecification>
</root>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句