I am playing around with XSLT for a small task. I have the following XML:
<PFeed>
<PID> MyProcess </PID>
<Version>1</Version>
<MetaData>
<Id> MyMetadataId </Id>
</MetaData>
<AllFeeds>
<FeedContent>
<Id> FeedContentId </Id>
</FeedContent>
</AllFeeds>
</PFeed>
I want to extract FeedContentId as text from this XML.
This is the XSLT code I have:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8" />
<xsl:template match="/PFeed/AllFeeds/FeedContent">
<xsl:value-of select="Id"/>
</xsl:template>
</xsl:stylesheet>
But instead of outputting FeedContentId I get the following:
MyProcess 1 MyMetadataId FeedContentId
Can you please point out what I am missing ?
You first need a template to match the root element, then match what's inside. This XSLT now works again. (Test e.g. on http://www.w3schools.com/xsl/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog)
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/PFeed">
<xsl:apply-templates select="AllFeeds/FeedContent"/>
</xsl:template>
<xsl:template match="FeedContent">
<xsl:value-of select="Id"/>
</xsl:template>
</xsl:stylesheet>
Output after transformation:
FeedContentId
However, the same thing (getting the /PFeed/AllFeeds/FeedContent/Id node) could also be achieved by evaluating the above simple XPath expression.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With