如何在xslt中获取属性名称?

时间:2017-09-20 09:14:24

标签: xml xslt xpath xslt-1.0 xslt-2.0

你能告诉我如何在xslt中获取属性值吗?我有一个xml。我需要解析这个xml。我想得到 blockquote 的类名。我不知道是blockquote的位置,但它出现在文章标签

我试过这个

XML

<amp_articleshow >

<article >

    <arttextnew>
        <page>
            <div class="section1">
                <div class="Normal">
                    Dipika Kakar and
                    <keyword keytype="UnKnown" smid="0" usetype="2" keywordseo="Shoaib-Ibrahim">Shoaib Ibrahim</keyword>
                    are currently seen showing off their dancing skills on
                    <keyword keytype="UnKnown" smid="0" usetype="2" keywordseo="Nach-Baliye-8">Nach Baliye 8</keyword>
                    and are one of the judges' favourite jodis.
                    <br/>
                    <br/>
                    Shoaib and Dipika are working really hard for their performances and have been giving their 100% to it.
                    <br/>
                    <br/>
                    Shoaib, who had earlier played the role of Prem on Sasural Simra Ka, crossed 100k followers on Instagram and the actor has been overwhelmed.
                    <br/>
                    <br/>
                    <strong>READ ALSO:</strong>
                    <br/>
                    <a class="inline-read" href="http://timesofindia.indiatimes.com/tv/news/hindi/nach-baliye-8-dipika-kakar-and-shoaib-ibrahims-hum-dil-de-chuke-moment/articleshow/58611440.cms">
                        <strong>
                            Nach Baliye 8: Dipika Kakar and Shoaib Ibrahim's 'Hum Dil De Chuke' moment
                        </strong>
                    </a>
                    <br/>
                    <br/>
                    He posted a video thanking his fans. He captioned it saying "#100K Followers! Can't believe I crossed this milestone. Thank you so much guys for all this love. Couldn't have done without you!#LoveYouGuys #SoHappy"
                    <br/>
                    <br/>
                    <blockquote class="instagram-media" data-instgrm-captioned="" data-instgrm-version="7" style=" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);">
                        <div style="padding:8px;">
                            <div style=" background:#F8F8F8; line-height:0; margin-top:40px; padding:50.0% 0; text-align:center; width:100%;">
                                <div style=" background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAMAAAApWqozAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAMUExURczMzPf399fX1+bm5mzY9AMAAADiSURBVDjLvZXbEsMgCES5/P8/t9FuRVCRmU73JWlzosgSIIZURCjo/ad+EQJJB4Hv8BFt+IDpQoCx1wjOSBFhh2XssxEIYn3ulI/6MNReE07UIWJEv8UEOWDS88LY97kqyTliJKKtuYBbruAyVh5wOHiXmpi5we58Ek028czwyuQdLKPG1Bkb4NnM+VeAnfHqn1k4+GPT6uGQcvu2h2OVuIf/gWUFyy8OWEpdyZSa3aVCqpVoVvzZZ2VTnn2wU8qzVjDDetO90GSy9mVLqtgYSy231MxrY6I2gGqjrTY0L8fxCxfCBbhWrsYYAAAAAElFTkSuQmCC); display:block; height:44px; margin:0 auto -44px; position:relative; top:-22px; width:44px;"></div>
                            </div>
                            <p style=" margin:8px 0 0 0; padding:0 4px;">
                                <a href="https://www.instagram.com/p/BT_zxpgBIgO/" style=" color:#000; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none; word-wrap:break-word;" target="_blank">
                                    #100K Followers! Can't believe I crossed this milestone. Thank you so much guys for all this love. Couldn't have done without you!#LoveYouGuys #SoHappy
                                </a>
                            </p>
                            <p style=" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;">A post shared by shoaib Ibrahim (@shoaib2087) on</p>
                            <time datetime="2017-05-12T15:17:19+00:00" style=" font-family:Arial,sans-serif; font-size:14px; line-height:17px;">May 12, 2017 at 8:17am PDT</time>
                        </div>
                    </blockquote>
                    <br/>
                    <br/>
                    Shoaib is currently seen in 'Koi Laut Ke Aaya Hai' opposite Surbhi Jyotii, while Dipika was last seen on Colors' 'Sasural Simar Ka'.
                    <br/>
                    <br/>
                    This is the first time ever that they will appear together on a reality show. The duo have been dating each other for quite some time now and plan to tie the knot in January next year.
                </div>
            </div>
        </page>
    </arttextnew>


</article>

</amp_articleshow>

XSLT

<xsl:template match="amp_articleshow">
     <xsl:value-of select="//./article/descendant::blockquote[@class]"/>
    </xsl:template>

预期输出 :: 的的Instagram媒体

3 个答案:

答案 0 :(得分:1)

尝试:

<xsl:value-of select="//blockquote/@class"/>

请注意,在XSLT 1.0中,这将返回文档中第一个块引用的类。

答案 1 :(得分:1)

这应该可以解决问题:

<xsl:template match="amp_articleshow">
   <xsl:value-of select="//./article/descendant::blockquote/@class"/>
</xsl:template>

这实际上是关于XPath的问题。

在XPath /foo[@bar]中选择一个具有属性foo的元素bar,但它不会为您提供bar的值,这是您所需要的。

要获取属性的值,只需将查询更改为/foo/@bar,这确实会为您提供foo的值。

这是一个很好的链接,显示了节点selection works

的方式

答案 2 :(得分:0)

Predicates []将检查具有class属性的bolckquote意味着它将返回具有class属性的blockquote值。

blockquote[@class]

如果你想获得@class值,你应该这样:

blockquote/@class