XSL value is not like xml value

I have an xml file containing the following:

<?xml version="1.0"?> <mods xmlns="http://www.loc.gov/mods/v3" xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink"> <titleInfo><title>A-Title-01</title></titleInfo> </mods> 

And the XSL file:

 <?xml version="1.0" encoding="UTF-8"?> <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="titleInfo/title"/></p> </xsl:template> </xsl:stylesheet> 

My problem: I do not get the title value in xHTML. I could only see

Description

Hello

But if I remove the default namespace from xml as follows:

 <?xml version="1.0"?> <mods xmlns:mods="http://www.loc.gov/mods/v3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink"> <titleInfo><title>A-Title-01</title></titleInfo> </mods> 

and change the style sheet to <xsl:template match="/mods"> . I see the meaning of the name.

But I cannot remove the default namespace from xml because xml is generated by the form and will not work if I remove the default namespace. I don’t even know how to get around this, or if I am doing something wrong. Please, help.

Thanks at Advance.

+4
source share
2 answers

Matching the pattern / does not start with the document element, it is the "root node" that is in front of any content. The first node () in the document should not be a document element, it can be a comment or processing instruction and will be a child of the "root node".

So, if you want to combine the mods document element and you don't want to worry about namespaces, the pattern match might be /* . Then your XPath select elements related to the document element will 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 a namespace and provide it with a prefix so you can use it in your XPath
  • Match the whole element with the predicate filter so that it matches 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 the whole element to the predicate filter so that it matches only local-name (). Not as clean / correct, but will most likely work.
  • Match the whole element at each step and rely on the structure of this simple document to find the element you are looking for.

Option number 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 number 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 number 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 number 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> 
+3
source

Add a prefix declaration for your namespace and then map it to the prefix names.

Below is not verified:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:loc="http://www.loc.gov/mods/v3" exclude-result-prefixes="loc"> <xsl:template match="/loc:mods"> <h2>Description</h2> <p>Hello</p> <p><xsl:value-of select="loc:titleInfo/loc:title"/></p> </xsl:template> </xsl:stylesheet> 
+3
source

Source: https://habr.com/ru/post/1502936/


All Articles