Jump to content

XML/XSl Help


CCBtx

This topic is 3625 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Can someone help me with the following:

 

I want to extract <loc>, <lastmod> and <priority> from this xml:

 

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url><loc>http://www.myurl.com/</loc><lastmod>2014-04-03T15:51:51-05:00</lastmod><priority>1.0</priority></url>
<url><loc>http://www.myurl.com/for-sale/TX/100/79109/173-Main-094451242</loc><lastmod>2014-04-07T03:09:00-05:00</lastmod><priority>0.7</priority></url>
<url><loc>http://www.myurl.com/for-sale/TX/100/79109/146-Main-098844556</loc><lastmod>2014-04-07T02:48:00-05:00</lastmod><priority>0.7</priority></url>
<url><loc>http://www.myurl.com/for-sale/TX/100/79109/353-Main-188745469</loc><lastmod>2014-04-07T02:31:00-05:00</lastmod><priority>0.7</priority></url>
</urlset>

 

I am using this stylesheet (which is not working):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:template match="/">
<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>

<PRODUCT BUILD="" NAME="FileMaker" VERSION="ProAdvanced 12.0v4"/>
<DATABASE DATEFORMAT="" LAYOUT="" NAME="" RECORDS="" TIMEFORMAT=""/>

<METADATA>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="loc" TYPE="TEXT" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="lastmod" TYPE="TEXT" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="priority" TYPE="TEXT" />
</METADATA>

<RESULTSET FOUND="">
<xsl:for-each select="//url">
<ROW RECORDID="" MODID="" >
<COL><DATA><xsl:value-of select="loc"/></DATA></COL>
<COL><DATA><xsl:value-of select="lastmod"/></DATA></COL>
<COL><DATA><xsl:value-of select="priority"/></DATA></COL>
</ROW>
</xsl:for-each>

</RESULTSET>
</FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>

 

Thanks very much.

Link to comment
Share on other sites

First thing: please use the "code" formatting when posting XML code, to make it readable:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
   <url>
      <loc>http://www.myurl.com/</loc>
      <lastmod>2014-04-03T15:51:51-05:00</lastmod>
      <priority>1.0</priority>
   </url>
   <url>
      <loc>http://www.myurl.com/for-sale/TX/100/79109/173-Main-094451242</loc>
      <lastmod>2014-04-07T03:09:00-05:00</lastmod>
      <priority>0.7</priority>
   </url>
   <url>
      <loc>http://www.myurl.com/for-sale/TX/100/79109/146-Main-098844556</loc>
      <lastmod>2014-04-07T02:48:00-05:00</lastmod>
      <priority>0.7</priority>
   </url>
   <url>
      <loc>http://www.myurl.com/for-sale/TX/100/79109/353-Main-188745469</loc>
      <lastmod>2014-04-07T02:31:00-05:00</lastmod>
      <priority>0.7</priority>
   </url>
</urlset>

Now, the reason why your stylesheet cannot work is that the source XML has a namespace of its own, declared in the root element:

xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"

All the elements descendant of the root element inherit this namespace. As a result, all your XPath expressions:

select="//url" 
select="loc" 
etc.

select nothing.

 

 

You need to declare the namespace in your stylesheet, assign it a prefix, and use that prefix when calling the elements in your XML:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:smp="http://www.sitemaps.org/schemas/sitemap/0.9">

<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">

<FMPXMLRESULT xmlns="http://www.filemaker.com/fmpxmlresult">
<ERRORCODE>0</ERRORCODE>

<PRODUCT BUILD="" NAME="FileMaker" VERSION="ProAdvanced 12.0v4"/>
<DATABASE DATEFORMAT="" LAYOUT="" NAME="" RECORDS="" TIMEFORMAT=""/>

<METADATA>
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="loc" TYPE="TEXT" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="lastmod" TYPE="TEXT" />
<FIELD EMPTYOK="YES" MAXREPEAT="1" NAME="priority" TYPE="TEXT" />
</METADATA>

<RESULTSET FOUND="">
<xsl:for-each select="//smp:url">
<ROW RECORDID="" MODID="" >
<COL><DATA><xsl:value-of select="smp:loc"/></DATA></COL>
<COL><DATA><xsl:value-of select="smp:lastmod"/></DATA></COL>
<COL><DATA><xsl:value-of select="smp:priority"/></DATA></COL>
</ROW>
</xsl:for-each>

</RESULTSET>
</FMPXMLRESULT>
</xsl:template>
</xsl:stylesheet>

Note also that performance-wise, it pays to be explicit in XSLT. It's better to say:

<xsl:for-each select="smp:urlset/smp:url">

than just:

<xsl:for-each select="//smp:url">
Link to comment
Share on other sites

  • 3 weeks later...

Glad it worked! I'd change comment's excellent code like this (two snippets):

1. if you use/create an xmlns for the source, then remember to exclude it from the output.

<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:smp="http://www.sitemaps.org/schemas/sitemap/0.9"
   exclude-result-prefixes="smp">

2. if you have 'relative' path for the child elements, the parent xmlns is only required.

<RESULTSET FOUND="">
   <xsl:for-each select="//smp:url">
      <ROW RECORDID="" MODID="" >
         <COL><DATA><xsl:value-of select="./loc"/></DATA></COL>
         <COL><DATA><xsl:value-of select="./lastmod"/></DATA></COL>
         <COL><DATA><xsl:value-of select="./priority"/></DATA></COL>
      </ROW>
   </xsl:for-each>

:cool:

Link to comment
Share on other sites

1. if you use/create an xmlns for the source, then remember to exclude it from the output.

 

2. if you have 'relative' path for the child elements, the parent xmlns is only required.

 

Re #1, it makes no practical difference, but perhaps as a point of good practice...

 

Alas, you could not be more wrong re #2. So who are you and what have you done to the real Beverly Voth, the author of FileMaker Pro 6 Developer's Guide to XML/XSL - who BTW would also not write "./loc" when "loc" is quite sufficient?

Link to comment
Share on other sites

This topic is 3625 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.