我正在JMeter中创建一些性能测试,我正在努力使测试脚本尽可能可配置。例如,我使用这个XML:
<Party>
<Id>123456</Id>
</Party>
<Agreement>
<InternalAgreement>
<Id>2508153801</Id>
<AgreementType>UYTU</AgreementType>
<AgreementTypeCombination>ULLL</AgreementTypeCombination>
<ContractType>3</ContractType>
<IdCombination>250851536</IdCombination>
<ProductCode>A260</ProductCode>
</InternalAgreement>
</Agreement>
<PartyAgreementRole>
<PartyInternalAgreementRole>
<PartyAgreementRoleType>AWS</PartyAgreementRoleType>
<RoleTypeSequenceNumber>054</RoleTypeSequenceNumber>
<EndDate>2016-11-28</EndDate>
</PartyInternalAgreementRole>
</PartyAgreementRole>
我想使用xslt将上面的xml转换为以下内容(提及不同的Id):
<Party>
<Id>${Id_1}</Id>
</Party>
<Agreement>
<InternalAgreement>
<Id>${Id_2}</Id>
<AgreementType>${AgreementType}</AgreementType>
<AgreementTypeCombination>${AgreementTypeCombination}</AgreementTypeCombination>
<ContractType>${ContractType}</ContractType>
<IdCombination>${IdCombination}</IdCombination>
<ProductCode>${ProductCode}</ProductCode>
</InternalAgreement>
</Agreement>
<PartyAgreementRole>
<PartyInternalAgreementRole>
<PartyAgreementRoleType>${PartyAgreementRoleType}</PartyAgreementRoleType>
<RoleTypeSequenceNumber>${RoleTypeSequenceNumber}</RoleTypeSequenceNumber>
<EndDate>${EndDate}</EndDate>
</PartyInternalAgreementRole>
</PartyAgreementRole>
到目前为止,我还没有设法在xslt中创建一个通用的解决方案。到目前为止,我想出了:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="node()/text()[.='VN']">${PartyAgreementRoleType}</xsl:template>
但这远不是解决方案。请问有人指导我正确的方向吗?
答案 0 :(得分:1)
我不理解您的代码中的<xsl:template match="node()/text()[.='VN']">${PartyAgreementRoleType}</xsl:template>
,因为您的示例输入中似乎没有VN
。
但是,如果要填充Id
个元素,那么
<xsl:template match="Id">
<xsl:copy>
<xsl:text>${Id_</xsl:text>
<xsl:number level="any"/>
<xsl:text>}</xsl:text>
</xsl:copy>
</xsl:template>
应该有效,如果要用其名称填充其他元素,那么
<xsl:template match="*[not(*)]">
<xsl:copy>
<xsl:text>${</xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text>}</xsl:text>
</xsl:copy>
</xsl:template>
应该实现这一点,所以将这些模板放在一起就可以了
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[not(*)]">
<xsl:copy>
<xsl:text>${</xsl:text>
<xsl:value-of select="local-name()"/>
<xsl:text>}</xsl:text>
</xsl:copy>
</xsl:template>
<xsl:template match="Id" priority="5">
<xsl:copy>
<xsl:text>${Id_</xsl:text>
<xsl:number level="any"/>
<xsl:text>}</xsl:text>
</xsl:copy>
</xsl:template>
</xsl:transform>
转换
<Root>
<Party>
<Id>123456</Id>
</Party>
<Agreement>
<InternalAgreement>
<Id>2508153801</Id>
<AgreementType>UYTU</AgreementType>
<AgreementTypeCombination>ULLL</AgreementTypeCombination>
<ContractType>3</ContractType>
<IdCombination>250851536</IdCombination>
<ProductCode>A260</ProductCode>
</InternalAgreement>
</Agreement>
<PartyAgreementRole>
<PartyInternalAgreementRole>
<PartyAgreementRoleType>AWS</PartyAgreementRoleType>
<RoleTypeSequenceNumber>054</RoleTypeSequenceNumber>
<EndDate>2016-11-28</EndDate>
</PartyInternalAgreementRole>
</PartyAgreementRole>
</Root>
进入
<?xml version="1.0" encoding="UTF-8"?><Root>
<Party>
<Id>${Id_1}</Id>
</Party>
<Agreement>
<InternalAgreement>
<Id>${Id_2}</Id>
<AgreementType>${AgreementType}</AgreementType>
<AgreementTypeCombination>${AgreementTypeCombination}</AgreementTypeCombination>
<ContractType>${ContractType}</ContractType>
<IdCombination>${IdCombination}</IdCombination>
<ProductCode>${ProductCode}</ProductCode>
</InternalAgreement>
</Agreement>
<PartyAgreementRole>
<PartyInternalAgreementRole>
<PartyAgreementRoleType>${PartyAgreementRoleType}</PartyAgreementRoleType>
<RoleTypeSequenceNumber>${RoleTypeSequenceNumber}</RoleTypeSequenceNumber>
<EndDate>${EndDate}</EndDate>
</PartyInternalAgreementRole>
</PartyAgreementRole>
</Root>