以下代碼作業正常,但我想簡化它。在這些示例中,我有 11 種貨幣面額(為了簡化問題,我只在此處包含了四種面額),并且我想要一種方法來簡化此代碼,而不必將該行重復thousand or hundred or fifty ... or one三遍。在我看來可以使用它來完成,<xsl:key但我還沒有找到方法。
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="test">
<root>
<other1>
</other1>
<currency g="1">
<sth1>A</sth1>
<thousand>1</thousand>
<hundred>2</hundred>
<fifty>4</fifty>
<one>10</one>
<sth2>C</sth2>
</currency>
<currency g="2">
<sth1>A</sth1>
<sth2>C</sth2>
</currency>
<currency g="3">
<sth1>A</sth1>
<thousand>1</thousand>
<hundred>2</hundred>
<fifty>4</fifty>
<one>10</one>
<sth2>C</sth2>
</currency>
<other2>
</other2>
</root>
</xsl:variable>
<xsl:variable xmlns:xalan="http://xml.apache.org/xalan" name="curr" select="xalan:nodeset($test)/root"/>
<xsl:template match="/">
<root>
<xsl:call-template name="currency"/>
</root>
</xsl:template>
<xsl:template name="currency">
<xsl:for-each select="$curr/currency[thousand or hundred or fifty or one]">
<xsl:if test="position() mod 3 = 1">
<xsl:variable name="nextSibling1" select="following-sibling::currency[thousand or hundred or fifty or one][1]"/>
<xsl:variable name="nextSibling2" select="following-sibling::currency[thousand or hundred or fifty or one][2]"/>
<col1>
<xsl:apply-templates select="." mode="total"/>
</col1>
<col2>
<xsl:apply-templates select="$nextSibling1" mode="total"/>
</col2>
<col3>
<xsl:apply-templates select="$nextSibling2" mode="total"/>
</col3>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="currency" mode="total">
<xsl:value-of select="thousand*1000 hundred*100 fifty*50 one"/>
</xsl:template>
<xsl:template match="*"/>
</xsl:stylesheet>
這是輸出:
<root>
<col1>1410</col1>
<col2>1410</col2>
<col3/>
</root>
uj5u.com熱心網友回復:
我仍然不確定您的代碼究竟應該完成什么。如果您不想重復[thousand or hundred or fifty or one]謂詞,那么可以嘗試類似
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="test">
<root>
<other1>
</other1>
<currency g="1">
<sth1>A</sth1>
<thousand>1</thousand>
<hundred>2</hundred>
<fifty>4</fifty>
<one>10</one>
<sth2>C</sth2>
</currency>
<currency g="2">
<sth1>A</sth1>
<sth2>C</sth2>
</currency>
<currency g="3">
<sth1>A</sth1>
<thousand>1</thousand>
<hundred>2</hundred>
<fifty>4</fifty>
<one>10</one>
<sth2>C</sth2>
</currency>
<other2>
</other2>
</root>
</xsl:variable>
<xsl:variable xmlns:xalan="http://xml.apache.org/xalan" name="curr" select="xalan:nodeset($test)/root/currency[thousand or hundred or fifty or one]"/>
<xsl:template match="/">
<root>
<xsl:for-each select="$curr">
<xsl:variable name="i" select="position()" />
<xsl:if test="$i mod 3 = 1">
<col1 >
<xsl:apply-templates select="." mode="total"/>
</col1>
<col2>
<xsl:apply-templates select="$curr[$i 1]" mode="total"/>
</col2>
<col3>
<xsl:apply-templates select="$curr[$i 2]" mode="total"/>
</col3>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template match="currency" mode="total">
<xsl:value-of select="thousand*1000 hundred*100 fifty*50 one"/>
</xsl:template>
</xsl:stylesheet>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373598.html
上一篇:驗證兩個物件XSD之間的關系
下一篇:使用C#將PT0S讀取到0秒
