从元素属性中选择值

时间:2015-06-11 14:53:00

标签: xml xslt value-of

我一直在尝试从嵌入式XML文件中进行选择,以便用于转换。基本上我想得到目前的任何价值"要求文件"



<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd" xmlns:configuration="http://eclipse.org/rmf/pror/toolextensions/1.0">
  <THE-HEADER>
    <REQ-IF-HEADER IDENTIFIER="rmf-8643a52b-b85b-4c9f-ada6-ef78c553fe6a">
      <COMMENT>Created by: me</COMMENT>
      <CREATION-TIME>2015-06-11T10:47:09.049+01:00</CREATION-TIME>
      <REQ-IF-TOOL-ID>ProR (http://pror.org)</REQ-IF-TOOL-ID>
      <REQ-IF-VERSION>1.0</REQ-IF-VERSION>
      <SOURCE-TOOL-ID>ProR (http://pror.org)</SOURCE-TOOL-ID>
    </REQ-IF-HEADER>
  </THE-HEADER>
  <CORE-CONTENT>
    <REQ-IF-CONTENT>
      <DATATYPES>
        <DATATYPE-DEFINITION-STRING IDENTIFIER="_ceAssxAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="T_String32k" MAX-LENGTH="32000"/>
      </DATATYPES>
      <SPEC-TYPES>
        <SPEC-OBJECT-TYPE IDENTIFIER="_ceAstBAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="Requirement Type">
          <SPEC-ATTRIBUTES>
            <ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_ceAstRAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="Description">
              <TYPE>
                <DATATYPE-DEFINITION-STRING-REF>_ceAssxAWEeWbqPmdfLAZ1w</DATATYPE-DEFINITION-STRING-REF>
              </TYPE>
            </ATTRIBUTE-DEFINITION-STRING>
          </SPEC-ATTRIBUTES>
        </SPEC-OBJECT-TYPE>
        <SPECIFICATION-TYPE IDENTIFIER="_ceAsthAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="Specification Type">
          <SPEC-ATTRIBUTES>
            <ATTRIBUTE-DEFINITION-STRING IDENTIFIER="_ceBTwBAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="Description">
              <TYPE>
                <DATATYPE-DEFINITION-STRING-REF>_ceAssxAWEeWbqPmdfLAZ1w</DATATYPE-DEFINITION-STRING-REF>
              </TYPE>
            </ATTRIBUTE-DEFINITION-STRING>
          </SPEC-ATTRIBUTES>
        </SPECIFICATION-TYPE>
      </SPEC-TYPES>
      <SPEC-OBJECTS>
        <SPEC-OBJECT IDENTIFIER="_ceBTwRAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:34.958+01:00">
          <VALUES>
            <ATTRIBUTE-VALUE-STRING THE-VALUE="This is a single SpecObject">
              <DEFINITION>
                <ATTRIBUTE-DEFINITION-STRING-REF>_ceAstRAWEeWbqPmdfLAZ1w</ATTRIBUTE-DEFINITION-STRING-REF>
              </DEFINITION>
            </ATTRIBUTE-VALUE-STRING>
          </VALUES>
          <TYPE>
            <SPEC-OBJECT-TYPE-REF>_ceAstBAWEeWbqPmdfLAZ1w</SPEC-OBJECT-TYPE-REF>
          </TYPE>
        </SPEC-OBJECT>
      </SPEC-OBJECTS>
      <SPECIFICATIONS>
        <SPECIFICATION IDENTIFIER="_ceBTwxAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00" LONG-NAME="Specification Document">
          <VALUES>
            <ATTRIBUTE-VALUE-STRING THE-VALUE="Requirements Document">
              <DEFINITION>
                <ATTRIBUTE-DEFINITION-STRING-REF>_ceBTwBAWEeWbqPmdfLAZ1w</ATTRIBUTE-DEFINITION-STRING-REF>
              </DEFINITION>
            </ATTRIBUTE-VALUE-STRING>
          </VALUES>
          <TYPE>
            <SPECIFICATION-TYPE-REF>_ceAsthAWEeWbqPmdfLAZ1w</SPECIFICATION-TYPE-REF>
          </TYPE>
          <CHILDREN>
            <SPEC-HIERARCHY IDENTIFIER="_ceBTxRAWEeWbqPmdfLAZ1w" LAST-CHANGE="2015-06-11T10:47:09.049+01:00">
              <OBJECT>
                <SPEC-OBJECT-REF>_ceBTwRAWEeWbqPmdfLAZ1w</SPEC-OBJECT-REF>
              </OBJECT>
            </SPEC-HIERARCHY>
          </CHILDREN>
        </SPECIFICATION>
      </SPECIFICATIONS>
    </REQ-IF-CONTENT>
  </CORE-CONTENT>
  <TOOL-EXTENSIONS>
    <REQ-IF-TOOL-EXTENSION>
      <configuration:ProrToolExtension>
        <configuration:specViewConfigurations>
          <configuration:ProrSpecViewConfiguration specification="_ceBTwxAWEeWbqPmdfLAZ1w">
            <configuration:columns>
              <configuration:Column label="Description" width="400"/>
            </configuration:columns>
            <configuration:leftHeaderColumn>
              <configuration:Column label="Lead Header Column" width="30"/>
            </configuration:leftHeaderColumn>
          </configuration:ProrSpecViewConfiguration>
        </configuration:specViewConfigurations>
        <configuration:generalConfiguration>
          <configuration:ProrGeneralConfiguration>
            <configuration:labelConfiguration>
              <configuration:LabelConfiguration>
                <defaultLabel>Description</defaultLabel>
              </configuration:LabelConfiguration>
            </configuration:labelConfiguration>
          </configuration:ProrGeneralConfiguration>
        </configuration:generalConfiguration>
      </configuration:ProrToolExtension>
    </REQ-IF-TOOL-EXTENSION>
  </TOOL-EXTENSIONS>
</REQ-IF>
&#13;
&#13;
&#13;

使用类似的东西:

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

<xsl:template match="REQ-IF/CORE-CONTENT/REQ-IF-CONTENT/SPECIFICATIONS[@LONG-NAME='Specification Document']/VALUES">
<xsl:value-of select="ATTRIBUTE-VALUE-STRING THE-VALUE">
</xsl:value-of>
</xsl:template>

但我看不出我出错的地方?我已经在模板匹配和价值上阅读了很多内容,但似乎是围成一圈。

如果我能看到如何做到这一点,我会对此感到满意。

2 个答案:

答案 0 :(得分:0)

<xsl:value-of select="ATTRIBUTE-VALUE-STRING THE-VALUE">

错了。这个是正确的:

<xsl:value-of select="ATTRIBUTE-VALUE-STRING/@THE-VALUE">

这个styleshhet使用您的条目编译并生成结果:

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

<xsl:template match="/">
    <root>
    <xsl:apply-templates select="REQ-IF/CORE-CONTENT/REQ-IF-CONTENT/SPECIFICATIONS/SPECIFICATION" />
    </root>
</xsl:template>

 <xsl:template match="SPECIFICATION[@LONG-NAME='Specification Document']">
    <output>
 <xsl:value-of select="VALUES/ATTRIBUTE-VALUE-STRING/@THE-VALUE " />
    </output>
</xsl:template>

结果是:

<root>
  <output>Requirements Document</output>
</root>

答案 1 :(得分:0)

有很多方法可以做到这一点,这是一个例子:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl d"
    xmlns:d="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
      <root>
        <xsl:apply-templates select="/*/*/*/*/d:SPECIFICATION[@LONG-NAME='Specification Document']/d:VALUES"/>
      </root>
    </xsl:template>

  <xsl:template match="*">
    <xsl:value-of select="d:ATTRIBUTE-VALUE-STRING/@THE-VALUE"/>
  </xsl:template>
</xsl:stylesheet>

请注意,您的XML具有默认命名空间,因此您需要声明指向默认命名空间uri的前缀,并在整个xpath表达式中使用该前缀(在上面的示例中为d:)。别忘了在exclude-result-prefixes列表中添加默认名称空间前缀。

输出

<?xml version="1.0" encoding="utf-8"?>
<root>Requirements Document</root>

旁注:用于代替上述示例中实际元素名称的通配符(*)只是为了缩短xpath。