清一色 对XSLT来说很新。我可以用一些帮助来理解一些基本的东西。
xml
<root>
<Article>
<Bullettext>10,00 </Bullettext>
<Bullettext>8,00 </Bullettext>
</Article>
<Article>
<something>some text</something>
</Article>
<Article>
<Corpsdetexte>Bulgaria</Corpsdetexte>
<Bullettext>15,0 </Bullettext>
<Bullettext>10,0 </Bullettext>
</Article>
<Article>
<Corpsdetexte>Somaialia</Corpsdetexte>
<bunk>Test</bunk>
<Bullettext>15,1</Bullettext>
<Bullettext>10,2</Bullettext>
<Bullettext>20,3</Bullettext>
<Bullettext>25,4</Bullettext>
<Bullettext>30,5 </Bullettext>
</Article>
</root>
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<!-- copy all elements as is -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<!-- process only the first bullet text element under this template -->
<xsl:template match="Bullettext[not(preceding-sibling::*[1][name()='Bullettext'])]">
<!-- find the element that we want to stop at -->
<xsl:variable name="stop" select="./following-sibling::*[name() != 'Bullettext'][1]"/>
<LIST>
<xsl:choose>
<!-- first, the simple case: there's no element we have to stop at -->
<xsl:when test="not($stop)">
<xsl:apply-templates select="." mode="item"/>
<xsl:apply-templates select="./following-sibling::Bullettext" mode="item"/>
</xsl:when>
<!-- is this required -->
<!-- transform all elements between the start and stop index into items -->
<xsl:otherwise>
<xsl:variable name="start_index" select="count(preceding-sibling::*) + 1"/>
<xsl:variable name="stop_index" select="count($stop/preceding-sibling::*)"/>
<xsl:apply-templates select="../*[position() >= $start_index
and position() <= $stop_index]"
mode="item"/>
</xsl:otherwise>
</xsl:choose>
</LIST>
</xsl:template>
<xsl:template match="Bullettext" />
<xsl:template match="Bullettext" mode="item">
<ITEM>
<xsl:value-of select="."/>
</ITEM>
</xsl:template>
</xsl:stylesheet>
XSLT 2 _注意缺少&#39;否则在选择中。
<xsl:stylesheet
version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<!-- copy all elements as is -->
<xsl:template match="node()">
<xsl:copy>
<xsl:apply-templates select="node()"/>
</xsl:copy>
</xsl:template>
<!-- process only the first bullet text element under this template -->
<xsl:template match="Bullettext[not(preceding-sibling::*[1][name()='Bullettext'])]">
<!-- find the element that we want to stop at -->
<xsl:variable name="stop" select="./following-sibling::*[name() != 'Bullettext'][1]"/>
<LIST>
<xsl:choose>
<!-- first, the simple case: there's no element we have to stop at -->
<xsl:when test="not($stop)">
<xsl:apply-templates select="." mode="item"/>
<xsl:apply-templates select="./following-sibling::Bullettext" mode="item"/>
</xsl:when>
<!-- is this required -->
<!-- transform all elements between the start and stop index into items -->
</xsl:choose>
</LIST>
</xsl:template>
<xsl:template match="Bullettext" />
<xsl:template match="Bullettext" mode="item">
<ITEM>
<xsl:value-of select="."/>
</ITEM>
</xsl:template>
</xsl:stylesheet>
两者都给出相同的结果
<root>
<Article>
<LIST>
<ITEM>10,00 </ITEM>
<ITEM>8,00 </ITEM>
</LIST>
</Article>
<Article>
<something>some text</something>
</Article>
<Article>
<Corpsdetexte>Bulgaria</Corpsdetexte>
<LIST>
<ITEM>15,0 </ITEM>
<ITEM>10,0 </ITEM>
</LIST>
</Article>
<Article>
<Corpsdetexte>Somaialia</Corpsdetexte>
<bunk>Test</bunk>
<LIST>
<ITEM>15,1 </ITEM>
<ITEM>10,2 </ITEM>
<ITEM>20,3 </ITEM>
<ITEM>25,4 </ITEM>
<ITEM>30,5 </ITEM>
</LIST>
</Article>
</root>
示例中的其他值是多少。为什么没有其他方法呢?
注意:这是对提出的问题的回答。抱歉没有链接
答案 0 :(得分:1)
您的输入不包含任何Bullettext
个节点。因此,匹配Bullettext
的三个模板永远不会应用,并且它们包含的内容没有区别。
然而,输入如:
<root>
<Article>
<Bullettext>alpha</Bullettext>
<Bullettext>bravo</Bullettext>
<text>charlie</text>
<Bullettext>delta</Bullettext>
</Article>
</root>
你会看到一个区别:第一个XSLT将返回:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Article>
<LIST>
<ITEM>alpha</ITEM>
<ITEM>bravo</ITEM>
</LIST>
<text>charlie</text>
<LIST>
<ITEM>delta</ITEM>
</LIST>
</Article>
</root>
而第二个只会产生:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Article>
<LIST/>
<text>charlie</text>
<LIST>
<ITEM>delta</ITEM>
</LIST>
</Article>
</root>