[英]XSLT - Concatenate Child Values
我有一個示例XML,如下所示:
<Record id="1">
<Field id="1" name="Field1">
<ListValues>
<ListValue id="1" displayName="Bank">Bank</ListValue>
<ListValue id="2" displayName="Personal">Personal</ListValue>
</ListValues>
</Field>
<Field id="2" name="Field2"/>
</Record>
我需要結合所有ID在我的輸出XML中創建一個Key Field,如下所示:
<Record>
<KeyField>1-1-12</KeyField>
</Record>
此關鍵字段是“記錄ID”,“字段ID”(始終為“字段1”)和“字段1”的子級的組合。 這些子項(ListValue)可以具有100-150個以上的值,我需要將它們組合起來以形成我的鍵字段(使用定界符是可選的)。
目前,我以這種方式執行相同的操作:
<KeyField>
<xsl:value-of select="concat(../Record/@id,'-',Field[@name='Field1']/@id,'-', concat(Field[@name='Field1']/ListValues/ListValue/@id,Field[@name='Field1']/ListValues/ListValue[2]/@id, and so on..))"/>
</KeyField>
問題是,如果我有100-150個這樣的值,我將無法繼續在KeyField元素中添加這么多的值。 有沒有一種方法可以預先計算出來並僅在關鍵字段元素中使用它,我又如何遍歷所有這些值?
我正在使用XSL 1.0。
以下是帶有命名模板的XSL 1.0解決方案,可以調用該模板來檢索每個記錄的關鍵字段。
使用的XML:
<Record id="1">
<Field id="1" name="Field1">
<ListValues>
<ListValue id="1" displayName="Bank">Bank</ListValue>
<ListValue id="2" displayName="Personal">Personal</ListValue>
<ListValue id="3" displayName="Personal">Personal</ListValue>
<ListValue id="4" displayName="Personal">Personal</ListValue>
<ListValue id="5" displayName="Personal">Personal</ListValue>
<ListValue id="6" displayName="Personal">Personal</ListValue>
<ListValue id="7" displayName="Personal">Personal</ListValue>
<ListValue id="8" displayName="Personal">Personal</ListValue>
</ListValues>
</Field>
<Field id="2" name="Field2"/>
</Record>
使用的XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:template match="/">
<xsl:for-each select="/Record">
<xsl:element name="Record">
<xsl:call-template name="GetKeyField">
<xsl:with-param name="record" select="current()" />
</xsl:call-template>
</xsl:element>
</xsl:for-each>
</xsl:template>
<xsl:template name="GetKeyField">
<xsl:param name="record" />
<xsl:variable name="recordId" select="$record/@id" />
<xsl:variable name="fieldId" select="$record/Field[@name='Field1']/@id" />
<xsl:variable name="listValueIds">
<xsl:for-each select="$record/Field[@name='Field1']//ListValue">
<xsl:value-of select="@id" />
</xsl:for-each>
</xsl:variable>
<xsl:element name="KeyField">
<xsl:value-of select="normalize-space(concat($recordId, '-', $fieldId, '-', $listValueIds))" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
結果:
<?xml version="1.0" encoding="UTF-8"?>
<Record><KeyField>1-1-12345678</KeyField></Record>
未經測試:
<xsl:template match="Record">
<xsl:element name="Keyfield">
<xsl:value-of select="@id" />-<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="Field">
<xsl:value-of select="@id" />-
</xsl:template>
<xsl:template match="ListValues><xsl:apply-templates /></xsl:template>
<xsl:template match="ListValue><xsl:value-of select="@id" /></xsl:template>
剛剛輸入! 不好意思!
好的,因此我在XSLT 2.0中找到了解決方案:
我創建了一個變量:
<xsl:variable name="Key_NewField">
<xsl:value-of select="Field[@name='Field1']/ListValues/ListValue[position() <= 150]/@id" />
</xsl:variable>
然后在最后一個串聯中使用<KeyField>
元素中的此變量($ Key_NewField)。
這給了我輸出(期望):
<Record>
<KeyField>1-1-1 2</KeyField>
</Record>
對於XSLT(1.0):
<xsl:variable name="Key_NewField">
<xsl:for-each select="Field[@name = 'Field1']/ListValues/ListValue">
<xsl:value-of select="@id"/>
</xsl:for-each>
</xsl:variable>
這給了我以下輸出:
<Record>
<KeyField>1-1-12</KeyField>
</Record>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.