2012-03-30 95 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> 
相关问题