【问题标题】:XML XSLT transformationXML XSLT 转换
【发布时间】:2019-10-06 09:23:13
【问题描述】:

我是 XML、XSLT 的新手。我有一个输入 xml 文件和目标 xml 文件。我需要编写生成目标 xml 的 xslt 代码。谁能帮我理解这一点。根 Element bill 与 Document 映射,并且 剩余的子元素和子子元素用 para 映射。我正在努力编写转换代码。第一个是输入,第二个是目标xml代码:谁能帮我转换一下。

  <?xml version="1.0" encoding="UTF-8"?>
  <?xml-stylesheet type="text/xsl" href="transformation.xsl"?>
   <bill bill-stage="Enrolled" bill-type="abc" dms- 
    id="1234" key="1" public- 
     private="public" stage-count="1">
  <main>
  <longTitle>
  <docTitle style="-uslm-lc:I123" class="centered 
   fontsize18">Hello</docTitle>
   <officialTitle style="-uslm-lc:I1234" class="centered fontsize8">To 
    name the Department of Electrical Engineering.<br 
    verticalSpace="08pt"/></officialTitle>
   </longTitle>
   <enactingFormula style="-uslm-lc:I2334">  This related to Student 
      Homework Submission on time</enactingFormula>
   <section style="-uslm-lc:I235"><num value="1">SECTION 1. </num> 
  <heading>Report Submission</heading>
  <content style="-uslm-lc:I1236">  The Department of Electrical 
  Engineering has come to a conclusion that students who do not submit 
  assignement on time will not be considered to sit in the finals
  </content>
  </section>
 </main>
  <signatures>
   <signature><role style="-uslm-lc:I114">Associate Department 
     Advisor</role></signature>
   <signature><role style="-uslm-lc:I114">Electrical Engineering Dept. 
    </role></signature>
  </signatures>
 </bill>

目标 XML

   <?xml version="1.0" encoding="UTF-8"?>
    <document>
      <para format="6655" loc="05">Hello</para>
        <para format="66554" loc="11">To name the Department  of 
           Electrical Engineering. </para>
         <para format="6501" loc="20">This related to Student Homework 
           Submission on time</para>
           <para format="6501" loc="46"><?xpp fv;1?>SECTION 1. <?xpp 
               fv;4?>Report Submission <?xpp rs-font?>The Department of 
                 Electrical Engineering has come to a conclusion that 
                     students who do not submit assignement on time will 
                       not be considered to sit in the finals</para>
            <para format="6501" loc="14">Associate Department 
             Advisor</para>
            <para format="6501" loc="14">Electrical Engineering Dept. 
         </para>
      </document>

我已尝试编写以下代码:

 <?xml version = "1.0" encoding="UTF-8"?>

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

<xsl:output method="text" indent="yes">
    <xsl:template  match="/">
        <xsl:element name ="document">
            <xsl:apply-templates select ="bill"/>
        </xsl:element>
    </xsl:template>

    <xsl:template match="document">
        <xsl:element name = "bill">
            <!--</xsl:copy-of select "*"/>-->
            <xsl:for-each select="bill/main/longTitle">
                <xsl:apply-template select="para">
                    <xsl:value-of select="docTitle"/>
                    <xsl:value-of select="officialTitle"/>
                </xsl:element>   
            </xsl:for-each>
            <xsl:for-each select="bill/main/">
                <xsl:value-of select="enactingFormula"/>

            </xsl:for-each>
            <xsl:for-each select="section">
            <xsl:for-each select="bill/main/section">
                <xsl:value-of select="num">
                <xsl:value-of select="heading">
                <xsl:value-of select="content">

            </xsl:for-each>
        </xsl:element>
    </xsl:template>

</xsl:output>

【问题讨论】:

  • 您需要什么样的帮助?您的代码是否给了您特定的错误消息?您不确定其中一个元素的正确语法吗?代码做的事情是否超出了您的预期?
  • 我需要有关映射的帮助。根元素“bill”映射到“document”,其余所有子元素映射到“para”。如何在 XSLT 中解决这个问题?

标签: xml xslt transformation


【解决方案1】:
**<?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:element name="document">
    <xsl:apply-templates select="bill"/>
  </xsl:element>
</xsl:template>
<xsl:template match="//docTitle">
    <para format="6655" loc="05">
    <xsl:apply-templates/>
  </para>
</xsl:template>
<xsl:template match="//officialTitle">
  <para format="66554" loc="11">
    <xsl:apply-templates/>
  </para>
</xsl:template>
<xsl:template match="//enactingFormula">
  <para format="6501" loc="20">
    <xsl:apply-templates/>
  </para>
</xsl:template>
<xsl:template match="//section">
  <para format="6501" loc="46">
    <xsl:apply-templates/>
  </para>
</xsl:template>
  <xsl:template match="num[parent::section]">
    <xsl:text>&lt;?xpp fv;1?&gt;</xsl:text>
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="heading[parent::section]">
    <xsl:text>&lt;?xpp fv;4?&gt;</xsl:text>
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="content[parent::section]">
    <xsl:text> &lt;?xpp rs-font?&gt;</xsl:text>
    <xsl:apply-templates/>
</xsl:template>
<xsl:template match="signatures">
  <xsl:apply-templates/>
</xsl:template>
  <xsl:template match="signature">
    <para format="6501" loc="14">
      <xsl:apply-templates/>
    </para>
  </xsl:template>
</xsl:stylesheet>**

【讨论】:

  • 我写了上面匹配目标xml的XSLT代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-07-28
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多