简体   繁体   English

XSL值似乎没有从xml获得值

[英]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 我只能看到

Description 描述

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. 但是, titleInfotitle元素继承document元素的名称空间。 So, if you want to match them you have several options: 所以,如果你想匹配它们,你有几个选择:

  1. Declare the namespace and give it a prefix so that you can use it in your XPath 声明命名空间并为其指定一个前缀,以便您可以在XPath中使用它
  2. 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. 通常在元素上匹配谓词过滤器以匹配local-name()和namespace-uri() - 但是如果您知道URI,则更容易声明它并使用选项#1中的前缀。
  3. Match generically on element with a predicate filter to match just the local-name(). 通常在元素上匹配谓词过滤器以匹配local-name()。 Not as clean/proper, but likely will work. 不是干净/适当,但可能会起作用。
  4. 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: 选项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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM