Xslt使用其他标记的值创建新子项

时间:2017-02-08 20:39:32

标签: xslt

我想将新的子标记添加到名称为A2
的OPO标记中 连接值形成标签Cod和A1。

例如这个xml

<OPO>
     <Cod>12345</Cod>
     <A1>Anders</A1>
</OPO>

我想要

 <OPO>
       Cod>12345</Cod>
       <A1>Anders</A1>
       <A2>12345 Anders</A2>
 </OPO>

怎么可能呢?

原始xml是

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:wsa="http://www.w3.org/2005/08/addressing"  xmlns:aaa="http://xxx/">
 <soap:Header>
  <MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:4d6b87d8-fe14-4579-ac34-fe841c184a4b</MessageID>
                  <RelatesTo RelationshipType="Reply" xmlns="http://www.w3.org/2005/08/addressing">uuid:1f9b0c7e-f36c-4fa3-ac2b-2377b57b6634</RelatesTo>
  <Action xmlns="http://www.w3.org/2005/08/addressing">http://xxx</Action>
</soap:Header>
<soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                  <OP1 xmlns="http://xxx/">
                     <OPR>
                        <OPO>
                           <Cod>..</Cod>
                           <A1>hi my...</A1>

                        </OPO>
                     </OPR>
                  </OP1>
               </soap:Body>
            </soap:Envelope>

2 个答案:

答案 0 :(得分:0)

解决方案

<xsl:template match="aaa:OPO">
    <xsl:copy>
      <xsl:apply-templates/>
       <xsl:element name="A2" namespace="http://xxx/">
          <xsl:value-of select = "concat(aaa:Cod, '  ', aaa:A1)" />
       </xsl:element>
    </xsl:copy>
</xsl:template> 
<xsl:strip-space elements="*"/><!--remove space for the removed tags-->

答案 1 :(得分:0)

如果您有这样的输入XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
 xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:aaa="http://xxx/">
    <soap:Header>
        <MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:4d6b87d8-fe14-4579-ac34-fe841c184a4b</MessageID>
        <RelatesTo RelationshipType="Reply" xmlns="http://www.w3.org/2005/08/addressing">uuid:1f9b0c7e-f36c-4fa3-ac2b-2377b57b6634</RelatesTo>
        <Action xmlns="http://www.w3.org/2005/08/addressing">http://xxx</Action>
    </soap:Header>
    <soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <OP1 xmlns="http://xxx/">
            <OPR>
                <OPO>
                    <Cod>12345</Cod>
                    <A1>Anders</A1>
                </OPO>
            </OPR>
        </OP1>
    </soap:Body>
</soap:Envelope>

像这样的XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:aaa="http://xxx/">
    <xsl:output method="xml" indent="yes" encoding="utf-8" />

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

    <xsl:template match="aaa:OPO">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*" />
            <xsl:element name="A2" namespace="{namespace-uri()}">
                <xsl:value-of select="concat(aaa:Cod, ' ', aaa:A1)" />
            </xsl:element>
        </xsl:copy>
    </xsl:template> 

</xsl:stylesheet>

将为您提供所需的输出

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing" xmlns:aaa="http://xxx/">
    <soap:Header>
        <MessageID xmlns="http://www.w3.org/2005/08/addressing">uuid:4d6b87d8-fe14-4579-ac34-fe841c184a4b</MessageID>
        <RelatesTo xmlns="http://www.w3.org/2005/08/addressing" RelationshipType="Reply">uuid:1f9b0c7e-f36c-4fa3-ac2b-2377b57b6634</RelatesTo>
        <Action xmlns="http://www.w3.org/2005/08/addressing">http://xxx</Action>
    </soap:Header>
    <soap:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <OP1 xmlns="http://xxx/">
            <OPR>
                <OPO>
                    <Cod>12345</Cod>
                    <A1>Anders</A1>
                    <A2>12345 Anders</A2>
                </OPO>
            </OPR>
        </OP1>
    </soap:Body>
</soap:Envelope>

P.S。:我昨天在磁盘上有完整的源文件; - )