2012-01-27 108 views
3

我有下面的XML:XSL檢查是否有子節點具有屬性

<record> 
    <d989 p="Apples" t="Apples"/> 
    <d990 p="Oranges" t="Bananas"/> 
    <record_group_1> 
    <d991 p="Mouse" t="Mouse and Cat"/> 
    <d991 p="Dog" t="Dog and Cat"/> 
    </record_group_1> 
    <record_group_2> 
    <d992 /> 
    </record_group_2> 
... 

和我使用後,確定以下XSL模板,如果該節點具有子:

<xsl:template name="hasChildren"> 
    <tr> 
    <td colspan="2" class="sectionTitle"> 
     <xsl:value-of select="translate(local-name(), '_', ' ')" /> 
    </td> 
    </tr> 
... 

如何包裝<xsl:template name="hasChildren">內容以確定有問題的節點是否有子節點,並確定任何子節點的屬性爲p。

我正在測試,如果當前節點具有p的屬性<xsl:if test="@p">,但我不知道如何找到節點的孩子是否有p。

對於上面的XML示例,我想忽略<record_group_2>,因爲它的子項不包含p的屬性,而我想要處理的是<record_group_1>

如果你需要更多的澄清,讓我知道...

回答

1

和我在確定節點 是否有子節點後,使用以下XSL模板:

<xsl:template name="hasChildren"> 
    <tr> 
     <td colspan="2" class="sectionTitle"> 
      <xsl:value-of select="translate(local-name(), '_', ' ')" /> 
     </td> 
    </tr> ... 
</xsl:template> 

此代碼不會在所有做它是說到做到!

I'm testing if the current node has an attribute of `p` with `<xsl:if test="@p">` but I'm not sure how I could find if the node's children has a `p`. 

使用

*[@p] 

此XPath表達式選擇任何子元素(當前節點的),其具有的屬性p。當在<xsl:if><xsl:when>test屬性中使用時,如果節點集非空,則選擇的節點集將轉換爲布爾型:true(),否則轉換爲布爾型:false()

對於XML上述例子我想忽略<record_group_2> 因爲它的孩子不包含其中,如 <record_group_1>我想處理

使用 p的屬性:只要上述表達。

這是一個完整的,非常簡單和短期轉型該副本輸出只有頂件的那些孩子,有一個p屬性:

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 
<xsl:strip-space elements="*"/> 

<xsl:template match="/*"> 
    <xsl:copy> 
    <xsl:copy-of select="*[*/@p]"/> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

當這種變換的應用提供的XML文檔(校正爲良好的形成):

<record> 
    <d989 p="Apples" t="Apples"/> 
    <d990 p="Oranges" t="Bananas"/> 
    <record_group_1> 
     <d991 p="Mouse" t="Mouse and Cat"/> 
     <d991 p="Dog" t="Dog and Cat"/> 
    </record_group_1> 
    <record_group_2> 
     <d992 /> 
    </record_group_2> 
</record> 

有用,正確ř esult 產生(不具有帶p屬性都將被刪除至少一個孩子非頂級元素):

<record> 
    <record_group_1> 
     <d991 p="Mouse" t="Mouse and Cat"/> 
     <d991 p="Dog" t="Dog and Cat"/> 
    </record_group_1> 
</record> 
+0

這是偉大的,正是我一直在尋找。 – Matthew 2012-02-07 17:38:38

+0

@Matthew:不客氣。 – 2012-02-07 18:38:48

1

這個表達式:

*[starts-with(local-name(), 'record_group_') and *[@p]] 

...名稱以record_group_開始,誰擁有與p屬性的子元素的所有元素相匹配(*[@p])。

目前還不清楚是什麼實際你要找的,但下面的樣式表應該表現出一般方法輸出:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 
    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
    <!-- ignore other elements --> 
    <xsl:template match="/*/*"/> 
    <xsl:template match="*[starts-with(local-name(), 'record_group_') 
           and *[@p]]"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 
</xsl:stylesheet> 

輸出:

<record> 
    <record_group_1> 
     <d991 p="Mouse" t="Mouse and Cat"/> 
     <d991 p="Dog" t="Dog and Cat"/> 
    </record_group_1> 
</record>