简体   繁体   English

如何使用xslt从xml获取标签值

[英]how to get tag values from xml using xslt

<block3>
    <tag>
        <name>113</name>
        <value>Nfeb</value>
    </tag>
    <tag>
        <name>108</name>
        <value>20234254321</value>
    </tag>
</block3>

Here in above xml we have two tags in block3 . 在上面的xml中,我们在block3有两个标签。 I don't want 108 tag, so I need to prepare a xslt in that I have to call only 113. How can i do that? 我不需要108标签,因此我需要准备一个xslt,因为我只需要调用113。我该怎么做? Can anyone please help me! 谁能帮帮我吗!

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>

    <xsl:template match="/*">
        <xsl:apply-templates select="tag[not(name = '108')]"/>
    </xsl:template>

    <xsl:template match="tag">
        <xsl:value-of select="
            concat(name, '+', value)
        "/>
    </xsl:template>
</xsl:stylesheet>

Result against your sample will be 113+Nfeb . 针对您的样本的结果将是113+Nfeb

A personally hate for-each , but for clarity. 个人讨厌for-each ,但是为了清楚起见。

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:template match="/">
        <xsl:for-each select="block3/tag[not(name = '108')]">
            <xsl:value-of select="concat(name, '+', value)"/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

I would create two templates. 我将创建两个模板。 One matching all <tag> elements which does what you want, this will hit case 113. And then another one overriding it, matching the specific cases which you want to skip. 一个匹配所有要执行所需操作的<tag>元素,这将遇到大小写113。然后另一个覆盖它,匹配要跳过的特定大小写。

<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="tag">.....do stuff....</xsl:template>
  <xsl:template match="tag[name = '108']" />

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM