我有这个xml:
<root>
<row>
<number>1001461</number>
<unit>CAN</unit>
</row>
<row>
<number>1001462</number>
<unit>KG</unit>
</row>
</root>
我的 Xslt:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:param name="formid" select="60202" />
<xsl:template match="@* | node()">
<DocumentElement>
<xsl:apply-templates>
<xsl:with-param name="primarykey" select="position()"/>
</xsl:apply-templates>
</DocumentElement>
</xsl:template>
<xsl:template match="row" name="trow">
<xsl:param name="primarykey"/>
<xsl:for-each select="*">
<SaveDataTable>
<KeyName>
<xsl:value-of select="name()"/>
</KeyName>
<KeyValue>
<xsl:value-of select="."/>
</KeyValue>
<PrimaryKey>
<xsl:value-of select="concat('-',$primarykey)"/>
</PrimaryKey>
<FormId>
<xsl:value-of select="$formid"/>
</FormId>
</SaveDataTable>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
预期输出:(注意前两个<PrimaryKey>
有-1,后两个有-2,这就是我需要的)
<DocumentElement>
<SaveDataTable>
<KeyName>number</KeyName>
<KeyValue>1001461</KeyValue>
<PrimaryKey>-1</PrimaryKey>
<FormId>60202</FormId>
</SaveDataTable>
<SaveDataTable>
<KeyName>unit</KeyName>
<KeyValue>CAN</KeyValue>
<PrimaryKey>-1</PrimaryKey>
<FormId>60202</FormId>
</SaveDataTable>
<SaveDataTable>
<KeyName>number</KeyName>
<KeyValue>1001462</KeyValue>
<PrimaryKey>-2</PrimaryKey>
<FormId>60202</FormId>
</SaveDataTable>
<SaveDataTable>
<KeyName>unit</KeyName>
<KeyValue>KG</KeyValue>
<PrimaryKey>-2</PrimaryKey>
<FormId>60202</FormId>
</SaveDataTable>
</DocumentElement>
基本上对于每个<row>...</row>
应该<PrimaryKey>-1</PrimaryKey>
减 1,但它没有发生。所有<PrimaryKey>
元素的值都是 -1 而不是 -1、-2 等等。
============================================
更新:我有点让它工作,但我不确定它是否有效。
工作 xslt(可能需要改进):
<xsl:stylesheet version="1.0" exclude-result-prefixes="msxsl" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="formid" select="60202"/>
<xsl:template match="@* | node()">
<DocumentElement>
<xsl:for-each select="row">
<xsl:call-template name="trow">
<xsl:with-param name="primarykey" select="position()"/>
</xsl:call-template>
</xsl:for-each>
</DocumentElement>
</xsl:template>
<xsl:template name="trow">
<xsl:param name="primarykey"/>
<xsl:for-each select="*">
<SaveDataTable>
<KeyName>
<xsl:value-of select="name()"/>
</KeyName>
<KeyValue>
<xsl:value-of select="."/>
</KeyValue>
<PrimaryKey>
<xsl:value-of select="concat('-',$primarykey)"/>
</PrimaryKey>
<FormId>
<xsl:value-of select="$formid"/>
</FormId>
</SaveDataTable>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>