【问题标题】:What does my XSLT need to do when the XML has a xmlns in it?当 XML 中有 xmlns 时,我的 XSLT 需要做什么?
【发布时间】:2010-06-24 21:13:50
【问题描述】:

对于以下 XML

<Properties ComponentID="1272040480745" Admin="true">
<Datum ID="P01" Type="File" Name="CSS To Use">style.css</Datum>
<Data>
    <External></External>
    <Result>
        <results xmlns="http://www.interwoven.com/schema/iwrr" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.interwoven.com/schema/iwrr iwrr.xsd" total="1" included="1" start="0" status="200">
           <assets>
               <document id="019c7c763e5ae286c7d566ff883f8199" uri="/document/id/019c7c763e5ae286c7d566ff883f8199" context="cb478aef64c6415b390e241885fd1346" path="templatedata/www/location/data/Belton">
                    <metadata>
                        <field name="TeamSite/Metadata/locationRegion">
                            Central
                        </field>
                    </metadata>
                </document>
            </assets>
        </results>
    </Result>
</Data>

如何从文档元素中选择路径属性?我目前的 xslt 是:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
   <xsl:template match="/">
      <div>      
        <xsl:apply-templates select="Properties/Data/Result/results/assets/document"></xsl:apply-templates>
        <xsl:text>HELLO THERE!</xsl:text>
  </div>
 </xsl:template>
 <xsl:template match="document">
<xsl:value-of select="@path"></xsl:value-of>
 </xsl:template>
 </xsl:stylesheet> 

如果我从 xml 的结果元素中删除 xmlns、xmlns:xsi 和 xsi:schemaLocation,则 xslt 将起作用。所以很明显我不了解命名空间的东西,非常感谢一些见解。谢谢

【问题讨论】:

    标签: xml xslt namespaces


    【解决方案1】:

    这种转变

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:iwwr="http://www.interwoven.com/schema/iwrr"
     >
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:template match="/">
       <xsl:value-of select="/*/*/*/iwwr:results/*/iwwr:document/@path"/>
     </xsl:template>
    </xsl:stylesheet>
    

    应用于提供的 XML 文档时,根据需要生成 path 属性的值

    templatedata/www/location/data/Belton
    

    【讨论】:

    • 请注意,在本例中,iwwr 是完整命名空间的别名。
    • @Mark-Schultheiss:这是给我的评论吗?如果是,它的含义是什么?命名空间前缀是命名空间前缀。根据定义,它绑定到命名空间uri。我的回答演示了如何使用绑定到特定命名空间的命名空间前缀,以使包含属于该命名空间的元素名称的 XPath 表达式更具可读性。
    【解决方案2】:

    您也必须将命名空间添加到您的 xpath:

      <xsl:apply-templates xmlns:iwrr="http://www.interwoven.com/schema/iwrr"  select="Properties/Data/Result/iwrr:results/iwr:assets/iwr:document"></xsl:apply-templates>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-20
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多