处理节点,xml到xslt

时间:2014-03-14 01:31:21

标签: xml xslt

经过一遍又一遍的摧毁,试图弄清楚这是我的xlst所剩下的一切。 我试图从xml文档中提取子元素并输出它们,但即使有一个格式良好的文档,我也是一些如何得到解析错误。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

      <xsl:output method="text"/>

      <xsl:template match="/">
        <xsl:for-each select="FamousMountains/mountain"/>
        <xsl:value-of select="name[@language='English'] "/>

      </xsl:template>

</xsl:stylesheet>

这是xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Note: This is a comment-->
<?xml-stylesheet type="text/xsl" href="Asg06.xsl"?>
<FamousMountains>
  <mountain>
    <name language="English">Mount Everest</name>
    <name language="PigLatin">ountMa verestEa</name>
    <location>Nepal</location>
    <height units="feet">29035</height>
  </mountain>
  <mountain>
    <name language="English">Mount Ranier</name>
    <location>Washington</location>
    <height units="feet">14411</height>
  </mountain>
  <mountain>
    <name language="English">Mount St. Helens</name>
    <location>Washington</location>
    <height units="feet">8364</height>
  </mountain>
  <mountain>
    <name language="English">Mount Washington</name>
    <name language="PigLatin">ountMa ashingtonWa</name>
    <location>New Hampshire</location>
    <height units="feet">6288</height>
  </mountain>
  <mountain>
    <name language="English">Mount Bonnell</name>
    <name language="PigLatin">ountMa onnellBa</name>
    <location>Austin</location>
    <height units="feet">800</height>
  </mountain>
  <mountain>
    <name language="English">Mount Vesuvius</name>
    <name language="PigLatin">ountMa esuviusVa</name>
    <location>Italy</location>
    <height units="feet">4203</height>
  </mountain>
  <mountain>
    <name language="English">Mount Etna</name>
    <name language="PigLatin">ountMa tnaEa</name>
    <location>Sicily</location>
    <height units="feet">10922</height>
  </mountain>
</FamousMountains>

这是xlst应该输出的内容:

Put your name here
Famous Mountains of the World

Mountain Name: Mount Everest
Mountain Name: (ountMa verestEa)

Mountain Name: Mount Ranier
Mountain Name: ()

Mountain Name: Mount St. Helens
Mountain Name: ()

Mountain Name: Mount Washington
Mountain Name: (ountMa ashingtonWa)

Mountain Name: Mount Bonnell
Mountain Name: (ountMa onnellBa)

Mountain Name: Mount Vesuvius
Mountain Name: (ountMa esuviusVa)

Mountain Name: Mount Etna
Mountain Name: (ountMa tnaEa)

非常感谢任何帮助或见解。

1 个答案:

答案 0 :(得分:0)

只是重申一下评论所说的内容(@helderdarocha),<xsl:value-of />只有在<xsl:for-each />标记内才有效。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>

    <xsl:template match="/">
        <xsl:for-each select="FamousMountains/mountain" >
            <xsl:value-of select="name[@language='English'] "/>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

<强>给出

Mount EverestMount RanierMount St. HelensMount WashingtonMount BonnellMount VesuviusMount Etna

这应该足以让你入门! (但无论如何这里都是答案。)

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text"/>

    <xsl:template match="/">Put your name here
Famous Mountains of the World<xsl:apply-templates />
    </xsl:template>

    <xsl:template match="mountain">
Mountain Name: <xsl:value-of select="name[@language='English'] "/>
Mountain Name: (<xsl:value-of select="name[@language='PigLatin'] "/>)</xsl:template>

</xsl:stylesheet>