Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于时髦XML的XSLT_Xml_Xslt - Fatal编程技术网

基于时髦XML的XSLT

基于时髦XML的XSLT,xml,xslt,Xml,Xslt,所以,我试图将一些XML转换成一种简单的格式,以便转储到数据库中。这就是它看起来的样子: <?xml version="1.0" encoding="UTF-8"?> <detail type="Courses Taken" gm_recid="FNBYHVW(,7()E)S" > <properties> <property name="reference" > <pr

所以,我试图将一些XML转换成一种简单的格式,以便转储到数据库中。这就是它看起来的样子:

<?xml version="1.0" encoding="UTF-8"?>
<detail type="Courses Taken" gm_recid="FNBYHVW(,7()E)S" >
          <properties>
            <property name="reference" >
              <property_string>M2</property_string>
            </property>
            <property name="Month YYYY" db_name="TITLE" >
              <property_string></property_string>
            </property>
            <property name="City, State" db_name="LINKACCT" >
              <property_string></property_string>
            </property>
          </properties>
</detail>

平方米
我更喜欢它看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<courses_taken>
    <gm_recid>FNBYHVW(,7()E)S</gm_recid>
    <reference>M2</reference>
    <date></date>
    <location></location>
</courses_taken>

FNBYHVW(,7()E)S
平方米

我对XSL不太熟悉,所以不知从哪里开始。有人能给我指出正确的方向吗?

这应该可以让你开始:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template name="replace">
<xsl:param name="ptext"/>
<xsl:param name="ppattern"/>
<xsl:param name="preplacement"/>

<xsl:choose>
  <xsl:when test="not(contains($ptext, $ppattern))">
    <xsl:value-of select="$ptext"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="substring-before($ptext, $ppattern)"/>
    <xsl:value-of select="$preplacement"/>
    <xsl:call-template name="replace">
      <xsl:with-param name="ptext"
        select="substring-after($ptext, $ppattern)"/>
      <xsl:with-param name="ppattern" select="$ppattern"/>
      <xsl:with-param name="preplacement" select="$preplacement"/>
    </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>



<xsl:template match="detail">
    <xsl:variable name="elemName">
      <xsl:call-template name="replace">
        <xsl:with-param name="ppattern" select="' '"/>
        <xsl:with-param name="ptext" select="@type"/>
        <xsl:with-param name="preplacement" select="'_'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="{$elemName}">
      <gm_recid>
        <xsl:value-of select="@gm_recid"/>
      </gm_recid>
      <xsl:apply-templates select="properties"/>
    </xsl:element>

  </xsl:template>


  <xsl:template match="properties">
    <xsl:for-each select="property">
        <!--Do something...-->
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

您的属性包含空格和/或其他无效元素字符,所以您可能需要单独决定如何处理这些字符

同时替换从此处获取的模板:


这应该让您开始:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template name="replace">
<xsl:param name="ptext"/>
<xsl:param name="ppattern"/>
<xsl:param name="preplacement"/>

<xsl:choose>
  <xsl:when test="not(contains($ptext, $ppattern))">
    <xsl:value-of select="$ptext"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:value-of select="substring-before($ptext, $ppattern)"/>
    <xsl:value-of select="$preplacement"/>
    <xsl:call-template name="replace">
      <xsl:with-param name="ptext"
        select="substring-after($ptext, $ppattern)"/>
      <xsl:with-param name="ppattern" select="$ppattern"/>
      <xsl:with-param name="preplacement" select="$preplacement"/>
    </xsl:call-template>
  </xsl:otherwise>
</xsl:choose>
</xsl:template>



<xsl:template match="detail">
    <xsl:variable name="elemName">
      <xsl:call-template name="replace">
        <xsl:with-param name="ppattern" select="' '"/>
        <xsl:with-param name="ptext" select="@type"/>
        <xsl:with-param name="preplacement" select="'_'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:element name="{$elemName}">
      <gm_recid>
        <xsl:value-of select="@gm_recid"/>
      </gm_recid>
      <xsl:apply-templates select="properties"/>
    </xsl:element>

  </xsl:template>


  <xsl:template match="properties">
    <xsl:for-each select="property">
        <!--Do something...-->
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

您的属性包含空格和/或其他无效元素字符,所以您可能需要单独决定如何处理这些字符

同时替换从此处获取的模板:


这一完整的XSLT转换:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vUpper" select=
 "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

 <xsl:variable name="vLower" select=
 "'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:variable name="vDigits" select=
 "'0123456789'"/>

 <xsl:variable name="vAlphaNum" select=
  "concat($vUpper, $vLower, $vDigits, '_')"/>

 <xsl:template match="detail">
  <xsl:element name=
  "{translate(@type,
              translate(@type, $vAlphaNum, ''),
              '______________________________'
              )}">
   <xsl:apply-templates select=
     "@*[not(name()='type')]|*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="detail/@*">
  <xsl:element name=
  "{translate(name(),
              translate(name(), $vAlphaNum, ''),
              '______________________________'
              )}">

   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="property[@name='reference']">
  <reference>
   <xsl:value-of select="."/>
  </reference>
 </xsl:template>

 <xsl:template match="property[@name='Month YYYY']">
  <date>
   <xsl:value-of select="."/>
  </date>
 </xsl:template>

 <xsl:template match="property[@name='City, State']">
  <location>
   <xsl:value-of select="."/>
  </location>
 </xsl:template>
</xsl:stylesheet>
<detail type="Courses Taken" gm_recid="FNBYHVW(,7()E)S" >
          <properties>
            <property name="reference" >
              <property_string>M2</property_string>
            </property>
            <property name="Month YYYY" db_name="TITLE" >
              <property_string></property_string>
            </property>
            <property name="City, State" db_name="LINKACCT" >
              <property_string></property_string>
            </property>
          </properties>
</detail>
<Courses_Taken>
   <gm_recid>FNBYHVW(,7()E)S</gm_recid>
   <reference>M2</reference>
   <date/>
   <location/>
</Courses_Taken>
在这种特殊情况下,我们不希望删除无效字符,而是用
“quot
替换每个字符

我们假设候选名称的长度不会超过30,并使用固定长度的替换字符串(当然可以根据需要将其设置为任意大):


这个完整的XSLT转换

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vUpper" select=
 "'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>

 <xsl:variable name="vLower" select=
 "'abcdefghijklmnopqrstuvwxyz'"/>

 <xsl:variable name="vDigits" select=
 "'0123456789'"/>

 <xsl:variable name="vAlphaNum" select=
  "concat($vUpper, $vLower, $vDigits, '_')"/>

 <xsl:template match="detail">
  <xsl:element name=
  "{translate(@type,
              translate(@type, $vAlphaNum, ''),
              '______________________________'
              )}">
   <xsl:apply-templates select=
     "@*[not(name()='type')]|*"/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="detail/@*">
  <xsl:element name=
  "{translate(name(),
              translate(name(), $vAlphaNum, ''),
              '______________________________'
              )}">

   <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="property[@name='reference']">
  <reference>
   <xsl:value-of select="."/>
  </reference>
 </xsl:template>

 <xsl:template match="property[@name='Month YYYY']">
  <date>
   <xsl:value-of select="."/>
  </date>
 </xsl:template>

 <xsl:template match="property[@name='City, State']">
  <location>
   <xsl:value-of select="."/>
  </location>
 </xsl:template>
</xsl:stylesheet>
<detail type="Courses Taken" gm_recid="FNBYHVW(,7()E)S" >
          <properties>
            <property name="reference" >
              <property_string>M2</property_string>
            </property>
            <property name="Month YYYY" db_name="TITLE" >
              <property_string></property_string>
            </property>
            <property name="City, State" db_name="LINKACCT" >
              <property_string></property_string>
            </property>
          </properties>
</detail>
<Courses_Taken>
   <gm_recid>FNBYHVW(,7()E)S</gm_recid>
   <reference>M2</reference>
   <date/>
   <location/>
</Courses_Taken>
在这种特殊情况下,我们不希望删除无效字符,而是用
“quot
替换每个字符

我们假设候选名称的长度不会超过30,并使用固定长度的替换字符串(当然可以根据需要将其设置为任意大):


不是有效的xml元素。也许是coursestaken?您需要一个基于XML小样本的XSLT教程。太广泛了,这里无法涵盖。我要求一个非常具体的解决方案。如果我能修复这一部分,我就能修复我的整个XML文档。@WatermarkStudios不,你不是在问一些具体的问题。xml不完整,输出相当抽象。你不能期望一个抽象输入的具体解决方案。更好吗?我展示的XML实际上就是我得到的XML。我只使用了一个实例。gm_recid是数据库中的外键,因此我至少可以将它链接到帐户记录。使用XSD.exe可以很容易地将输出映射到C#类,并且比上述方法更易于管理。顺便说一句,谢谢你提供以下参考信息。不是有效的xml元素。也许是coursestaken?您需要一个基于XML小样本的XSLT教程。太广泛了,这里无法涵盖。我要求一个非常具体的解决方案。如果我能修复这一部分,我就能修复我的整个XML文档。@WatermarkStudios不,你不是在问一些具体的问题。xml不完整,输出相当抽象。你不能期望一个抽象输入的具体解决方案。更好吗?我展示的XML实际上就是我得到的XML。我只使用了一个实例。gm_recid是数据库中的外键,因此我至少可以将它链接到帐户记录。使用XSD.exe可以很容易地将输出映射到C#类,并且比上述方法更易于管理。顺便说一句…感谢您提供以下参考信息。此代码不完整(注释),但更重要的是,代码的某些部分缺失,并且不是格式良好的XML文档。请更正。@DimitreNovatchev关于本计划的不完整性。关于格式不好,代码块中遗漏了一些内容。我明白了,在写这篇文章的时候,原来的文章和现在有很大的不同。当我运行您的转换时,结果只是:
FNBYHVW(,7()E)S
是的,我知道。正如我所说的,这篇文章是不一样的,所以我不知道如何处理其余的属性,我把它留了下来。这段代码不完整(注释),但更重要的是,代码的某些部分丢失了,并且它不是一个格式良好的XML文档。请更正。@DimitreNovatchev关于本计划的不完整性。关于格式不好,代码块中遗漏了一些内容。我明白了,在写这篇文章的时候,原来的文章和现在有很大的不同。当我运行您的转换时,结果只是:
FNBYHVW(,7()E)S
是的,我知道。正如我所说,这篇文章是不一样的,所以我不知道如何处理其余的属性,我把它留了下来。@FailedDev:是的,非常强大。@FailedDev:是的,非常强大。