2012-11-19 54 views
0

你好,我嘗試使用XSLT如何獲得一個變量在內部for循環XSL

<?xml version="1.0" encoding="utf-8"?> 
    <posts> 
     <row Id="1" PostTypeId="1" AcceptedAnswerId="13" CreationDate="2010-09-13T19:16:26.763" Score="45" ViewCount="8799" Body="&lt;p&gt;This is a common question by those who have just rooted their phones. What apps, ROMs, benefits, etc. do I get from rooting? What should I be doing now?&lt;/p&gt;&#xA;" OwnerUserId="10" LastEditorUserId="267" LastEditorDisplayName="" LastEditDate="2011-01-25T16:00:21.373" LastActivityDate="2011-08-02T14:44:12.740" CommunityOwnedDate="2011-01-25T08:44:10.820" Title="I've rooted my phone. Now what?" Tags="&lt;rooting&gt;" AnswerCount="8" CommentCount="5" FavoriteCount="31" /> 
     <row Id="2" PostTypeId="1" AcceptedAnswerId="4" CreationDate="2010-09-13T19:17:17.917" Score="10" ViewCount="619" Body="&lt;p&gt;I have a Google Nexus One with Android 2.2. I didn't like the default SMS-application so I installed Handcent-SMS. Now when I get an SMS, I get notified twice. How can I fix this?&lt;/p&gt;&#xA;" OwnerUserId="7" LastEditorUserId="7" LastEditorDisplayName="" LastEditDate="2010-09-13T19:25:52.433" LastActivityDate="2010-09-13T19:25:52.433" Title="I installed another SMS application, now I get notified twice" Tags="&lt;2.2-froyo&gt;&lt;sms&gt;&lt;handcent-sms&gt;&lt;applications&gt;&lt;notifications&gt;" AnswerCount="3" FavoriteCount="2" /> 
     <row Id="4" PostTypeId="2" ParentId="2" CreationDate="2010-09-13T19:19:23.200" Score="16" ViewCount="" Body="&lt;p&gt;You can turn off notification in your stock Messaging application by going into the settings dialog (Menu button -&gt; Settings) and unchecking Notifications&lt;/p&gt;&#xA;" OwnerUserId="21" LastActivityDate="2010-09-13T19:19:23.200" CommentCount="1" /> 
     <row Id="5" PostTypeId="1" CreationDate="2010-09-13T19:19:35.360" Score="5" ViewCount="234" Body="&lt;p&gt;I have a Motorola DROID v1 that is running Froyo (2.2). I've noticed that if my device sleeps for a while, when I wake it up it will not attempt to connect to the wireless AP in my home. When I go to the wireless settings section, I see a note next to the AP entry that says 'disabled'. If I click the entry and select 'connect', it connects right away. It will remain connected until the next longish sleep, when it is again marked as disabled.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Is there any way to prevent this AP entry from being marked as disabled?&lt;/p&gt;&#xA;" OwnerUserId="9" LastActivityDate="2011-09-05T03:43:01.573" Title="Moto Droid v1 disabling primary Wireless AP" Tags="&lt;2.2-froyo&gt;" AnswerCount="1" CommentCount="7" FavoriteCount="1" /> 
     <row Id="6" PostTypeId="1" CreationDate="2010-09-13T19:20:47.643" Score="2" ViewCount="67" Body="&lt;p&gt;Google recently intoduced a new mechanism for developers to protect their applications from piracy - the License Verification Library. I've been looking at incorporating this into my apps (in fact I have it working) but I'm concerned that it will adversely affect the user experience.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;Has anybody had problems using apps that incorporate the licensing check? Does it cause problems when you don't have Internet connectivity in order to do the validation? Are there any other annoyances from an end-user perspective?&lt;/p&gt;&#xA;" OwnerUserId="19" LastActivityDate="2010-10-13T22:41:58.303" Title="Experiences with Google's new licensing service for apps?" Tags="&lt;market&gt;" AnswerCount="1" /> 
     <row Id="7" PostTypeId="2" ParentId="2" CreationDate="2010-09-13T19:20:52.027" Score="2" ViewCount="" Body="&lt;p&gt;Open the default messaging application, click the menu button and then Settings. Scroll down and disable &lt;strong&gt;Notifications&lt;/strong&gt;.&lt;/p&gt;&#xA;" OwnerUserId="27" LastActivityDate="2010-09-13T19:20:52.027" /> 

我使用它來解析XML

<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:template match="/"> 
      <xsl:for-each select="posts/row"> 
       <xsl:if test="@PostTypeId = 2"> 

       <xsl:variable name="parent" select="@ParentId"/> 
       <xsl:for-each select="/posts/row"> 
       <xsl:if test="@PostTypeId = 1"> 
       <xsl:variable name="patentVariable" select="$parent"/> 
           <xsl:if test="@ParentId = $patentVariable"> 
        <field name ="$parentid"><xsl:value-of select="@Title" /></field> 
           </xsl:if> 
       </xsl:if> 
       </xsl:for-each> 
       </xsl:if> 
      </xsl:for-each> 
    </xsl:template> 
</xsl:stylesheet> 

我不能解析XML在for循環中打印父id可以告訴我如何做到這一點?

預期輸出

<field name="1">I've rooted my phone. Now what?</field> 
+0

您好!在這種情況下是否可以顯示您的預期產出?謝謝! –

+0

有兩種類型,一種是PostTypeId = 1,它具有title屬性,另一種是PostTypeId = 2,它具有指向PostType = 1的parentId,我需要獲得postTypeId = 2的標題 – Kathick

回答

0

你缺少的XPath的一個非常基本的功能。您的XSLT顯示您似乎不知道XPath predicates(即方括號中的過濾表達式)。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:template match="/"> 
    <xsl:apply-templates select="posts/row[@PostTypeId = 2]" /> 
    </xsl:template> 

    <xsl:template match="row[@PostTypeId = 2]"> 
    <xsl:variable name="parent" select="@ParentId" /> 
    <field name="{$parent}"> 
     <xsl:value-of select="../row[@Id = $parent]/@Title" /> 
    </field> 
    </xsl:template> 

</xsl:stylesheet> 

正如你所看到的,不需要像<xsl:if test="@PostTypeId = 2">這樣的東西。我建議閱讀XPath的基礎知識。

另請注意,您應該避免<xsl:for-each>。它比你想象的要少得多。改爲使用<xsl:apply-templates>,就像上面顯示的那樣。