I am having problem transforming xml to xml using xsl. I found out where the problem comes from but still don’t know how to fix it.
<?xml version="1.0" encoding="UTF-8"?>
<tells uri="" xmlns="http://dl.kr.org/dig/2003/02/lang">
<impliesc>
<catom name="name1"/>
<catom name="name2"/>
</impliesc>
<impliesc>
<catom name="name3"/>
<catom name="name4"/>
</impliesc>
</tells>
I think the problems comes from this line
<tells uri="" xmlns="http://dl.kr.org/dig/2003/02/lang">
If I remove the “uri=”” xmlns=”http://dl.kr.org/dig/2003/02/lang”” and leave only the
"<tells>"
everything works perfectly. But is there a way to transform it without removing it?
<?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="/">
<xsl:element name="tells">
<xsl:for-each select="./tells/impliesc">
<xsl:element name="impliesc">
<xsl:for-each select="./tells/impliesc/catom">
<xsl:element name="catom">
<xsl:attribute name="name">
<xsl:value-of select="./tells/impliesc/individual/@name"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
You are correct: the reason your transformation isn’t working is that you have a namespace declaration on the
tellselement.To make this particular problem go away, you need to make your XSLT aware of the namespace, and tell it that the
tellselement that it’s trying to transform belongs to that namespace. The most common way to do this is to declare a namespace prefix in thexsl:stylesheetelement, and then use that prefix in the XPath, e.g.:The
xmlns:nattribute at the top of the stylesheet tells the XSLT processor that the ‘http://dl.kr.org/dig/2003/02/lang’ namespace exists, and that then:prefix is an abbreviation for it. In theselectandmatchattributes, then:prefix tells the XSLT processor that when it’s looking for elements namedtells, it should look for elements with that name that are in thehttp://dl.kr.org/dig/2003/02/langnamespace.XML namespaces are an essential part of XML technologies, and people often overlook them when they’re learning XML – they’re complicated and not very intuitive, and you can ignore them for quite a while when you’re coming up to speed on XML. But there comes a point where you can no longer afford the simplifying assumption that namespaces don’t exist, and you’re at that point.