XSLT复制特定元素下的所有内容,但更改某些属性的值

时间:2019-03-20 14:53:25

标签: xml xslt

我在xslt问题上苦苦挣扎,我需要将所有内容复制到特定元素下,但是我还需要更改其中一个属性的值。

这是我的XML:

<?xml version="1.0" encoding="UTF-8"?>
<message>
  <datetime datum="20190318" time="154933"/>
  <information environment="A" formula="AA" database_action="I"/>
  <identification versionnumber="1" publication_type="MESSAGE_NAME"/>
  <content>
    <PickWave>
      <Pick pickID="1" groupID="11802182" groupType="orderPick" eachQuantity="1" sourceLocation="P5F.021.01-004" destinationLocation="E1A">
        <Container containerID="11802182">
          <ContainerType>CRATE</ContainerType>
        </Container>
        <Product productID="593000" description="593000-DESCRIPTION" unitOfMeasure="Box" weightUnitOfMeasure="g" dimensionUnitOfMeasure="mm" length="230" width="115,5" height="95" weight="312,1">
          <Identifiers>
            <Identifier label="barcode">
              <AllowedValues>8713776016527</AllowedValues>
            </Identifier>
          </Identifiers>
        </Product>
        <Data>
          <containerType>CRATE</containerType>
        </Data>
      </Pick>
      <Pick pickID="2" groupID="11802182" groupType="orderPick" eachQuantity="1" sourceLocation="P5F.022.01-001" destinationLocation="E1A">
        <Container containerID="11802182">
          <ContainerType>CRATE</ContainerType>
        </Container>
        <Product productID="600318" description="600318-DESCRIPTION" unitOfMeasure="Bag" weightUnitOfMeasure="g" dimensionUnitOfMeasure="mm" length="160" width="160" height="230" weight="26,5">
          <Identifiers>
            <Identifier label="barcode">
              <AllowedValues>8711247936732</AllowedValues>
            </Identifier>
          </Identifiers>
        </Product>
        <Data>
          <containerType>CRATE</containerType>
        </Data>
      </Pick>
    </PickWave>
  </content>
  <error exitcode="" message=""/>
</message>

这是我的XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>
    <xsl:strip-space elements="*"/>

    <xsl:template match="node()|@*">
        <xsl:copy copy-namespaces="no">
            <xsl:message>
                <xsl:value-of select="name(.)"/>
            </xsl:message>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <!-- When attribute matches @weight, @lenght, @width or @height, replace comma (EU) with dots (US) as decimal seperator -->
    <xsl:template match="*:Product/@weight | *:Product/@length | *:Product/@width | *:Product/@height">
        <xsl:variable name="vAttr" select="name(.)"/>
        <xsl:attribute name="{$vAttr}" select="translate(.,',','.')"/>
    </xsl:template> 

</xsl:stylesheet>

我了解到我的第一个模板匹配每个元素/属性,因此创建了<message>直到<content>个元素,但是我如何从PickWave元素中获取它来匹配每个元素和属性呢?这些元素?

这将是我想要的输出:

<?xml version="1.0" encoding="UTF-8"?>
<PickWave>
    <Pick pickID="1" groupID="11802182" groupType="orderPick" eachQuantity="1" sourceLocation="P5F.021.01-004" destinationLocation="E1A">
        <Container containerID="11802182">
            <ContainerType>CRATE</ContainerType>
        </Container>
        <Product productID="593000" description="593000-DESCRIPTION" unitOfMeasure="Box" weightUnitOfMeasure="g" dimensionUnitOfMeasure="mm" length="230" width="115.5" height="95" weight="312.1">
            <Identifiers>
                <Identifier label="barcode">
                    <AllowedValues>8713776016527</AllowedValues>
                </Identifier>
            </Identifiers>
        </Product>
        <Data>
            <containerType>CRATE</containerType>
        </Data>
    </Pick>
    <Pick pickID="2" groupID="11802182" groupType="orderPick" eachQuantity="1" sourceLocation="P5F.022.01-001" destinationLocation="E1A">
        <Container containerID="11802182">
            <ContainerType>CRATE</ContainerType>
        </Container>
        <Product productID="600318" description="600318-DESCRIPTION" unitOfMeasure="Bag" weightUnitOfMeasure="g" dimensionUnitOfMeasure="mm" length="160" width="160" height="230" weight="26.5">
            <Identifiers>
                <Identifier label="barcode">
                    <AllowedValues>8711247936732</AllowedValues>
                </Identifier>
            </Identifiers>
        </Product>
        <Data>
            <containerType>CRATE</containerType>
        </Data>
    </Pick>
</PickWave>

2 个答案:

答案 0 :(得分:1)

如果只希望在输出中使用PickWave元素,则一种解决方案是简单地使模板与文档节点匹配,然后仅选择要复制的元素。...

尝试将此模板添加到现有XSLT

<xsl:template match="/">
    <xsl:apply-templates select="message/content/PickWave" />
</xsl:template>

答案 1 :(得分:1)

  

但是如何获取它以匹配来自   PickWave

如果我说对了,您想更改PickWave元素的 any 后代元素中的那些属性。所以你需要:

<xsl:template match="*:PickWave//@weight
                    |*:PickWave//@lenght
                    |*:PickWave//@width
                    |*:PickWave//@height">
    <xsl:attribute name="{name()}" select="translate(.,',','.')"/>
</xsl:template> 

或者如果您想要更多的XSLT 2.0样式的图案

<xsl:template match="*:PickWave//@*[
                        some $a in ../(@weight|@lenght|@width|@height) 
                        satisfies $a is .
                     ]">
    <xsl:attribute name="{name()}" select="translate(.,',','.')"/>
</xsl:template>