如何在XSL中的内部for循环中获取变量

时间:2012-11-19 07:18:28

标签: xml xslt

您好我正在尝试使用XSLT解析xml

 <?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>

我无法在for循环中打印父ID,任何人都可以告诉我该怎么做?

预期输出

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

1 个答案:

答案 0 :(得分: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>,就像我上面所示。