当XML中包含xmlns时,我的XSLT需要做什么?

时间:2010-06-24 21:13:50

标签: xml xslt namespaces

对于以下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将起作用。显然我不明白名称空间的东西,并会非常欣赏一些见解。感谢

2 个答案:

答案 0 :(得分:0)

您还必须将名称空间添加到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>

答案 1 :(得分:0)

此转化

<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