Xml XSLT转换-在通过一组节点的迭代过程中设置变量
我的任务是创建XSLT转换。 在过程语言中,解决方案如下所示:Xml XSLT转换-在通过一组节点的迭代过程中设置变量,xml,xslt,Xml,Xslt,我的任务是创建XSLT转换。 在过程语言中,解决方案如下所示: foreach( Book book in set) { int importanceIndicator = 0; foreach( Page page in book ) { if(page.name == "important_page") { importanceIndicator = 1; break; } } book.n
foreach( Book book in set)
{
int importanceIndicator = 0;
foreach( Page page in book )
{
if(page.name == "important_page")
{
importanceIndicator = 1;
break;
}
}
book.newField.importanceIndicator = importanceIndicator;
}
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<set>
<xsl:for-each select="set">
<xsl:for-each select="book">
<book>
<xsl:variable name="importanceIndicatorTmp">
<xsl:choose>
<xsl:when test= "page/@name = 'important_page'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<newField importanceIndicator="{$importanceIndicatorTmp}"></newField>
</book>
</xsl:for-each>
</xsl:for-each>
</set>
</xsl:template>
</xsl:stylesheet>
如何在XSLT中解决这个问题(XSLT与过程语言非常不同)
示例XML文件:
输入:
<set>
<book>
<page name="important_page"></page>
<page name="first_page"></page>
<page name="important_page"></page>
<page name="second_page"></page>
<page name="important_page"></page>
</book>
<book>
<page name="another_page"></page>
<page name="first_page"></page>
<page name="3_page"></page>
<page name="second_page"></page>
<page name="another_page"></page>
</book>
<book>
<page name="important_page"></page>
<page name="first_page"></page>
<page name="important_page"></page>
<page name="second_page"></page>
<page name="another_page"></page>
</book>
</set>
输出:
<set>
<book>
<newField importance_indicator="1"></newField>
</book>
<book>
<newField importance_indicator="0"></newField>
</book>
<book>
<newField importance_indicator="1"></newField>
</book>
</set>
在XSLT中,不需要循环来完成这一点。处理book节点时,可以检查其子节点的内容
<xsl:template match="book">
<newfield>
<xsl:choose>
<xsl:when test="page/@name='important page'">
<xsl:attribute name="importance_indicator" select="1">
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="importance_indicator" select="0">
</xsl:otherwise>
在XSLT中,不需要循环来完成这一点。处理book节点时,可以检查其子节点的内容
<xsl:template match="book">
<newfield>
<xsl:choose>
<xsl:when test="page/@name='important page'">
<xsl:attribute name="importance_indicator" select="1">
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="importance_indicator" select="0">
</xsl:otherwise>
首先从开始(在本例中,它将只复制集
节点)
要创建属性,可以将xsl:attribute
与包含要检查的条件的xsl:choose
一起使用
<xsl:attribute name="importance_indicator">
<xsl:choose>
<xsl:when test="page[@name='important_page']">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
因此page[@name='important\u page'])
将选择页面,如果它是“重要页面”boolean()
将返回true或false,具体取决于该页是否存在,然后number将“true”转换为“1”或“false”转换为“0”。首先从开始(在这种情况下,它将只复制集
节点)
要创建属性,可以将xsl:attribute
与包含要检查的条件的xsl:choose
一起使用
<xsl:attribute name="importance_indicator">
<xsl:choose>
<xsl:when test="page[@name='important_page']">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
因此page[@name='important\u page'])
将选择页面,如果它是“重要页面”boolean()
将根据页面是否存在返回true或false,然后数字将“true”转换为“1”或“false”转换为“0”。我找到了答案,可以这样做:
foreach( Book book in set)
{
int importanceIndicator = 0;
foreach( Page page in book )
{
if(page.name == "important_page")
{
importanceIndicator = 1;
break;
}
}
book.newField.importanceIndicator = importanceIndicator;
}
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<set>
<xsl:for-each select="set">
<xsl:for-each select="book">
<book>
<xsl:variable name="importanceIndicatorTmp">
<xsl:choose>
<xsl:when test= "page/@name = 'important_page'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<newField importanceIndicator="{$importanceIndicatorTmp}"></newField>
</book>
</xsl:for-each>
</xsl:for-each>
</set>
</xsl:template>
</xsl:stylesheet>
1.
0
我找到了答案,可以这样做:
foreach( Book book in set)
{
int importanceIndicator = 0;
foreach( Page page in book )
{
if(page.name == "important_page")
{
importanceIndicator = 1;
break;
}
}
book.newField.importanceIndicator = importanceIndicator;
}
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<set>
<xsl:for-each select="set">
<xsl:for-each select="book">
<book>
<xsl:variable name="importanceIndicatorTmp">
<xsl:choose>
<xsl:when test= "page/@name = 'important_page'">1</xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<newField importanceIndicator="{$importanceIndicatorTmp}"></newField>
</book>
</xsl:for-each>
</xsl:for-each>
</set>
</xsl:template>
</xsl:stylesheet>
1.
0
在XSLT中,您不需要一个循环来实现这一点。Template match=“book”已在所有book元素上循环。在XSLT中,不需要循环即可完成此操作。Template match=“book”已经在所有书籍元素上循环。事实上,当你用功能术语思考时,最好尽量避免使用“期间”这样的术语,因为它意味着一个时间维度。事实上,当你用功能术语思考时,最好尽量避免使用“期间”这样的术语,因为它意味着一个时间维度。