我不是XSLT专家,在这里寻求帮助。
这是我的XML输入:
<?xml version="1.0" encoding="UTF-8"?>
<Target_Root>
<RecordHeader>
<FileHeader>1234567</FileHeader>
</RecordHeader>
<TransDetails>
<TransHeader>
<FileHeaderRec/>
<BatchHeaderRec/>
<OrderingPartyRec/>
<TransBody>
<TransactionRec/>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii1 </Description>
</_0160>
</DescRec>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii2</Description>
</_0160>
</DescRec>
<NameBenRec/>
<CityBenRec/>
</TransBody>
<TransBody>
<TransactionRec/>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii3 </Description>
</_0160>
</DescRec>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii4</Description>
</_0160>
</DescRec>
<NameBenRec/>
<CityBenRec/>
</TransBody>
</TransHeader>
<BatchTrailerRec/>
<FileTrailerRec/>
</TransDetails>
我的预期输出是:
<?xml version="1.0" encoding="UTF-8"?>
<Target_Root>
<RecordHeader>
<FileHeader>1234567</FileHeader>
</RecordHeader>
<TransDetails>
<TransHeader>
<FileHeaderRec/>
<BatchHeaderRec/>
<OrderingPartyRec/>
<TransBody>
<TransactionRec/>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii1 Srii2</Description>
</_0160>
</DescRec>
<NameBenRec/>
<CityBenRec/>
</TransBody>
<TransBody>
<TransactionRec/>
<DescRec>
<_0160>
<RecordCode>1</RecordCode>
<VariantCode>A</VariantCode>
<Description>Srii3 Srii4</Description>
</_0160>
</DescRec>
<NameBenRec/>
<CityBenRec/>
</TransBody>
</TransHeader>
<BatchTrailerRec/>
<FileTrailerRec/>
</TransDetails>
如图所示,我试图连接属于一个上下文id的段下的元素值。虽然我可以使用我的内部应用程序来完成它,但它似乎非常耗时。以前有人遇到过这个要求吗?在此先感谢您的帮助。
答案 0 :(得分:0)
您可以建立身份变换。请参阅注释以获得解释。
建议 XSLT 1.0 :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<!-- Identity transform -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Do nothing for DescRec elements that have a preceding sibling DescRec -->
<xsl:template match="DescRec[preceding-sibling::DescRec]"/>
<!-- For Description elements, concatenate the content of all the Description elements
within the same TransBody context -->
<xsl:template match="Description">
<xsl:copy>
<xsl:for-each select="ancestor::TransBody//Description">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
对于 XSLT 2.0 :
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<!-- Identity transform -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- Do nothing for DescRec elements that have a preceding sibling DescRec -->
<xsl:template match="DescRec[preceding-sibling::DescRec]"/>
<!-- For Description elements, concatenate the content of all the Description elements
within the same TransBody context -->
<xsl:template match="Description">
<xsl:copy>
<xsl:value-of select="ancestor::TransBody//Description" separator="''"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>