如何在XSLT中使用for-each提取特定元素?

时间:2019-01-10 10:46:30

标签: xml xslt

在我的班级考试中,我们了解了与XML,DTD,XSD,XSL有关的知识,我们被要求创建一个XSLT,该XSLT将从XML文档中仅提取Saint-Malo镇内的公园,然后将结果转换为XML文档。我的结果几乎正确,但不太正确。改变成绩现在为时已晚,但是我真的很好奇如何做到这一点。

这是原始XML的内容:

<entry>
        <id>https://www2.mgcloud.fr/dataserver/tourismebretagne/data/jardins35fr('PCUBRE0351000100')</id>
        <title type="text">'PCUBRE0351000100'</title>
        <updated>2015-05-25T06:18:05Z</updated>
        <author>
            <name>https://www2.mgcloud.fr/dataserver/tourismebretagne/catalog/agents/17692</name>
        </author>
        <link rel="edit" title="jardins35frType" href="jardins35fr('PCUBRE0351000100')"/>
        <category term="fr.mgdis.odata.data.jardins35frType" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
        <content type="parc">
            <properties>
                <num_id>PCUBRE0351000100</num_id>
                <titre>Le Domaine de la Briantais</titre>
                <theme>Parc</theme>
                <telephone>02 99 81 83 42</telephone>
                <fax>02 99 82 51 56</fax>
                <web>www.ville-saint-malo.fr</web>
                <mail>deep@saint-malo.fr</mail>
                <adresse>Rue Maurice Nogues</adresse>
                <code_postal>35400</code_postal>
                <insee>35288</insee>
                <commune>Saint-Malo</commune>
                <latitude>48.6194348</latitude>
                <longitude>-2.0147895</longitude>
                <pmr>Oui</pmr>
                <descriptif>Le Château a été construit à partir de 1864, en remplacement d'un vieux manoir du XVIIème siècle qui appartenait à  de notables armateurs malouins. La famille La Chambre en devient propriétaire en 1888. Il est entouré d'un parc de 27 hectares surplombant la Tour Solidor, et la vallée de la Rance. Parc ouvert de 9h à 19h en juillet et août. Ouvert de 14h à 17h ou 18h en semaine hors saison, et de 9h à 17h ou 19h les week-ends et jours fériés toujours en basse saison. Parc avec vues sur mer. Renseignements : ville de Saint Malo - Direction de l'Environnement et de l'Espace Public - Division des Espaces verts. 02 99 81 83 42 pour tout renseignement complémentaire.</descriptif>
                <photos>http://cdt29.media.tourinsoft.com/upload/4CECC4CF-0833-47A8-9B65-762C434CC0D0/Dinard-vu-de-la-Briantais.jpg</photos>
            </properties>
        </content>
    </entry>

这是我写的XSLT:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="4.0" indent="yes"/>
    <xsl:template match="/">
        <parcs_et_jardins_de_Saint_Malo>
            <xsl:for-each select="//properties[commune='Saint-Malo']">
                <titre><xsl:value-of select="//titre"/></titre>
            </xsl:for-each>
        </parcs_et_jardins_de_Saint_Malo>
    </xsl:template>
</xsl:stylesheet>

这就是生成的XML文档的内容:

<?xml version="4.0"?>
<parcs_et_jardins_de_Saint_Malo>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
  <titre>Parc Oberthur</titre>
</parcs_et_jardins_de_Saint_Malo>

显然这对我不起作用,但是我不明白为什么。问题在于“公社”元素可以包含其他城镇,而我们不想将这些结果发挥作用-只是那些拥有圣马洛的城市。我的XSLT一次又一次地生成了同一座城镇-尽管元素的数量是正确的数量(“ titre”元素的数量与提到的Saint-Malo一样多)。

我非常感谢您的帮助,并希望这不会造成混淆。

1 个答案:

答案 0 :(得分:1)

只需替换一下。...

 <titre><xsl:value-of select="//titre"/></titre>

有了这个......

<titre><xsl:value-of select="titre"/></titre>

通过在表达式的开头执行//,您将从文档节点开始在整个XML中搜索titre。因此,它将选择文档中的所有titre节点。在XSLT 1.0中,xsl:value-of仅在选择了多个节点的情况下才返回第一个节点的值。

通过执行<xsl:value-of select="titre"/>,您仅选择titre元素,它们是您用properties选择的当前xsl:for-each的子元素。

请注意,如果仅是要获得titre个节点,则不要这样做……

<xsl:for-each select="//properties[commune='Saint-Malo']">
  <titre><xsl:value-of select="titre"/></titre>
</xsl:for-each>

您可以执行此操作,以复制所有匹配的节点

<xsl:copy-of select="//properties[commune='Saint-Malo']/titre" />
相关问题