[英]XSL value-of doesn't seem to get value from the xml
I have an xml file which contains following: 我有一个xml文件,其中包含以下内容:
<?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 an XSL file: 和一个XSL文件:
<?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 is I don't get the title value in the xHTML. 我的问题是我没有在xHTML中获得标题值。 I could only see 我只能看到
Hello 你好
But If I remove default namespace from the xml like this: 但是,如果我从xml中删除默认命名空间,如下所示:
<?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's match to <xsl:template match="/mods">
I can see the title value. 并将样式表的匹配更改为<xsl:template match="/mods">
我可以看到标题值。
But I can not remove the default namespace from the xml because xml is generated by a form and it won't work if I remove the default namespace. 但我无法从xml中删除默认命名空间,因为xml是由表单生成的,如果删除默认命名空间,它将无法工作。 I don't even have a clue how to get around this or if I am doing something wrong. 我甚至不知道如何绕过这个或者我做错了什么。 Please help. 请帮忙。
Thanks in Advance. 提前致谢。
Add a prefixed declaration for your namespace and then match on the prefixed names. 为命名空间添加带前缀的声明,然后匹配前缀名称。
Below is untested: 以下是未经测试的:
<?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>
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 /*
. 因此,如果您想匹配文档元素mods
,并且您不想担心名称空间,则模板匹配可能是/*
。 Then your XPath selecting elements relative from the document element would work. 然后您的XPath选择元素相对于文档元素将起作用。
However, your titleInfo
and title
elements inherit the namespace of the document element. 但是, titleInfo
和title
元素继承document元素的名称空间。 So, if you want to match them you have several options: 所以,如果你想匹配它们,你有几个选择:
Option #1: 选项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: 选项#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: 选项#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: 选项#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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.