2012-03-30 50 views
1

我正在編寫此XSLT文件並且遇到了有關如何執行以下操作的問題。我有一個具有相同名稱屬性的元素列表,我需要將它們複製並檢查它們是否具有必需的文本。如果沒有一個元素不添加一個元素。XSL檢查多個具有相同名稱和屬性的元素是否與給定文本相同

例XML:

<record> 
    </Title> 
    </subTitle> 
    <note tag='1'> 
    <access tag='1'>nothing</access> 
    <access tag='a'>Home</access> 
    <access tag='a'>School</access> 
    </note tag='1'> 
</record> 

與實施例這將輸出:

<record> 
    </Title> 
    </subTitle> 
    <note tag='1'> 
    <access tag='1'>nothing</access> 
    <access tag='a'>Home</access> 
    <access tag='a'>School</access> 
    <access tag="a'>Required</access> 
    </note tag='1'> 
</record> 

如果得到的XML被通過XSLT跑再次它會輸出是沒有變化。我知道如何做到這一點,如果與屬性a訪問將只有1元素。我有的問題是檢查多個。

感謝您的任何幫助。

回答

0
<xsl:template match="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="note[count(access[text()='Required'])=0]"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
     <xsl:element name="access"> 
     <xsl:attribute name="tag">a</xsl:attribute> 
     Required 
     </xsl:element> 
    </xsl:copy> 
</xsl:template> 
+0

感謝您的幫助。使用計數有助於。如何考慮a的屬性。 IE瀏覽器如果標籤='1'必須在它然後我仍然想要添加它。 – Flips 2012-03-30 17:20:49

1

下面是一個簡短和簡單的解決方案

<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="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match= 
"note[not(access[@tag = 'a' and . = 'Required'])]"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    <access tag="a">Required</access> 
    </xsl:copy> 
</xsl:template> 
</xsl:stylesheet> 

當該變換被應用到所提供的XML文檔(校正的嚴重畸形原始到一個良好的XML文檔) :

<record> 
    <Title/> 
    <subTitle/> 
    <note tag='1'> 
     <access tag='1'>nothing</access> 
     <access tag='a'>Home</access> 
     <access tag='a'>School</access> 
    </note> 
</record> 

想要的,正確的結果是生產

<record> 
    <Title/> 
    <subTitle/> 
    <note tag="1"> 
     <access tag="1">nothing</access> 
     <access tag="a">Home</access> 
     <access tag="a">School</access> 
     <access tag="a">Required</access> 
    </note> 
</record> 
相關問題