XSLT stylesheet that converts XML to XML (RSS 2.0)
this is what i have so far:
< ?xml version="1.0"?>
< xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
< xmlns:g="http://base.google.com/ns/1.0" version="1.0">
< xsl:output method="RSS 2.0"/>
< xsl:template match="Description">
< title>
< /title>
< /xsl:template>
< xsl:template match="Caption">
< description>
< /description>
< /xsl:template>
< xsl:template match="Url>
< link>
< /link>
< /xsl:template>
< xsl:template match="Condition">
< g:condition>
< /g:condition>
< /xsl:template>
< xsl:template match="Picture">
< g:image_link>
< /g:image_link>
< /xsl:template>
< /xsl:stylesheet>
when i try to use this on a smaller file i created with just a couple items on it, i get these debug errors:
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4:
parser
error :
error parsing attribute name
< xmlns:g="http://base.google.com/ns/1.0" version="1.0">
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4:
parser
error :
attributes construct error
< xmlns:g="http://base.google.com/ns/1.0" version="1.0">
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4:
namespace
error :
Namespace prefix xml开发者_开发问答ns on g is not defined
< xmlns:g="http://base.google.com/ns/1.0" version="1.0">
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:4:
parser
error :
Couldn't find end of Start Tag g line 4
< xmlns:g="http://base.google.com/ns/1.0" version="1.0">
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20:
parser
error :
Unescaped '<' not allowed in attributes values
< link>
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20:
parser
error :
attributes construct error
< link>
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:20:
parser
error :
Couldn't find end of Start Tag template line 19
< link>
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:22:
parser
error :
Opening and ending tag mismatch: stylesheet line 2 and template
< /xsl:template>
^
/Users/subnetfile/Desktop/finalxsltemplate.xslt:24:
parser
error :
Extra content at the end of the document
< xsl:template match="Condition">
^
error
xsltParseStylesheetFile : cannot parse /Users/subnetfile/Desktop/finalxsltemplate.xslt
(null)
am i understanding this correctly that i only really need to use the 'match' function as all the items simply have one sub-level of attributes with no attribute-values or anything like that.
also, do the fields in the final product NEED to have corresponding values in the original file, or can they be inserted for each item. for instance, the final format needs a field called 'payment_accepted' and there is no field to correspond, but i would just like to add the same value for each item, such as 'Visa'. Would i, instead of 'match', use something like 'foreach'?EDIT:
< xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:g="http://base.google.com/ns/1.0"
version="1.0">
< xsl:output method="RSS 2.0"/>
< xsl:template match="*[local-name()='title']">
< xsl:text>title: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="*[local-name()='link']">
< xsl:text>link: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="*[local-name()='description']">
< xsl:text>description: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="language"/> < !-- suppress -->
< /xsl:stylesheet>
This is starting to do something, but I need another clue please
EDIT:
I have made some progress and made something that does something correctly:< xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:g="http://base.google.com/ns/1.0"
version="1.0">
< xsl:output method="text"/>
< xsl:template match="Description">
< xsl:text>title: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="Url">
< xsl:text>link: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="Caption">
< xsl:text>description: </xsl:text>
< xsl:apply-templates/>
< /xsl:template>
< xsl:template match="language"/> < !-- suppress -->
< /xsl:stylesheet>
and this works with no debug errors, it isnt exactly what i need, how would i add the google namespace attributes that use the xlmnsg..etc and some attributes that dont have corresponding values in the xml file, like i just want to assign the same g:condition 'new' to each node(am i correct that each element is called a node here?)
There are many formal errors in the text that you provide as "XSLT stylesheet":
the left angle brackets of any start tag are not followed immediately by a name.
It isn't well-formed XML:
<xmlns:g="http://base.google.com/ns/1.0"
version="1.0">
There are two errors here:
The name
xmlns:g
is not allowed because a namespace prefix must not start with the reserved word "xml".An elemnt name cannot be followed by a non-space character (in this case
=
).
This looks like a namespace declaration and should not be coded as something that looks like an element.
<xsl:template match="Url> <link> </link> </xsl:template> <xsl:template match="Condition">
The second quote that must enclose the value of the match
attribute is missing, so everything between the first quote on the first line and the first quote on the last line is considered to be the attribute value ...
Even when all these errors are corrected and the stylesheet now looks like this:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:g="http://base.google.com/ns/1.0" version="1.0"> <xsl:output method="RSS 2.0"/> <xsl:template match="Description"> <title> </title> </xsl:template> <xsl:template match="Caption"> <description> </description> </xsl:template> <xsl:template match="Url"> <link> </link> </xsl:template> <xsl:template match="Condition"> <g:condition> </g:condition> </xsl:template> <xsl:template match="Picture"> <g:image_link> </g:image_link> </xsl:template> </xsl:stylesheet>
this code is pretty meaningless. It doesn't really process the source XML file and doesn't bring any contents from it in the resulting output.
My recommendation to you is to read at least a light XSLT tutorial and only when you have some basic understanding to start writing code.
精彩评论