0

我不是 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 的段下的元素值。虽然我可以使用我的内部应用程序来完成,但这似乎非常耗时。以前有人遇到过这个要求吗?在此先感谢您的帮助。

4

1 回答 1

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>
于 2012-12-17T16:09:35.787 回答