简体   繁体   English

在XSLT中复制xml属性

[英]Copy xml attribute in XSLT

I would like to copy the attribute value from input to output. 我想将属性值从输入复制到输出。

Place of attribute in input is: katz-request/request/statebond-buy/payment/amount/@curr 属性在输入中的位置是:katz-request / request / statebond-buy / pay / amount / @ curr

Place of attribute in output is: katz-reply/reply/statebond-buy/amount/@curr 属性在输出中的位置是:katz-reply / reply / statebond-buy / amount / @ curr

Here goes input xml 输入XML

<?xml version="1.0" encoding="ISO-8859-2" standalone="yes"?>
<katz-request>
   <request>
      <statebond-buy>
         <isin>PL0000108155</isin>
         <name>DOS0616</name>
         <count>2</count>
         <pay-date>2014-06-18</pay-date>
         <payment>
            <amount curr="PLN">200.00</amount>
         </payment>
      </statebond-buy>
   </request>
</katz-request>

Here goes my transformation 这是我的转型

<?xml version="1.0" encoding="iso-8859-2" standalone="yes"?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="iso-8859-2" indent="yes" standalone="yes"/>
    <xsl:template match="/">
        <katz-reply>
            <reply>
                <statebond-buy>
                    <count>
                        <xsl:value-of select="katz-request/request/statebond-buy/count"/>
                    </count>
                    <amount curr="DEM">
                        <xsl:value-of select="katz-request/request/statebond-buy/payment/amount"/>
                    </amount>
                    <tx-id>843084</tx-id>
                    <day-ref-id>837</day-ref-id>
                    <appl-id>000000000002267</appl-id>
                </statebond-buy>
            </reply>
        </katz-reply>
    </xsl:template>
</xsl:transform>

Here is the result 这是结果

<?xml version="1.0" encoding="iso-8859-2" standalone="yes"?>
<katz-reply>
  <reply>
    <statebond-buy>
      <count>2</count>
      <amount curr="DEM">200.00</amount>
      <tx-id>843084</tx-id>
      <day-ref-id>837</day-ref-id>
      <appl-id>000000000002267</appl-id>
    </statebond-buy>
  </reply>
</katz-reply>

that holds "DEM" instead of "PLN". 持有“ DEM”而不是“ PLN”。

You could make use of "Attribute Value Templates" here, and write out your XML like this 您可以在此处使用“属性值模板”,并像这样写出您的XML

        <amount curr="{katz-request/request/statebond-buy/payment/amount/@curr}">
            <xsl:value-of select="katz-request/request/statebond-buy/payment/amount"/>
        </amount>

The curly braces indicate an expression to be evaluated, not output literally. 花括号指示要计算的表达式,而不是按字面值输出。

But much better would be to re-write your XSLT to use the XSLT Identity Template , and then it is just a case of copying across whole nodes. 但是更好的办法是重新编写XSLT以使用XSLT 身份模板 ,然后这只是跨整个节点复制的一种情况。

Try this XSLT 试试这个XSLT

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="iso-8859-2" indent="yes" standalone="yes"/>

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

    <xsl:template match="statebond-buy">
        <statebond-buy>
            <xsl:apply-templates select="count" />
            <xsl:apply-templates select="payment/amount" />
            <tx-id>843084</tx-id>
            <day-ref-id>837</day-ref-id>
            <appl-id>000000000002267</appl-id>
        </statebond-buy>
    </xsl:template>
</xsl:stylesheet>

How about a simple one: 一个简单的怎么样?

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="iso-8859-2" indent="yes"/>

<xsl:template match="/">
    <katz-reply>
        <reply>
            <xsl:for-each select="katz-request/request/statebond-buy">
                <statebond-buy>
                    <xsl:copy-of select="count | payment/amount"/>
                    <tx-id>843084</tx-id>
                    <day-ref-id>837</day-ref-id>
                    <appl-id>000000000002267</appl-id>
                </statebond-buy>
            </xsl:for-each>             
        </reply>
    </katz-reply>
</xsl:template>

</xsl:stylesheet>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM