Reputation: 11
I am trying to loop through descendant nodes in an XML document, but I can't seem to get the correct syntax. The XML files come with a namespace included so in my XSL document I have aliased that namespace. A simple version of my XML document looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="my.xsl"?>
<game id="4496878f-921c-41ab-9fcd-54906a9ed89c" status="closed">
<summary>
<quarter id="1" points="10"/>
<quarter id="2" points="3"/>
<quarter id="3" points="0"/>
<quarter id="4" points="6"/>
</summary>
</game>
My XSL document looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="my.xsl">
<xsl:output method="text" indent="no" />
<xsl:template match="/">
<xsl:text>QUARTER,POINTS</xsl:text>
<xsl:apply-templates select="/f:game/f:summary" />
</xsl:template>
<xsl:template match="f:summary">
<xsl:text> </xsl:text>
<xsl:for-each select"/f:quarter">
<xsl:text>"</xsl:text><xsl:value-of select="@id"/><xsl:text>"</xsl:text>
<xsl:text>,"</xsl:text><xsl:value-of select="@points"/><xsl:text>"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 219
Reputation: 116959
The XML files come with a namespace
I don't see a namespace in the provided XML example. As others have noted, if you have a namespace, you must bind the prefix to the same namespace URI used in the input.
In addition, <xsl:for-each select"/f:quarter">
is trying to select a root element named quarter
, which doesn't exist. Not to mention the missing =
.
You probably meant <xsl:for-each select=".//f:quarter">
, but you don't need it in order to select the children (as opposed to descendants) of the current node - just do <xsl:for-each select="f:quarter">
.
Upvotes: 1
Reputation: 1076
Xml add Namespace
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Untitled4.xsl"?>
<game id="4496878f-921c-41ab-9fcd-54906a9ed89c" status="closed" xmlns="http://www.test.com/">
<summary>
<quarter id="1" points="10"/>
<quarter id="2" points="3"/>
<quarter id="3" points="0"/>
<quarter id="4" points="6"/>
</summary>
</game>
and xsl file add namesapce
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:t="http://www.test.com/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xslFormatting="urn:xslFormatting">
<xsl:output method="text" indent="no" />
<xsl:template match="/">
<xsl:text>QUARTER,POINTS</xsl:text>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="t:summary">
<xsl:text> </xsl:text>
<xsl:for-each select="t:quarter">
<xsl:text>\"</xsl:text><xsl:value-of select="@id"/><xsl:text>\"</xsl:text>
<xsl:text>,\"</xsl:text><xsl:value-of select="@points"/><xsl:text>\"</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1