English 中文(简体)
鉴于XML的投入,我如何能够取得以下成果:
原标题:How can I make XSL produce the following result given the input XML
  • 时间:2012-01-11 16:37:56
  •  标签:
  • xml
  • xslt

我尝试了几件东西......使用变式和模板,并取得了一些缓慢的进展。 但是,我不要忘记这一点——与我的情况相左。

投入......

  • Table 700 can contain multiple entries.
  • The values for Subject1, Subject2, Subject3 will be English, Math, Science (But the order may vary from one source XML to another)
  • The Scores are positionally ties to the Subject (That is Score1 is for Subject1)

The OutPut...

  • Will ALWAYS contain 6 nodes (ENGLISH, MATH, SCIENCE, Class, Class2, Class3)
  • The Subject_ tags order ALWAYS needs to be English, Math, then Science.
  • The Subject_ tags will be upper cased
  • The Subject_ tags will contain a flag of 1 if the corresponding score is > 0; otherwise 0
  • The Class_Score tags order order ALWAYS needs to be English, Math, then Science.

我质疑我的守则,而以下做法可能并非完全有效,而是对我所尝试的内容的看法。

我的第一个问题是——我是否走上正轨?

  • Pass Subject to Grades template
  • Capture the index of Subject
  • Pass Subject and Index to add-Grades-nodes template

这正是我的背景问题所阻止的。

纽约总部 我的投入

<?xml version="1.0"?>
<Account Number="123456">
  <Data>
    <Table ID="700">
      <Record ID="1" SubClass="Person">
        <Name.Last>Smith</Name.Last>
        <Name.First>John</Name.First>
        <Score1>50</Score1>
        <Score2>75</Score2>
        <Score3>100</Score3>
        <Subject1>Math</Subject1>
        <Subject2>English</Subject2>
        <Subject3>Science</Subject3>
      </Record>
      <Record ID="2" SubClass="Person">
        <Name.Last>Smith</Name.Last>
        <Name.First>Jane</Name.First>
        <Score1></Score1>
        <Score2>77</Score2>
        <Score3>80</Score3>
        <Subject1>Math</Subject1>
        <Subject2>English</Subject2>
        <Subject3>Science</Subject3>
      </Record>
    </Table>
  </Data>
</Account>

- 希望产出XML——

<Out>
    <Subject_ENGLISH>1</Subject_ENGLISH>
    <Subject_MATH>1</Subject_MATH>
    <Subject_SCIENCE>1</Subject_SCIENCE>
    <Class_SCORE>75</Class_SCORE>
    <Class2_SCORE>50</Class2_SCORE>
    <Class3_SCORE>100</Class3_SCORE>
    <Subject_ENGLISH>0</Subject_ENGLISH>
    <Subject_MATH>1</Subject_MATH>
    <Subject_SCIENCE>1</Subject_SCIENCE>
    <Class_SCORE></Class_SCORE>
    <Class2_SCORE>77</Class2_SCORE>
    <Class3_SCORE>80</Class3_SCORE>
</Out>

————————

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
  <xsl:output method="xml" encoding="UTF-8"/>

  <xsl:template match="Account">
    <Out>
      <xsl:for-each select="/Account/Data/Table[@ID= 700 ]/Record[@SubClass= Person ]>

        <xsl:call-template name="Grades">
          <xsl:with-param name="Subject">English</xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="Grades">
          <xsl:with-param name="Subject">Math</xsl:with-param>
        </xsl:call-template>
        <xsl:call-template name="Grades">
          <xsl:with-param name="Subject">Science</xsl:with-param>
        </xsl:call-template>


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


  <xsl:template name="Grades">
    <xsl:param name="Subject"/>

    <xsl:for-each select="*[starts-with(name(),  Subject )][node()=$Subject]">

      <xsl:variable name= cr-index >
        <xsl:value-of select = substring(name(), string-length(name())) />
      </xsl:variable>

      <xsl:call-template name="create-Grades-nodes">
        <xsl:with-param name="cr-context" select =".."/>
        <xsl:with-param name="Subject">
          <xsl:value-of select= $Subject  />
        </xsl:with-param>
        <xsl:with-param name="cr-index">
          <xsl:value-of select= $cr-index />
        </xsl:with-param>
      </xsl:call-template>
    </xsl:for-each>
  </xsl:template>

  <xsl:template name="create-Grades-nodes" match="$cr-context" >
    <xsl:param name ="cr-context"/>
    <xsl:param name ="Subject"/>
    <xsl:param name ="cr-index"/>

    <xsl:variable name= cr-score >
      <xsl:value-of select= name($cr-context)/concat("Score", $cr-index) />
    </xsl:variable>

    <xsl:variable name="smallcase" select=" abcdefghijklmnopqrstuvwxyz " />
    <xsl:variable name="uppercase" select=" ABCDEFGHIJKLMNOPQRSTUVWXYZ " />

      <xsl:element name= {concat("Subject_", translate($Subject, $smallcase, $uppercase))} >
        <xsl:choose>
        <xsl:when test=  $cr-score &gt; 0 >
          <xsl:value-of select = "1"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select = "0"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:element>

  </xsl:template>
</xsl:stylesheet>
最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 这一转变:

<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:param name="pSubjects">
  <s name="English" at="1"/>
  <s name="Math"    at="2"/>
  <s name="Science" at="3"/>
 </xsl:param>

 <xsl:variable name="vSubjects" select=
 "document(  )/*/xsl:param[@name= pSubjects ]/*"/>

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

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

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

 <xsl:template match="Table[@ID= 700 ]/Record">
  <xsl:apply-templates select="*[starts-with(name(),  Su )]">
    <xsl:sort select="$vSubjects[@name = current()]/@at" data-type="number"/>
  </xsl:apply-templates>

  <xsl:apply-templates select="*[starts-with(name(),  Sc )]">
   <xsl:sort select=
    "$vSubjects[@name
               = current()/../*[starts-with(name(),  Su )]
                    [substring-after(name(),  Subject )
                    =
                     substring-after(name(current()),  Score )
                    ]
                ]/@at"
     data-type="number"/>
  </xsl:apply-templates>
 </xsl:template>

 <xsl:template match="*[starts-with(name(),  Su )]">
   <xsl:variable name="vgenName" select=
   "concat( Subject_ ,
            translate(., $vLower, $vUpper)
           )"/>
  <xsl:element name="{$vgenName}">
    <xsl:value-of select="."/>
  </xsl:element>
 </xsl:template>

 <xsl:template match="*[starts-with(name(),  Sc )]">
  <xsl:variable name="vInd" select=
   "$vSubjects[@name
               = current()/../*[starts-with(name(),  Su )]
                    [substring-after(name(),  Subject )
                    =
                     substring-after(name(current()),  Score )
                    ]
                ]/@at"/>

  <xsl:variable name="vgenName" select=
   "concat( Class ,
           translate($vInd,  1 ,   ),
            _SCORE 
           )
   "/>
   <xsl:element name="{$vgenName}">
    <xsl:value-of select="."/>
   </xsl:element>
 </xsl:template>
</xsl:stylesheet>

在应用XML文件时:

<Account Number="123456">
  <Data>
    <Table ID="700">
      <Record ID="1" SubClass="Person">
        <Name.Last>Smith</Name.Last>
        <Name.First>John</Name.First>
        <Score1>50</Score1>
        <Score2>75</Score2>
        <Score3>100</Score3>
        <Subject1>Math</Subject1>
        <Subject2>English</Subject2>
        <Subject3>Science</Subject3>
      </Record>
      <Record ID="2" SubClass="Person">
        <Name.Last>Smith</Name.Last>
        <Name.First>Jane</Name.First>
        <Score1></Score1>
        <Score2>77</Score2>
        <Score3>80</Score3>
        <Subject1>Math</Subject1>
        <Subject2>English</Subject2>
        <Subject3>Science</Subject3>
      </Record>
    </Table>
  </Data>
</Account>

<produces the Hope,那末正确结果:

<Out>
  <Subject_ENGLISH>English</Subject_ENGLISH>
  <Subject_MATH>Math</Subject_MATH>
  <Subject_SCIENCE>Science</Subject_SCIENCE>
  <Class_SCORE>75</Class_SCORE>
  <Class2_SCORE>50</Class2_SCORE>
  <Class3_SCORE>100</Class3_SCORE>
  <Subject_ENGLISH>English</Subject_ENGLISH>
  <Subject_MATH>Math</Subject_MATH>
  <Subject_SCIENCE>Science</Subject_SCIENCE>
  <Class_SCORE>77</Class_SCORE>
  <Class2_SCORE></Class2_SCORE>
  <Class3_SCORE>80</Class3_SCORE>
</Out>
问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签