Home > Net >  XSLT parsing works wrong
XSLT parsing works wrong

Time:10-27

I have RSS feed and my task is to parse that feed to some other XML schema. I am using XSLT parser. Here my rss feed and parser:

<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
xmlns:georss="http://www.georss.org/georss"
xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
>
    <channel>
        <title>Vita Brevis</title>
        <atom:link href="https://vitabrevis.americanancestors.org/feed/" rel="self" type="application/rss xml"/>
        <link>https://vitabrevis.americanancestors.org</link>
        <description>A resource for family history from AmericanAncestors.org</description>
        <lastBuildDate>Mon, 25 Oct 2021 16:23:50  0000</lastBuildDate>
        <language>en-US</language>
        <sy:updatePeriod>
            hourly
        </sy:updatePeriod>
        <sy:updateFrequency>
            1
        </sy:updateFrequency>
        <generator>https://wordpress.org/?v=5.8.1</generator>
        <site xmlns="com-wordpress:feed-additions:1">62574325</site>
        <item>
            <title>ICYMI: Of Plimoth Plantation</title>
        </item>
    </channel>
</rss>

and XSLT

    <?xml version="1.0" encoding="UTF-8"?>
      <xsl:stylesheet
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        version="2.0"
        xpath-default-namespace="http://purl.org/rss/1.0/modules/content/"
        exclude-result-prefixes="content">

    <xsl:output method="xml" doctype-public="XSLT-compat" omit-xml-declaration="yes" 
  encoding="UTF-8" indent="yes"/>


    <xsl:template match="/">
        <xsl:element name="update">
            <xsl:apply-templates select="rss/channel/item"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="rss/channel/item">
        <xsl:element name="add">
            <xsl:element name="doc">                   
                <xsl:element name="field">
                    <xsl:attribute name="name">tcngrams_title</xsl:attribute>
                    <xsl:value-of select="title"/>
                </xsl:element>           
            </xsl:element>
        </xsl:element>
    </xsl:template>
    </xsl:stylesheet>

The desired result should be that

<update>
    <add>
      <doc>
        <field name="tcngrams_title">ICYMI: Of Plimoth Plantation</field>
      </doc>
    </add>
</update>

But I am getting only <update />.

I am using the http://xsltransform.net/ transformer. What I am doing wrong here?

CodePudding user response:

Because you have xpath-default-namespace="http://purl.org/rss/1.0/modules/content/".

Your RSS elements are not bound to any namespace, they are in the "no namespace".

But since you added that default XPath namespace attribute, it is looking for element bound to that namespace and finding none.

  • Related