Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xslt XSL检查具有相同名称和属性的多个元素是否等于给定的文本_Xslt - Fatal编程技术网

Xslt XSL检查具有相同名称和属性的多个元素是否等于给定的文本

Xslt XSL检查具有相同名称和属性的多个元素是否等于给定的文本,xslt,Xslt,我正在编写这个XSLT文件,并且对如何执行以下操作有疑问。我有一个具有相同名称属性的元素列表,我需要复制它们,并检查它们是否具有所需的文本。如果没有一个元素不添加一个元素 XML示例: <record> </Title> </subTitle> <note tag='1'> <access tag='1'>nothing</access> <access tag='a'>Home<

我正在编写这个XSLT文件,并且对如何执行以下操作有疑问。我有一个具有相同名称属性的元素列表,我需要复制它们,并检查它们是否具有所需的文本。如果没有一个元素不添加一个元素

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>

没有什么
家
学校

A.
要求的

A.
要求的

这里有一个简短而简单的解决方案

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

要求的
将此转换应用于提供的XML文档时(将格式严重错误的原始文档更正为格式良好的XML文档):


没有什么
家
学校
生成所需的正确结果

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

没有什么
家
学校
要求的

这里有一个简短而简单的解决方案

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

要求的
将此转换应用于提供的XML文档时(将格式严重错误的原始文档更正为格式良好的XML文档):


没有什么
家
学校
生成所需的正确结果

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

没有什么
家
学校
要求的

谢谢您的帮助。使用计数有帮助。考虑a的属性怎么样。IE如果tag='1'中有要求,则我仍要添加它。谢谢您的帮助。使用计数有帮助。考虑a的属性怎么样。即,如果tag='1'中有必填项,则我仍要添加它。