需要使用XSLT其XSL文件使用properties.xml文件,将XML文件转换成另一个XML(n

2019-09-16 10:01发布

考虑我的XML输入文件,如下

     <?xml version="1.0" encoding="UTF-8"?>
        <com xsi:schemaLocation="http://do.way.com/sales/Amb
           http://do.way.com/temp/sales/ale/ax.xsd" 
           xmlns:w="http://do.way.com/sales/W" 
           xmlns="http://do.way.com/sales/Amb" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:wire="http://do.way.com/sales/Wire">

    <content>
      <wire:wire>
        <wire:sI>

          <w:aH>
          <text>Ccc  <dynamic name="C_R_N"/>: More</text>
          <w:contact value="false"/>
          </w:aH>

          <w:page>
            <nL>Please call us</nL>
          </w:page>

          <w:body>
            <text> C R N: <strong>
           <dynamic name="C_R_N"/></strong>
            </text>
          </w:body>

          <w:body>
            <text>
              RE: <dynamic name="D_C_P"/> <dynamic name="M_C_O"/>
            </text>
          </w:body>

          <w:body>
            <text>
              In order <strong>
                <dynamic name="M_D_D"/>
              </strong>, we need some information.
            </text>
          </w:body>

          <w:body>
            <text>
              <strong>
                Please call us <dynamic name="C_P_D_N"/>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic name="DST_M_F_S_Hour"/> am - 
    <dynamic name="DST_M_F_E_Hour"/> pm; Saturday, 
    <dynamic name="DST_M_F_S_Hour"/> am - 
    <dynamic name="DST_M_F_E_Hour"/> pm.
                </li>
                       </ul>
            </text>
          </w:body>

          <w:body>
            <text>
              Thank you for your  action. .
            </text>
          </w:body>


          <w:body>
            <text>
              <dynamic name="C_D_N"/>
            </text>
          </w:body>


          <w:TextAndImage>
            <text>
             <dynamic name="C_S_N_D"/>            
            </text>
          </w:TextAndImage>

          <w:dynamicNames>
            <w:dynamicName value="LOW"/>
            <w:dynamicName value="Env"/>
          </w:dynamicNames>

          </wire:sI>
        </wire:wire>

          </content>
          </com>    

其将上述输入XML文件所需的XSL文件是指具有下面properties.xml中给出的输入XML标签元件(键)的各变换标记(值)一个properties.xml文件

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

            <Properties>     
            <Property value="w:aH">aHe</Property>
           <Property value="w:contact">contact1</Property>
           <Property value="w:page">pageH</Property> 
           <Property value="w:body">bodyP</Property>
           <Property value="w:TextAndImage">textAndImage1</Property>
           <Property value="w:dynamicNames">dynamicNames1</Property>
           <Property value="w:dynamicName">dynamicValName</Property>
         </Properties> 

我转换的XML看起来像

    <Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd" 
 xmlns="id:d1234" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


     <aHe>
      <text>Ccc  
             <dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml" >
                  C_R_N
            </dynamic>
        : More
    </text>
      <contact1>false</contact1>
      </aHe>

      <pageH>
        <nL>Please call us</nL>
      </pageH>

      <bodyP>
        <text> C R N: <strong>
       <dynamic name="C_R_N" xmlns="http://www.w3.org/1999/xhtml">
      C_R_N
     </dynamic>
      </strong>
        </text>
      </bodyP>

      <bodyP>
        <text>
          RE: <dynamic name="D_C_P" xmlns="http://www.w3.org/1999/xhtml"> 
              D_C_P</dynamic>
             <dynamic name="M_C_O" xmlns="http://www.w3.org/1999/xhtml">M_C_O</dynamic>
        </text>
      </bodyP>

      <bodyP>
        <text>
          In order <strong>
            <dynamic name="M_D_D" xmlns="http://www.w3.org/1999/xhtml"> M_D_D </dynamic>
          </strong>, we need some information.
        </text>
      </bodyP>

      <bodyP>
        <text>  Please call us 
          <strong>
           <dynamic name="C_P_D_N" xmlns="http://www.w3.org/1999/xhtml">C_P_D_N</dynamic>.
          </strong> Our hours <ul class="nested">
            <li>
               Monday - Friday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_E_Hour</dynamic> pm; Saturday, <dynamic name="DST_M_F_S_Hour" xmlns="http://www.w3.org/1999/xhtml">DST_M_F_S_Hour</dynamic> am - <dynamic name="DST_M_F_E_Hour" xmlns="http://www.w3.org/1999/xhtml">
              DST_M_F_E_Hour</dynamic>pm.
            </li>
                   </ul>
        </text>
      </bodyP>

      <bodyP>
        <text>
          Thank you for your  action. .
        </text>
      </bodyP>


      <bodyP>
        <text>
          <dynamic name="C_D_N" xmlns="http://www.w3.org/1999/xhtml">
          C_D_N </dynamic>
        </text>
      </bodyP>


      <textAndImage>
        <text>
          <dynamic name="C_S_N_D" xmlns="http://www.w3.org/1999/xhtml">
           C_S_N_D </dynamic>
        </text>
      </textAndImage>

      <dynamicNames1>
        <dynamicValName>LOW</dynamicValName>
        <dynamicValName>Env</dynamicValName>
      </dynamicNames1>

     </Content>


      Can anyone  provide me with the xsl file which meets my requirement.

Answer 1:

它看起来像你不需要因为你只是摆脱它可以做更简单的名称空间的属性文件:

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

你想达到什么样的其余部分是纯大概XSLT的东西。

编辑

更新包括的东西,可以让你更接近你的目标

<xsl:template match="/">
    <Content>
        <xsl:apply-templates/>
    </Content>
</xsl:template>

<xsl:template match="@*">
    <xsl:copy/>
</xsl:template>

<xsl:template match="* [local-name() != 'com' and local-name() != 'content' and local-name() != 'wire' and local-name() != 'sI']">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node() | @*"/>
    </xsl:element>
</xsl:template>

<xsl:template match="* [local-name() = 'dynamic']">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates select="node() | @*"/>
        <xsl:value-of select="@name"/>
    </xsl:element>
</xsl:template>

<xsl:template match="* [local-name() = 'contact' or local-name()='dynamicName']">
    <xsl:element name="{local-name()}">
        <xsl:value-of select="@value"/>
    </xsl:element>
</xsl:template>

其结果是

<?xml version="1.0" encoding="UTF-8"?>
<Content>
    <aH>
        <text>Ccc  <dynamic name="C_R_N">C_R_N</dynamic>: More</text>
        <contact>false</contact>
    </aH>
    <page>
        <nL>Please call us</nL>
    </page>
    <body>
        <text> C R N: <strong>
                <dynamic name="C_R_N">C_R_N</dynamic>
            </strong></text>
    </body>
    <body>
        <text>
              RE: <dynamic name="D_C_P">D_C_P</dynamic><dynamic name="M_C_O">M_C_O</dynamic></text>
    </body>
    <body>
        <text>
              In order <strong>
                <dynamic name="M_D_D">M_D_D</dynamic>
            </strong>, we need some information.
            </text>
    </body>
    <body>
        <text><strong>
                Please call us <dynamic name="C_P_D_N">C_P_D_N</dynamic>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am - 
    <dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm; Saturday, 
    <dynamic name="DST_M_F_S_Hour">DST_M_F_S_Hour</dynamic> am - 
    <dynamic name="DST_M_F_E_Hour">DST_M_F_E_Hour</dynamic> pm.
                </li>
            </ul></text>
    </body>
    <body>
        <text>
              Thank you for your  action. .
            </text>
    </body>
    <body>
        <text>
            <dynamic name="C_D_N">C_D_N</dynamic>
        </text>
    </body>
    <TextAndImage>
        <text>
            <dynamic name="C_S_N_D">C_S_N_D</dynamic>
        </text>
    </TextAndImage>
    <dynamicNames>
        <dynamicName>LOW</dynamicName>
        <dynamicName>Env</dynamicName>
    </dynamicNames>
</Content>

相反,如果你想将所有属性的属性动态/ @名称和otherElement / @值的元件主体的价值观,那么下面会做什么:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <Content>
            <xsl:apply-templates/>
        </Content>
    </xsl:template>
    <xsl:template match="@*">
        <xsl:copy/>
    </xsl:template>
    <xsl:template match="* [local-name() != 'com' and local-name() != 'content' and local-name() != 'wire' and local-name() != 'sI']">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="* [local-name() = 'dynamic']">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*[local-name !='name']"/>
            <xsl:value-of select="@name"/>
        </xsl:element>
    </xsl:template>
    <xsl:template match="* [local-name() != 'dynamic' and @value]">
        <xsl:element name="{local-name()}">
            <xsl:apply-templates select="node() | @*[local-name !='value']"/>
            <xsl:value-of select="@value"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

有结果

<?xml version="1.0" encoding="UTF-8"?>
<Content>
    <aH>
        <text>Ccc  <dynamic>C_R_N</dynamic>: More</text>
        <contact>false</contact>
    </aH>
    <page>
        <nL>Please call us</nL>
    </page>
    <body>
        <text> C R N: <strong>
                <dynamic>C_R_N</dynamic>
            </strong></text>
    </body>
    <body>
        <text>
              RE: <dynamic>D_C_P</dynamic><dynamic>M_C_O</dynamic></text>
    </body>
    <body>
        <text>
              In order <strong>
                <dynamic>M_D_D</dynamic>
            </strong>, we need some information.
            </text>
    </body>
    <body>
        <text><strong>
                Please call us <dynamic>C_P_D_N</dynamic>.
              </strong> Our hours <ul class="nested">
                <li>
                   Monday - Friday, 
    <dynamic>DST_M_F_S_Hour</dynamic> am - 
    <dynamic>DST_M_F_E_Hour</dynamic> pm; Saturday, 
    <dynamic>DST_M_F_S_Hour</dynamic> am - 
    <dynamic>DST_M_F_E_Hour</dynamic> pm.
                </li>
            </ul></text>
    </body>
    <body>
        <text>
              Thank you for your  action. .
            </text>
    </body>
    <body>
        <text>
            <dynamic>C_D_N</dynamic>
        </text>
    </body>
    <TextAndImage>
        <text>
            <dynamic>C_S_N_D</dynamic>
        </text>
    </TextAndImage>
    <dynamicNames>
        <dynamicName>LOW</dynamicName>
        <dynamicName>Env</dynamicName>
    </dynamicNames>
</Content>


Answer 2:

我已经通过采取由DavidCarlisle给出的代码部分符合要求的帮助下用下面的XSLT代码

     <?xml version="1.0"?>
      <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  
       xmlns="id:d1234"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       
       xmlns:w="http://do.way.com/sales/W" 
       xmlns:wire="http://do.way.com/sales/Wire"    
       exclude-result-prefixes=" wire w ">

         <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
         <xsl:template match="/">
           <Content xsi:schemaLocation="id:d1234 http://abc:10/w/g/B/System/abc.xsd">
            <xsl:apply-templates select="w:sI"/>
          </Content>
        </xsl:template>

         <xsl:template match="*[local-name()!=sI]">
            <xsl:variable name="n" select="name()"/>
              <xsl:variable name="new">
                <xsl:for-each select="document('Properties.xml')">
                 <xsl:choose>
                  <xsl:when test="key('n',$n)">
                    <xsl:value-of select="key('n',$n)"/>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select="$n"/>
                  </xsl:otherwise>
                </xsl:choose>
               </xsl:for-each>
              </xsl:variable>

       <xsl:element name="{$new}">
          <xsl:copy-of select="@*"/>
            <xsl:apply-templates/>
          </xsl:element>
       </xsl:template>


     <xsl:key name="n" match="Property" use="@value"/>

        <xsl:template match="*[name() = 'dynamic']">
            <dynamic xmlns="http://www.w3.org/1999/xhtml">
               <xsl:copy-of select="@*"/>
                   <strong>
              <xsl:value-of select="@name"/>
             </strong>
           </dynamic>

        </xsl:template>
    </xsl:stylesheet>

唯一的问题IM在此面向是我不能使用XSLT代码川方可以采取具有“值”属性(接触和dynamicName)提取其属性值,并把它们各自的下标记的(在指定properties.xml中)内容的节点。 请通过此XSL文件,并在有必要改变或我也同样高兴,如果你能拿出一个新的one.But请确保使用properties.xml中file.Thanks的。



文章来源: need to transform an xml file into another xml using xslt whose xsl file uses a Properties.xml file