我的XSLT转换遇到了问题。我想将xml从一种形式转换为另一种形式。我的输入XML根标记是。
输入XML:
<ICWRRsp>
<IcwrId>379505</IcwrId>
<IcwrId>379506</IcwrId>
<IcwrId>379507</IcwrId>
<IcwrId>379508</IcwrId>
<IcwrId>379509</IcwrId>
<IcwrId>379510</IcwrId>
<WorkId>1920305</WorkId>
<WorkId>1920475</WorkId>
<WorkId>1920673</WorkId>
<WorkId>1920676</WorkId>
<WorkId>1920717</WorkId>
<WorkId>1920729</WorkId>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>V1</Jurisdiction>
<Jurisdiction>MD</Jurisdiction>
<Jurisdiction>MD</Jurisdiction>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FCC</IcgsWc>
<IcgsWc>0FEN</IcgsWc>
<IcgsWc>0FEN</IcgsWc>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<WcId>0</WcId>
<StatusCode>0</StatusCode>
<StatusDesc>SUCESS</StatusDesc>
</ICWRRsp>
输出XML:
<ICWRRsp>
<ICWR>
<IcwrId>379505</IcwrId>
<WorkId>1920305</WorkId>
<Jurisdiction>V1</Jurisdiction>
<IcgsWc>0FCC</IcgsWc>
<WcId>0</WcId>
</ICWR>
<ICWR>
<IcwrId>379505</IcwrId>
<Jurisdiction>V1</Jurisdiction>
<IcgsWc>0FCC</IcgsWc>
<WcId>0</WcId>
</ICWR>
<StatusCode>0</StatusCode>
<StatusDesc>SUCESS</StatusDesc>
XSLT:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<ICWRRsp>
<xsl:for-each select="ICWRRsp">
<ICWR>
<IcwrId><xsl:value-of select="IcwrId"/></IcwrId>
<WorkId><xsl:value-of select="WorkId"/></WorkId>
<Jurisdiction><xsl:value-of select="Jurisdiction"/></Jurisdiction>
<IcgsWc><xsl:value-of select="IcgsWc"/></IcgsWc>
<WcId><xsl:value-of select="WcId"/></WcId>
</ICWR>
</xsl:for-each>
</ICWRRsp>
</xsl:template>
</xsl:stylesheet>
我已经编写了XSLT,但它没有迭代。我陷入了循环中。我得到以下输出:
<?xml version="1.0" encoding="UTF-8"?>
<ICWRRsp>
<ICWR>
<IcwrId>379505</IcwrId>
<WorkId>1920305</WorkId>
<Jurisdiction>V1</Jurisdiction>
<IcgsWc>0FCC</IcgsWc>
<WcId>0</WcId>
</ICWR>
</ICWRRsp>
有人可以帮我写一下XSLT吗?
答案 0 :(得分:1)
XML中只有一个ICWRRsp
元素。它是根元素,所以你的xsl:for-each
只会做一件事。
看起来每个ICWR
需要一个IcwrId
元素,因此您需要选择IcwrId
元素
<xsl:for-each select="ICWRRsp/IcwrId">
唯一的问题是获取相关元素,这些元素遵循兄弟姐妹,而不是孩子。
为此,首先将当前IcwrId
元素的位置存储在变量中:
<xsl:variable name="pos" select="position()" />
然后,要获取其他元素,请执行此操作,例如......
<WorkId><xsl:value-of select="following-sibling::WorkId[$pos]"/></WorkId>
即。获取与IcwrId
元素
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:template match="/">
<ICWRRsp>
<xsl:for-each select="ICWRRsp/IcwrId">
<xsl:variable name="pos" select="position()" />
<ICWR>
<IcwrId><xsl:value-of select="."/></IcwrId>
<WorkId><xsl:value-of select="following-sibling::WorkId[$pos]"/></WorkId>
<Jurisdiction><xsl:value-of select="following-sibling::Jurisdiction[$pos]"/></Jurisdiction>
<IcgsWc><xsl:value-of select="following-sibling::IcgsWc[$pos]"/></IcgsWc>
<WcId><xsl:value-of select="following-sibling::WcId[$pos]"/></WcId>
</ICWR>
</xsl:for-each>
</ICWRRsp>
</xsl:template>
</xsl:stylesheet>