XSLT 脚本中 'where' 和 'if' 之间的区别
Difference between 'where' and 'if' in an XSLT script
我是 XML/XSLT 的新手,我对 <xsl:if test="x">
和在语句末尾添加 where="x"
之间的区别有点困惑。
下面是一些示例数据和两个 XSLT 版本的代码。我在这里尝试了 运行 两种方式:https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 但没有出现,所以我可能做错了什么。有人能为我澄清一下吗?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
使用 where
我的 XSLT 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
使用 if
语句的代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
xsl:for-each
没有 where
属性。
你的意思是叫一个predicate which is enclosed by Double brackets.
所以把你的 xsl:for-each
从
改成
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
到
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
这应该可以解决问题。
我是 XML/XSLT 的新手,我对 <xsl:if test="x">
和在语句末尾添加 where="x"
之间的区别有点困惑。
下面是一些示例数据和两个 XSLT 版本的代码。我在这里尝试了 运行 两种方式:https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 但没有出现,所以我可能做错了什么。有人能为我澄清一下吗?
<?xml version="1.0"?>
<Tests xmlns="http://www.adatum.com">
<Test TestId="0001" TestType="CMD">
<Name>Convert number to string</Name>
<CommandLine>Examp1.EXE</CommandLine>
<Input>1</Input>
<Output>One</Output>
</Test>
<Test TestId="0002" TestType="CMD">
<Name>Find succeeding characters</Name>
<CommandLine>Examp2.EXE</CommandLine>
<Input>abc</Input>
<Output>def</Output>
</Test>
<Test TestId="0003" TestType="GUI">
<Name>Convert multiple numbers to strings</Name>
<CommandLine>Examp2.EXE /Verbose</CommandLine>
<Input>123</Input>
<Output>One Two Three</Output>
</Test>
<Test TestId="0004" TestType="GUI">
<Name>Find correlated key</Name>
<CommandLine>Examp3.EXE</CommandLine>
<Input>a1</Input>
<Output>b1</Output>
</Test>
<Test TestId="0005" TestType="GUI">
<Name>Count characters</Name>
<CommandLine>FinalExamp.EXE</CommandLine>
<Input>This is a test</Input>
<Output>14</Output>
</Test>
</Tests>
使用 where
我的 XSLT 是:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
使用 if
语句的代码
<?xml version="1.0" encoding="UTF-8"?>
<xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Tests/Test">
<xsl:if test="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet
xsl:for-each
没有 where
属性。
你的意思是叫一个predicate which is enclosed by Double brackets.
所以把你的 xsl:for-each
从
<xsl:for-each select="Tests/Test" where="@TestType='CMD'">
<xsl:value-of select="current()">
</xsl:for-each>
到
<xsl:for-each select="Tests/Test[@TestType='CMD']">
<xsl:value-of select="current()">
</xsl:for-each>
这应该可以解决问题。