3

以下は私の要件です。XSLT を使用してこれを行うことはできますか? AttributeName の値をポリシーの下のタグとして変換し、対応する AttributeValue を値として変換したいと考えています。

入力:

<Policy>
    <Attributes>
        <AttributeName>is_policy_loan</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>
    <Attributes>
        <AttributeName>is_policy_owners</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
    <Attributes>
        <AttributeName>is_policy_twoyears</AttributeName>
        <AttributeValue>Yes</AttributeValue>
    </Attributes>       
</Policy>

出力:

<Policy>
    <is_policy_loan>Yes</is_policy_loan>
    <is_policy_owners>Yes</is_policy_owners>
    <is_policy_twoyears>Yes</is_policy_twoyears>
</Policy>
4

2 に答える 2

1

私がする方法は、

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
   <xsl:output method="xml" indent="yes" encoding="UTF-8" omit-xml-declaration="yes" />
   <xsl:template match="Policy">
      <xsl:element name="Policy">
         <xsl:apply-templates />
      </xsl:element>
   </xsl:template>
   <xsl:template match="Attributes">
      <xsl:variable name="name" select="AttributeName" />
      <xsl:element name="{$name}">
         <xsl:value-of select="AttributeValue" />
      </xsl:element>
   </xsl:template>
</xsl:stylesheet>

出力は、

<Policy>
    <is_policy_loan>Yes</is_policy_loan>
    <is_policy_owners>Yes</is_policy_owners>       
    <is_policy_twoyears>Yes</is_policy_twoyears>       
</Policy>
于 2013-11-05T03:18:54.783 に答える
1

次のxslファイルがその役割を果たします。

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <!-- create the <AttributeName>AttributeValue</..> nodes -->
  <xsl:template match="//Attributes">
    <xsl:variable name="name" select="AttributeName" />
    <xsl:element name="{$name}">
      <xsl:value-of select="AttributeValue" />
    </xsl:element>
  </xsl:template>

  <!-- wrap nodes in a `Policy` node -->
  <xsl:template match="/">
    <Policy>
      <xsl:apply-templates/>
    </Policy>
  </xsl:template>
</xsl:stylesheet>
于 2013-11-04T21:12:10.850 に答える