XSLT 从带有命名空间前缀的标签中获取值的问题 [英] Problem with XSLT getting values from tags with namespace prefixes

查看:25
本文介绍了XSLT 从带有命名空间前缀的标签中获取值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从一些定义了命名空间前缀的 XML 中获取宽度和高度的值时遇到了一个特定的问题.我可以使用带有命名空间n:"的普通 xpath 很容易地从 RelatedMaterial 获取其他值,例如 SomeText,但无法获取宽度和高度的值.

I have a specific problem getting values for width and height out of some XML that has namespace prefixes defined. I can get other values such as SomeText from RelatedMaterial quite easily using normal xpath with namespace "n:" but unable to get values for width and height.

示例 XML:

<Description>
<Information>
<GroupInformation xml:lang="en">
 <BasicDescription>
  <RelatedMaterial>
   <SomeText>Hello</SomeText>
   <t:ContentProperties>
    <t:ContentAttributes>
     <t:Width>555</t:Width>
     <t:Height>444</t:Height>
    </t:ContentAttributes>
   </t:ContentProperties>
  </RelatedMaterial>
 </BasicDescription>
</GroupInformation>
</Information>
</Description>

以下是 XSLT 的摘录:

Here is an extract from the XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:n="urn:t:myfoo:2010" xmlns:tva2="urn:t:myfoo:extended:2008"

<xsl:apply-templates select="n:Description/n:Information/n:GroupInformation"/>

<xsl:template match="n:GroupInformation">
  <width>
    <xsl:value-of select="n:BasicDescription/n:RelatedMaterial/t:ContentProperties/t:ContentAttributes/t:Width"/>
  </width>
</xsl:template>

上述 XSLT 不适用于获取宽度.有什么想法吗?

The above XSLT does not work for getting the width. Any ideas?

推荐答案

我不确定您是否意识到您的输入和 XSLT 都无效,最好提供工作示例.

I'm not sure you have realised that both your input and XSLT is invalid, it's always better to provide working examples.

无论如何,如果我们查看 XPath 表达式 n:BasicDescription/n:RelatedMaterial/t:ContentProperties/t:ContentAttributes/t:Width,您使用的是前缀 n 映射到 urn:t:myfoo:2010 但实际上数据在默认命名空间中.t 前缀也是如此,它在输入数据和 XSLT 中都没有定义.

Anyway, if we look at the XPath expression n:BasicDescription/n:RelatedMaterial/t:ContentProperties/t:ContentAttributes/t:Width you're using a prefix n which is mapped to urn:t:myfoo:2010 but when the data infact is in the default namespace. The same goes for the t prefix which isn't defined at all in neither the input data nor XSLT.

您需要在 XML 数据和 XSLT 转换中定义双方"的命名空间,并且它们需要相同,不是前缀,而是 URI.

You need to define the namespaces on "both sides", in the XML data and the XSLT transformation and they need to be the same, not the prefixes, but the URI.

其他人可能比我更能解释这一点.

Somebody else could probably explain this better than me.

我已更正了您的示例并添加了一些内容以使其有效.

I've corrected your example and added a few things to make this work.

输入:

<?xml version="1.0" encoding="UTF-8"?>
<Description 
  xmlns="urn:t:myfoo:2010" 
  xmlns:t="something...">
  <Information>
    <GroupInformation xml:lang="en">
      <BasicDescription>
        <RelatedMaterial>
          <SomeText>Hello</SomeText>
          <t:ContentProperties>
            <t:ContentAttributes>
              <t:Width>555</t:Width>
              <t:Height>444</t:Height>
            </t:ContentAttributes>
          </t:ContentProperties>
        </RelatedMaterial>
      </BasicDescription>
    </GroupInformation>
  </Information>
</Description>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" 
  xmlns:n="urn:t:myfoo:2010" 
  xmlns:t="something...">

  <xsl:template match="/">
    <xsl:apply-templates select="n:Description/n:Information/n:GroupInformation"/>
  </xsl:template>

  <xsl:template match="n:GroupInformation">
    <xsl:element name="width">
      <xsl:value-of select="n:BasicDescription/n:RelatedMaterial/t:ContentProperties/t:ContentAttributes/t:Width"/>
    </xsl:element>
  </xsl:template>

</xsl:stylesheet>

输出:

<?xml version="1.0" encoding="UTF-8"?>
<width>555</width>

这篇关于XSLT 从带有命名空间前缀的标签中获取值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆