我想通过使用XSLT 1.0属性“标题”将我的xml条目分组来创建一个部分
我的XML文件:
<ProductFeatureRecord Type="Detail">
<Data>
<Feature FeatureID="27219" Description="Myheadline" FeatureType="Headline" ></Feature>
<Feature FeatureID="33751" Description="Myboolean" FeatureType="Boolean" />
<Feature FeatureID="35668" Description="Myvalue" FeatureType="Value" FeatureValue="1" />
<Feature FeatureID="25935" Description="Mygroup" FeatureType="Group" FeatureValue="1" />
<Feature FeatureID="21567" Description="Myvalue" FeatureType="Value" FeatureValue="1" />
<Feature FeatureID="20894" Description="Mygroup" FeatureType="Group" FeatureValue="1" />
<Feature FeatureID="17021" Description="Myboolean" FeatureType="Boolean" />
<Feature FeatureID="26225" Description="Myheadline2" FeatureType="Headline" />
<Feature FeatureID="20591" Description="Myvalue2" FeatureType="Value" FeatureValue="1" />
<Feature FeatureID="31680" Description="Mygroup2" FeatureType="Group" FeatureValue="1" />
<Feature FeatureID="30882" Description="Myvalue2" FeatureType="Value" FeatureValue="1" />
<Feature FeatureID="30883" Description="Myvalue2" FeatureType="Value" FeatureValue="1" />
</Data>
</ProductFeatureRecord>
当前的XSL文件:
<xsl:for-each select="ProductFeatureRecord/Data/Feature">
<xsl:if test="@FeatureType='Headline'">
<section><xsl:text> </xsl:text>
<dl><xsl:text> </xsl:text>
<h2><xsl:value-of select="@Description"></xsl:value-of></h2><xsl:text> </xsl:text>
</dl><xsl:text> </xsl:text>
</section><xsl:text> </xsl:text>
</xsl:if>
<xsl:if test="@FeatureType='Boolean'">
<dt><xsl:value-of select="@Description"></xsl:value-of></dt><xsl:text> </xsl:text><dd>ja</dd><xsl:text> </xsl:text>
</xsl:if>
<xsl:if test="@FeatureType='Value'">
<dt><xsl:value-of select="@Description"></xsl:value-of></dt><xsl:text> </xsl:text><dd><xsl:value-of select="@FeatureValue"></xsl:value-of></dd><xsl:text> </xsl:text>
</xsl:if>
<xsl:if test="@FeatureType='Group'">
<dt><xsl:value-of select="@Description"></xsl:value-of></dt><xsl:text> </xsl:text><dd><xsl:value-of select="@FeatureValue"></xsl:value-of></dd><xsl:text> </xsl:text>
</xsl:if>
</xsl:for-each>
但我的section和dl标签现在关闭不正确:/任何想法如何解决这个以获得此输出?
<section>
<dl>
<h2>Myheadline</h2>
<dt>Myboolean</dt>
<dd>ja</dd>
<dt>Myvalue</dt>
<dd>1</dd>
<dt>Mygroup</dt>
<dd>1</dd>
<dt>Myvalue</dt>
<dd>1</dd>
<dt>Mygroup</dt>
<dd>1</dd>
<dt>Myboolean</dt>
<dd>ja</dd>
<section>
</dl>
</section>
<dl>
<h2>Myheadline2</h2>
<dt>Myvalue2</dt>
<dd>1</dd>
<dt>Mygroup2</dt>
<dd>1</dd>
<dt>Myvalue2</dt>
<dd>1</dd>
<dt>Myvalue2</dt>
<dd>1</dd>
</dl>
</section>
答案 0 :(得分:0)
你的问题令人困惑。您似乎希望对条目进行分组,以便每个Feature
"@FeatureType='Headline'
开始一个新的部分,并包含以下所有功能。如果是这样,请尝试以下方法:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="group" match="Feature[not(@FeatureType='Headline')]" use="generate-id(preceding-sibling::Feature[@FeatureType='Headline'][1])" />
<xsl:template match="Data">
<xsl:for-each select="Feature[@FeatureType='Headline']">
<section>
<dl>
<h2>
<xsl:value-of select="@Description"/>
</h2>
<xsl:apply-templates select="key('group', generate-id())"/>
</dl>
</section>
</xsl:for-each>
</xsl:template>
<xsl:template match="Feature">
<dt>
<xsl:value-of select="@Description"/>
</dt>
<dd>
<xsl:value-of select="@FeatureValue"/>
</dd>
</xsl:template>
<xsl:template match="Feature[@FeatureType='Boolean']">
<dt>
<xsl:value-of select="@Description"/>
</dt>
<dd>ja</dd>
</xsl:template>
</xsl:stylesheet>
应用于以下测试输入:
<ProductFeatureRecord Type="Detail">
<Data>
<Feature FeatureID="27219" Description="Myheadline" FeatureType="Headline" ></Feature>
<Feature FeatureID="33751" Description="MybooleanA" FeatureType="Boolean" />
<Feature FeatureID="35668" Description="Myvalue" FeatureType="Value" FeatureValue="1" />
<Feature FeatureID="25935" Description="Mygroup" FeatureType="Group" FeatureValue="2" />
<Feature FeatureID="21567" Description="Myvalue" FeatureType="Value" FeatureValue="3" />
<Feature FeatureID="20894" Description="Mygroup" FeatureType="Group" FeatureValue="4" />
<Feature FeatureID="17021" Description="MybooleanB" FeatureType="Boolean" />
<Feature FeatureID="26225" Description="Myheadline2" FeatureType="Headline" />
<Feature FeatureID="20591" Description="Myvalue2" FeatureType="Value" FeatureValue="5" />
<Feature FeatureID="31680" Description="Mygroup2" FeatureType="Group" FeatureValue="6" />
<Feature FeatureID="30882" Description="Myvalue2" FeatureType="Value" FeatureValue="7" />
<Feature FeatureID="30883" Description="Myvalue2" FeatureType="Value" FeatureValue="8" />
</Data>
</ProductFeatureRecord>
结果将是:
<section>
<dl>
<h2>Myheadline</h2>
<dt>MybooleanA</dt>
<dd>ja</dd>
<dt>Myvalue</dt>
<dd>1</dd>
<dt>Mygroup</dt>
<dd>2</dd>
<dt>Myvalue</dt>
<dd>3</dd>
<dt>Mygroup</dt>
<dd>4</dd>
<dt>MybooleanB</dt>
<dd>ja</dd>
</dl>
</section>
<section>
<dl>
<h2>Myheadline2</h2>
<dt>Myvalue2</dt>
<dd>5</dd>
<dt>Mygroup2</dt>
<dd>6</dd>
<dt>Myvalue2</dt>
<dd>7</dd>
<dt>Myvalue2</dt>
<dd>8</dd>
</dl>
</section>
注意:这是Muenchian grouping的变体。在了解这一点之前,您需要熟悉该方法。