1

我一直在尝试从嵌入的 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>

使用这样的东西:

<?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>

但我看不到哪里出错了?我已经阅读了很多关于模板匹配和价值的内容,但似乎在兜圈子。

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

4

2 回答 2

0

the line

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

is wrong. This one is correct :

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

This styleshhet compile and produce a result with your entry :

<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>

The result is :

<root>
  <output>Requirements Document</output>
</root>
于 2015-06-11T15:25:06.020 回答
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 的前缀,并d:在整个 xpath 表达式中使用该前缀(在我上面的示例中)。不要忘记在exclude-result-prefixes列表中添加默认命名空间前缀。

输出 :

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

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

于 2015-06-12T02:45:00.883 回答