我们如何使用 XSLT 实现 XML,它根据字符串模式删除不需要的数据
How can we use XSLT to achieve an XML which removes unwanted data based on a pattern of string
请求XML:
<SchoolName>
<name>ABC International, Wisconsin</name>
<rank>10</rank>
</SchoolName>
<SchoolName>
<name>XYZ Primary, Las Vegas</name>
<rank>4</rank>
</SchoolName>
<SchoolName>
<name>Ryan Academy, Wisconsin</name>
<rank>6</rank>
</SchoolName>
<SchoolName>
<name>Advanced Elementary, Houston</name>
<rank>15</rank>
</SchoolName>
目标XML:
必须只考虑名称包含 "Wisconsin" 但名称不应包含 "Academy" 的学校。所以它只会 return 以下:
<SchoolName>
<name>ABC International, Wisconsin</name>
<rank>1</rank>
</SchoolName>
实现此目标的最佳方法是什么?实现此目的的 XSLT 文件应该是什么样子?
您可以使用 xslt:if
过滤器
来实现您的目标
<xsl:for-each select="SchoolNames/SchoolName">
<xsl:if test="name[contains(text(),'Wisconsin')] and not(name[contains(text(),'Academy')])">
<!-- do something -->
</xsl:if>
</xsl:for-each>
或select谓词:
<xsl:for-each select="SchoolNames/SchoolName[name[contains(text(),'Wisconsin')] and not(name[contains(text(),'Academy')])]">
<!-- do something -->
</xsl:for-each>
请求XML:
<SchoolName>
<name>ABC International, Wisconsin</name>
<rank>10</rank>
</SchoolName>
<SchoolName>
<name>XYZ Primary, Las Vegas</name>
<rank>4</rank>
</SchoolName>
<SchoolName>
<name>Ryan Academy, Wisconsin</name>
<rank>6</rank>
</SchoolName>
<SchoolName>
<name>Advanced Elementary, Houston</name>
<rank>15</rank>
</SchoolName>
目标XML: 必须只考虑名称包含 "Wisconsin" 但名称不应包含 "Academy" 的学校。所以它只会 return 以下:
<SchoolName>
<name>ABC International, Wisconsin</name>
<rank>1</rank>
</SchoolName>
实现此目标的最佳方法是什么?实现此目的的 XSLT 文件应该是什么样子?
您可以使用 xslt:if
过滤器
<xsl:for-each select="SchoolNames/SchoolName">
<xsl:if test="name[contains(text(),'Wisconsin')] and not(name[contains(text(),'Academy')])">
<!-- do something -->
</xsl:if>
</xsl:for-each>
或select谓词:
<xsl:for-each select="SchoolNames/SchoolName[name[contains(text(),'Wisconsin')] and not(name[contains(text(),'Academy')])]">
<!-- do something -->
</xsl:for-each>