XSLT:为什么 <for-each> 不迭代两次
XSLT: why does <for-each> not iterate twice
对于给定的 XSL 和 XML 我只得到一行输出。由于 for-each
有两个序列,我希望有 2 行输出?
我错过了什么?
使用的 XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="pat" as="item()*">
<xsl:sequence select="/myroot[1]/mychild[1]/test[1]/@testing"/>
<xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/>
</xsl:variable>
<xsl:for-each select="$pat">
<xsl:choose>
<xsl:when test=". instance of element()">
<xsl:text> Node: </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Atomic value: </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML 使用:
<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
<myroot >
<mychild id="123">
<fruit>apple</fruit>
<test hello="world" testing="removed" brackets="angled" question="answers"/>
<comment>This is a comment</comment>
</mychild>
<mychild id="789">
<fruit>orange</fruit>
<test brackets="round" hello="greeting">
<number>111</number>
</test>
<!-- this is a test comment -->
<dates>
<modified>123</modified>
<created>880</created>
<accessed>44</accessed>
</dates>
</mychild>
<mychild id="456">
<fruit>banana</fruit>
<comment>This will be removed</comment>
</mychild>
</myroot>
输出如下所示。我收到的只有一行。
为什么第二个序列没有显示任何东西?
<?xml version="1.0" encoding="UTF-8"?>
原子值:移除
我希望它显示如下所示的内容
<?xml version="1.0" encoding="UTF-8"?>
原子值:移除
节点:这是评论
因为 selected 序列中只有一个节点:
/myroot[1]/mychild[1]/test[1]/@testing
selects 1 个节点
/myroot[1]/mychild[2]/comment[1]
selects 0 个节点
对于#2,如果你想select第一个评论第二个[=14=的子节点] 元素而不是第一个 comment
元素 (它不作为第二个 mychild
元素的子元素存在), 然后改变
<xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/>
至
<xsl:sequence select="/myroot[1]/mychild[2]/comment()[1]"/>
对于给定的 XSL 和 XML 我只得到一行输出。由于 for-each
有两个序列,我希望有 2 行输出?
我错过了什么?
使用的 XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<xsl:output indent="yes" />
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:variable name="pat" as="item()*">
<xsl:sequence select="/myroot[1]/mychild[1]/test[1]/@testing"/>
<xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/>
</xsl:variable>
<xsl:for-each select="$pat">
<xsl:choose>
<xsl:when test=". instance of element()">
<xsl:text> Node: </xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>Atomic value: </xsl:text>
</xsl:otherwise>
</xsl:choose>
<xsl:value-of select="."/>
<xsl:text>
</xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
XML 使用:
<?xml version="1.0" encoding="Windows-1252" standalone="no"?>
<myroot >
<mychild id="123">
<fruit>apple</fruit>
<test hello="world" testing="removed" brackets="angled" question="answers"/>
<comment>This is a comment</comment>
</mychild>
<mychild id="789">
<fruit>orange</fruit>
<test brackets="round" hello="greeting">
<number>111</number>
</test>
<!-- this is a test comment -->
<dates>
<modified>123</modified>
<created>880</created>
<accessed>44</accessed>
</dates>
</mychild>
<mychild id="456">
<fruit>banana</fruit>
<comment>This will be removed</comment>
</mychild>
</myroot>
输出如下所示。我收到的只有一行。
为什么第二个序列没有显示任何东西?
<?xml version="1.0" encoding="UTF-8"?>
原子值:移除
我希望它显示如下所示的内容
<?xml version="1.0" encoding="UTF-8"?>
原子值:移除
节点:这是评论
因为 selected 序列中只有一个节点:
/myroot[1]/mychild[1]/test[1]/@testing
selects 1 个节点/myroot[1]/mychild[2]/comment[1]
selects 0 个节点
对于#2,如果你想select第一个评论第二个[=14=的子节点] 元素而不是第一个 comment
元素 (它不作为第二个 mychild
元素的子元素存在), 然后改变
<xsl:sequence select="/myroot[1]/mychild[2]/comment[1]"/>
至
<xsl:sequence select="/myroot[1]/mychild[2]/comment()[1]"/>