0

我有以下 XML 并希望将 XML 属性的值code="MA"<FullNameVerifiesToAddress>node 和<FullNameVerifiesToSSN>node 获取到 node<Summary>中。

<PreciseIDServer>
    <Header>
        <ReportDate>09042018</ReportDate>
        <ReportTime>235641</ReportTime>
    </Header>
    <Summary>
        <TransactionID>1421957889</TransactionID>
        <InitialDecision>ACC</InitialDecision>
        <FinalDecision>ACC</FinalDecision>
        <CrossReferenceIndicatorsGrid>
            <FullNameVerifiesToAddress code="MA"/>
            <FullNameVerifiesToSSN code="MA"/>
        </CrossReferenceIndicatorsGrid>
    </Summary>
</PreciseIDServer>

我现在使用以下 XSLT 将<ReportTime>from<Header>节点放入<summary>,但我还需要 Summary 节点中的上述属性。

<xsl:template match="Summary">
    <xsl:copy>
        <xsl:apply-templates select="@* | ancestor::PreciseIDServer/Header/ReportTime | node()"/>
    </xsl:copy>
</xsl:template>

我想要作为 OUTPUT 的 XML 应该类似于

<PreciseIDServer>
    <Header>
        <ReportDate>09042018</ReportDate>
        <ReportTime>235641</ReportTime>
    </Header>
    <Summary>
        <TransactionID>1421957889</TransactionID>
        <InitialDecision>ACC</InitialDecision>
        <FinalDecision>ACC</FinalDecision>
        <ReportTime>235641</ReportTime>
        <FullNameVerifiesToAddress>MA </FullNameVerifiesToAddress>
        <FullNameVerifiesToSSN> MA </FullNameVerifiesToSSN>
        <CrossReferenceIndicatorsGrid>
            <FullNameVerifiesToAddress code="MA"/>
            <FullNameVerifiesToSSN code="MA"/>
        </CrossReferenceIndicatorsGrid>
    </Summary>
</PreciseIDServer>
4

1 回答 1

0

我认为您需要通过第二种模式推送一些节点,因为您想在未复制的同时输出它们,同时还想转换它们,因此最小的 XSLT 3 解决方案是

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    expand-text="yes"
    version="3.0">

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:output method="xml" indent="yes"/>
  <xsl:strip-space elements="*"/>

    <xsl:template match="Summary">
        <xsl:copy>
            <xsl:apply-templates select="@* | ancestor::PreciseIDServer/Header/ReportTime | node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="CrossReferenceIndicatorsGrid">
        <xsl:apply-templates mode="code-att-to-content"/>
        <xsl:next-match/>
    </xsl:template>

    <xsl:template match="CrossReferenceIndicatorsGrid/*" mode="code-att-to-content">
        <xsl:copy>{@code}</xsl:copy>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bdxtqL

对于 XSLT 2,您需要将声明替换xsl:mode为身份转换模板

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

以及带有. _ {@code}<xsl:value-of select="@code"/>

对于 XSLT 1,您需要与 XSLT 2 相同的更改,但还需要在身份模板上添加名称(即更改<xsl:template match="@* | node()"><xsl:template match="@* | node()" name="identity">)并替换使用<xsl:next-match/>with <xsl:call-template name="identity"/>

于 2018-10-04T06:24:28.027 回答