Xml 如何使用;如果;在字符的xslt中?
我正在编写一个XSLT代码来格式化Sample.xml(见下文) 我试图做的是,在Sample.xslt文件中,我检查type=mp4并显示名称(在本例中,应该显示的名称是RequiredFileName。简而言之,我希望当我将xslt应用于XML文件时,我得到我的mp4文件的名称 Sample.xmlXml 如何使用;如果;在字符的xslt中?,xml,xslt,if-statement,Xml,Xslt,If Statement,我正在编写一个XSLT代码来格式化Sample.xml(见下文) 我试图做的是,在Sample.xslt文件中,我检查type=mp4并显示名称(在本例中,应该显示的名称是RequiredFileName。简而言之,我希望当我将xslt应用于XML文件时,我得到我的mp4文件的名称 Sample.xml <assets> <asset id="01" path="//my_computer/path" name="testfile" type="mov"> <ch
<assets>
<asset id="01" path="//my_computer/path" name="testfile" type="mov">
<child_entries>
<asset id="21" path="//my_computer/test.mp3" name="001-2323" type="mp3"></asset>
<asset id="23" path="//my_computer/test.mp4" name="RequiredFileName" type="mp4"></asset>
<child_entries>
</assets>
Sample.xslt
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Path>
<xsl:for-each select="assets"/>
<xsl:if test="/asset/child_entries/asset/@type = mp4">
<xsl:value-of select="child_entries/asset/@name"/>
</xsl:if>
</Path>
</xsl:template>
谢谢你把它扔了。
你认为你的问题是关于<代码> XSL:如果但实际上不是。这可以做得更直接。请考虑下面的样式表:<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Path>
<xsl:apply-templates select="//asset[@type='mp4']/@path"/>
</Path>
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="assets">
<xsl:for-each select="asset">
<xsl:for-each select="child_entries">
<Path><xsl:value-of select="asset[@type='mp4']/@path" /></Path>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
如果要在输出中为输入中的每个匹配元素指定一个Path
元素,请执行以下操作:
<Path>//my_computer/test.mp3//my_computer/test.mp4</Path>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:apply-templates select="//asset[@type='mp4']" />
</xsl:template>
<xsl:template match="asset">
<Path>
<xsl:value-of select="@path" />
</Path>
</xsl:template>
</xsl:stylesheet>
一如既往,更具体的输出取决于一组可能的输入和您的特定口味
作为结束语,几乎没有必要为每个
使用xsl:for-each。XSLT处理器已经在遍历文档中的节点。这正是XSLT工作原理的核心。最好直接匹配/选择所需内容。您在上面的xml中有几处输入错误。在修复输入错误后,如h作为非闭合元素,我只是添加了一些额外的嵌套循环。正如@lwburk所示,您可以大大简化它。下面是一个工作样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Path>
<xsl:apply-templates select="//asset[@type='mp4']/@path"/>
</Path>
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0' ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="assets">
<xsl:for-each select="asset">
<xsl:for-each select="child_entries">
<Path><xsl:value-of select="asset[@type='mp4']/@path" /></Path>
</xsl:for-each>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
下面是一个工作XML输入文件:
<assets>
<asset id="01" path="//my_computer/path" name="testfile" type="mov">
<child_entries>
<asset id="21" path="//my_computer/test.mp3" name="001-2323"
type="mp3" ></asset>
<asset id="23" path="//my_computer/test.mp4" name="RequiredFileName"
type="mp4" ></asset>
</child_entries>
</asset>
</assets>
这些输出:
<?xml version="1.0" encoding="UTF-8"?><Path>//my_computer/test.mp4</Path>
//我的电脑/test.mp4
我选择path是因为您包含了一个path元素。如果您只需要名称,可以在XSLT文件中替换这一行:
<Path><xsl:value-of select="asset[@type='mp4']/@path" /></Path>
与:
想提到@lwburk解决方案更好,只是想提供一个更接近“让您的代码工作”的解决方案。祝您好运!