I am new to XML/XSLT, and I'm a bit confused about the difference between the <xsl:if test="x"> and adding a where="x" at the end of a statement.
Below is some example data and two XSLT versions of code. I tried running it both ways here: https://www.w3schools.com/xml/tryxslt.asp?xmlfile=cdcatalog&xsltfile=cdcatalog_ex1 but nothing appears, so I may be doing something wrong. Is anyone able to clarify this for me?
<?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>
Using where my XSLT is:
<?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
Code using the if statemtent
<?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
Aucun commentaire:
Enregistrer un commentaire