A template match of /
does not start at the document element, it is the "root node" - which is before any content. The first node() in the document doesn't have to be the document element, it could be a comment or a processing instruction and would be a child of the "root node".
So, if you want to match the document element mods
, and you don't want to worry about namespaces your template match could be /*
. Then your XPath selecting elements relative from the document element would work.
However, your titleInfo
and title
elements inherit the namespace of the document element. So, if you want to match them you have several options:
- Declare the namespace and give it a prefix so that you can use it in your XPath
- Match generically on element with a predicate filter to match the local-name() and namespace-uri() - but if you know the URI it would be easier to declare it and use the prefix in option #1.
- Match generically on element with a predicate filter to match just the local-name(). Not as clean/proper, but likely will work.
- Match generically on element for each step and rely on the structure of this simple document to find the element you are looking for
Option #1:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:m="http://www.loc.gov/mods/v3">
<xsl:template match="/m:mods">
<h2>Description</h2>
<p>Hello</p>
<p><xsl:value-of select="m:titleInfo/m:title"/></p>
</xsl:template>
</xsl:stylesheet>
Option #2:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*[local-name()='mods' and namespace-uri()='http://www.loc.gov/mods/v3']">
<h2>Description</h2>
<p>Hello</p>
<p><xsl:value-of select="*[local-name()='titleInfo'
and namespace-uri()='http://www.loc.gov/mods/v3']/*[local-name()='title' and namespace-uri()='http://www.loc.gov/mods/v3']"/></p>
</xsl:template>
</xsl:stylesheet>
Option #3:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*[local-name()='mods']">
<h2>Description</h2>
<p>Hello</p>
<p><xsl:value-of select="*[local-name()='titleInfo']/*[local-name()='title']"/></p>
</xsl:template>
</xsl:stylesheet>
Option #4:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/*">
<h2>Description</h2>
<p>Hello</p>
<p><xsl:value-of select="*/*"/></p>
</xsl:template>
</xsl:stylesheet>