简体   繁体   English

XSLT遍历和选择

[英]XSLT Transversing and selection

I am very new to xslt and need some assistance with a particular piece of code I have an XML file: ` 我是xslt的新手,需要一段特定的代码,我有一个XML文件,需要一些帮助:

<in:inputs xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
<!-- Function Call Result (0 ms), XPath /in:inputs/in:result[@name='SitemapXml']/Page-->
<in:result name="SitemapXml">
    <Page MenuTitle="Frontpage" UrlTitle="Home"  FolderPath="/Home" Depth="1"  xmlns="">
        <Page Depth="2" />
        <Page MenuTitle="Treadmills Reviews" UrlTitle="Treadmills-Reviews" Description=""  FolderPath="/Home/Treadmills-Reviews" Depth="2" isopen="true" iscurrent="true"  >
            <Page MenuTitle="Sole Fitness" UrlTitle="Sole-Fitness" Description=""  FolderPath="/Home/Treadmills-Reviews/Sole-Fitness" Depth="3">
                <Page MenuTitle="F-Series" UrlTitle="F-Series" Description=""  FolderPath="/Home/Treadmills-Reviews/Sole-Fitness/F-Series" Depth="4">
                    <Page MenuTitle="F63" UrlTitle="F63" Description=""  FolderPath="/Home/Treadmills-Reviews/Sole-Fitness/F-Series/F63" Depth="5" />
                </Page>
            </Page>
            <Page MenuTitle="Nordic Track" UrlTitle="Nordic-Track" Description=""  FolderPath="/Home/Treadmills-Reviews/Nordic-Track" Depth="3"/>
        </Page>
        <Page MenuTitle="Our Top Picks" UrlTitle="Our-Top-Picks" Description=""  FolderPath="/Home/Our-Top-Picks" Depth="2"/>
        <Page MenuTitle="Forums" UrlTitle="Forums" Description=""  FolderPath="/Home/Forums" Depth="2"/>
    </Page>
</in:result>

` `

I am trying to transverse the nodes with xslt and then print out the value of the "MenuTitle" of the node that "iscurrent='true'". 我正在尝试使用xslt遍历节点,然后打印出“ iscurrent ='true'”节点的“ MenuTitle”值。 There could be any number of levels of "Page" nodes as well as the iscurrent attribute will be in which ever page is currently open. 可以有许多级别的“页面”节点,并且iscurrent属性将位于当前打开页面的位置。

My xslt file is as follows ` 我的xslt文件如下

 <xsl:template match="/">
  <html>
  <head>

  </head>

  <body>
<span>
    <xsl:apply-templates mode="HeaderName" select="."/>
</span>

  </body>
  </html>
 </xsl:template>

<xsl:template mode="HeaderName" match="*">
<xsl:if test="@iscurrent='true'">
    <xsl:value-of  select="@MenuTilte" />
</xsl:if>
</xsl:template>

</xsl:stylesheet>`

What am I doing wrong if not everything? 如果不是全部,我在做什么错?

Thanks in advance. 提前致谢。

This transform : 此转换:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="in:result">
<xsl:apply-templates select="//Page[@iscurrent = 'true']"/>
</xsl:template>

<xsl:template match="Page">

<xsl:message terminate="no">
  <xsl:value-of select="@MenuTitle"/>
</xsl:message>
</xsl:template>

</xsl:stylesheet>

When applied to your input .xml file which I assume has some formatting mistakes, ie the comment is not correct as it comment the whole .xml out and there are two root element( I commended out the first one) 当我将其应用于输入.xml文件时,我认为它存在一些格式错误,即,注释不正确,因为它将整个.xml注释掉了,并且有两个根元素(我推荐第一个)

Will output message : 将输出消息:

[xslt] Treadmills Reviews

Of course in your case you will have to use the value of the attribute or in any case do something which suits your needs. 当然,在您的情况下,您将必须使用属性的值,或者在任何情况下都必须执行适合您需要的操作。

Regarding your xslt. 关于您的xslt。 Make sure you use the correct namespace and that you select correct nodes in your apply templates element. 确保使用正确的名称空间,并在应用模板元素中选择正确的节点。

The above xsl is for testing purposes only. 上面的xsl仅用于测试目的。 Change the output to html and remove the message element. 将输出更改为html并删除message元素。 Youy will end up in something like this : Youy最终会遇到这样的事情:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:in="http://www.composite.net/ns/transformation/input/1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template match="in:result">
<html>
  <head>
    <title>Test</title>

  </head>
  <body>
    <span>
      <xsl:apply-templates select="//Page[@iscurrent = 'true']"/>
    </span>
  </body>
</html>
</xsl:template>

<xsl:template match="Page">

  <xsl:value-of select="@MenuTitle"/>

</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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