如何在 xslt 中模板内的属性中使用 if 条件(或 when )
大家好,
如何使用属性的 if 条件在每次循环时打印不同的值。 我有一个名为“AttributeName”的属性。每次循环(共5次)时应使用不同的名称。
我尝试了一些事情..但没有成功。我的输入 XML 不会有任何相关数据。我所要做的就是当循环进行到 5 次时每次打印不同的值。
输入 XML:
<?xml version="1.0" encoding="UTF-8"?>
</Solution>
<Solution>
<ID>22060000000000000000000002</ID>
<Title>ABCD</Title>
<Observations>
<Observation>DEF</Observation>
</Observations>
<ProblemDescription> 1234</ProblemDescription>
<ProblemCause>ADDD</ProblemCause>
<RepairProcedures>
<RepairProcedure>XYZ</RepairProcedure>
</RepairProcedures>
<ScenarioExplanation>
<Scenario>JIJIJIJIJI</Scenario>
<Scenario>SCENARIO1.</Scenario>
</ScenarioExplanation>
<DocumentReferences>
<DocRef>NO DATA</DocRef>
</DocumentReferences>
</Solution>
</Solutions>
XSLT:
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:stylesheet><xsl:template>
<xsl:element name="Tables">
<!-- Tables code here -->
</xsl:element>
<xsl:element name="Relationships">
<xsl:for-each select="Solutions/Solution">
<xsl:if test="RepairProcedures/RepairProcedure!= '' ">
<xsl:for-each select="RepairProcedures/RepairProcedure">
<xsl:variable name="vNumCols" select="5"/>
<xsl:element name="Relationship">
<xsl:for-each select="position() < vNumCols">
<xsl:attribute name="Action"><xsl:value-of select="'Insert'"/></xsl:attribute>
<xsl:attribute name="DestinationKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="RelationshipSpec"><xsl:value-of select="'TableName'"/></xsl:attribute>
<xsl:attribute name="SourceKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="RelCommonKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="AttributeName">
<xsl:if test="position() = 1"><xsl:value-of select="CPComments"/></xsl:if>
<xsl:if test="position() = 2"><xsl:value-of select="CPConflict"/></xsl:if>
</xsl:attribute>
<xsl:attribute name="AttributeValue"><xsl:value-of select="1"/></xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
我循环通过 Repairprocedures/repairprocedure 是为了获取“RelationshipSpec”文本。
预期输出
<Relationships>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPComments" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPConflict" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPUserID" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProcessID" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProductId" AttributeValue=""/>
</Relationships>
这 5 行中唯一更改的值是“AttributeName”。我正在尝试为 AttributeName 不同的一个关系打印 5 行。
请帮我做这件事。
我正在使用 XSLT 1.0
谢谢, 拉姆
HI All,
How to use the if condition for an attribute to print different value when its looped everytime.
I have an attribute called "AttributeName". This shall take different name when it is looped for each time (total 5 times).
I tried few things.. but it didnt work out. My Input XML will not have any data for this. All I have to do is to print different value each time when the loop goes till 5 times.
Input XML:
<?xml version="1.0" encoding="UTF-8"?>
</Solution>
<Solution>
<ID>22060000000000000000000002</ID>
<Title>ABCD</Title>
<Observations>
<Observation>DEF</Observation>
</Observations>
<ProblemDescription> 1234</ProblemDescription>
<ProblemCause>ADDD</ProblemCause>
<RepairProcedures>
<RepairProcedure>XYZ</RepairProcedure>
</RepairProcedures>
<ScenarioExplanation>
<Scenario>JIJIJIJIJI</Scenario>
<Scenario>SCENARIO1.</Scenario>
</ScenarioExplanation>
<DocumentReferences>
<DocRef>NO DATA</DocRef>
</DocumentReferences>
</Solution>
</Solutions>
XSLT:
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:stylesheet><xsl:template>
<xsl:element name="Tables">
<!-- Tables code here -->
</xsl:element>
<xsl:element name="Relationships">
<xsl:for-each select="Solutions/Solution">
<xsl:if test="RepairProcedures/RepairProcedure!= '' ">
<xsl:for-each select="RepairProcedures/RepairProcedure">
<xsl:variable name="vNumCols" select="5"/>
<xsl:element name="Relationship">
<xsl:for-each select="position() < vNumCols">
<xsl:attribute name="Action"><xsl:value-of select="'Insert'"/></xsl:attribute>
<xsl:attribute name="DestinationKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="RelationshipSpec"><xsl:value-of select="'TableName'"/></xsl:attribute>
<xsl:attribute name="SourceKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="RelCommonKey"><xsl:value-of select="1"/></xsl:attribute>
<xsl:attribute name="AttributeName">
<xsl:if test="position() = 1"><xsl:value-of select="CPComments"/></xsl:if>
<xsl:if test="position() = 2"><xsl:value-of select="CPConflict"/></xsl:if>
</xsl:attribute>
<xsl:attribute name="AttributeValue"><xsl:value-of select="1"/></xsl:attribute>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
I am looping through repairprocedures/repairprocedure is to get the "RelationshipSpec" text.
Expected output
<Relationships>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPComments" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPConflict" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="CPUserID" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProcessID" AttributeValue=""/>
<Relationship Action="Delete" DestinationKey="1" RelationshipSpec="XYZ" SourceKey="2" RelCommonKey="1_2" AttributeName="ProductId" AttributeValue=""/>
</Relationships>
The only value changed in these 5 rows is "AttributeName". I am trying to print 5 rows for one relation with AttributeName being different.
Please help me in doing this.
I am using XSLT 1.0
Thank you,
Ramm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道没有类似以下内容的东西:
工作吗?
我可能更喜欢
,但无论如何它也不会更短。Doesn't something along the lines of:
work?
I would probably prefer
<xsl:choose>
, but it's not like it would have been shorter anyway.