簡體   English   中英

XSL名稱值對轉換

[英]XSL Name Value Pair transformation

我不確定是否有可能,但是確實可以。

通過此XML:

<?xml version="1.0" encoding="UTF-8"?>
<AttributesCollection>
    <Attributes>
        <AttributeName>AAA</AttributeName>
        <AttributeValue>Value1</AttributeValue>
    </Attributes>
    <Attributes>
        <AttributeName>BBB</AttributeName>
        <AttributeValue>Value2</AttributeValue>
    </Attributes>
</AttributesCollection>

我希望使用XSL轉換將其轉換為以下內容:

<Attributes>
   <AAA>Value1</AAA>
   <BBB>Value2</BBB>
</Attributes>

我可以獲得屬性名稱,但不確定如何形成XML。 這是我嘗試過的。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <xsl:for-each select="./AttributesCollection/Attributes/AttributeName">
            Name:<xsl:value-of select="."/>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

給我的:

<?xml version="1.0" encoding="UTF-8"?>
            Name:AAA
            Name:BBB

那么,有可能做我想要的嗎? 有什么幫助嗎? 謝謝

應該這樣做:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/*">
    <Attributes>
      <xsl:apply-templates select="Attributes" />
    </Attributes>
  </xsl:template>

  <xsl:template match="Attributes">
    <xsl:element name="{AttributeName}">
      <xsl:value-of select="AttributeValue" />
    </xsl:element>
  </xsl:template>
</xsl:stylesheet>

對示例數據運行時,結果為:

<Attributes>
  <AAA>Value1</AAA>
  <BBB>Value2</BBB>
</Attributes>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM