컴퓨터 공부/번역

6. XML 속성

려리군 2009. 5. 13. 01:43

XML Attributes
XML 속성


XML elements can have attributes in the start tag, just like HTML.
XML 엘레먼트는 HTML처럼 시작 태그에서 속성들을 가질 수 있다.

Attributes provide additional information about elements.
속성은 엘레먼트에 관해 추가적인 정보를 제공한다.


XML Attributes
XML 속성

From HTML you will remember this: <img src="computer.gif">. The "src" attribute provides additional information about the <img> element.
HTML로부터 당신은 <img src="computer.gif">를 기억할 수 있을 것이다. "src" 속성은 <img> 엘레먼트에 관해 추가적인 정보를 제공한다.

In HTML (and in XML) attributes provide additional information about elements:
HTML(과 XML)에서 속성은 엘레먼트에 관한 추가적인 정보를 제공한다.


<img src="computer.gif">
<a href="demo.asp">


Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
속성은 주로 자료의 부분이 아닌 정보를 제공한다. 아래 예제에서 파일 형태는 자료와 관련이 없으나 이 엘레먼트를 조작하길 원하는 소프트웨어에게는 중요하다.


<file type="gif">computer.gif</file>


XML Attributes Must be Quoted
XML 속성은 따옴표 안에 들어가야 한다.

Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person's sex, the person tag can be written like this:
속성 값은 따옴표 안에 항상 포함되어야 하지만 작은 따옴표와 큰 따옴표 모두 사용될 수 있다. 사람의 성에 대해 person 태그는 다음과 같이 쓸 수 있다.


<person sex="female">


or like this:

또는


<person sex='female'>


If the attribute value itself contains double quotes you can use single quotes, like in this example:
속성 값 자체가 큰 따옴표를 포함한다면 당신은 다음 예제처럼 작은 따옴표를 사용할 수 있다.


<gangster name='George "Shotgun" Ziegler'>


or you can use character entities:
아니면 당신은 문자 개체를 사용할 수 있다.


<gangster name="George "Shotgun" Ziegler">


XML Elements vs. Attributes
XML 엘레먼트와 속성

Take a look at these examples:
다음 예제들을 보자.


<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>

<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>


In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.
첫번째 예제에서 sex는 속성이다. 마지막 예제에서 sex는 엘레먼트다. 두 예제 모두 같은 정보를 제공한다.

There are no rules about when to use attributes and when to use elements. Attributes are handy in HTML. In XML my advice is to avoid them. Use elements instead.
엘레먼트를 언제 사용하는 지와 속성을 언제 사용하는 지에 대한 규칙은 없다. 속성은 HTML에서 편리하다. XML에서는 이를 피하라고 충고하고 싶다. 대신에 엘레먼트를 사용해라.


My Favorite Way
내가 좋아하는 방법

The following three XML documents contain exactly the same information:
다음 세 개의 XML 문서는 정확하게 같은 정보를 포함한다.

A date attribute is used in the first example:
첫 번째 예제에서는 날짜 속성이 사용된다.


<note date="10/01/2008">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


A date element is used in the second example:
두 번째 예제에서는 날짜 엘레먼트가 사용된다.


<note>
<date>10/01/2008</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


An expanded date element is used in the third: (THIS IS MY FAVORITE):
세 번째는 확장된 날짜 엘레먼트가 사용된다. (이것이 내가 좋아하는 방법)


<note>
<date>
<day>10</day>
<month>01</month>
<year>2008</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>


Avoid XML Attributes?
XML 속성을 피해야할까?

Some of the problems with using attributes are:
속성을 사용하는 몇가지 문제점은

  • attributes cannot contain multiple values (elements can)
  • 속성은 (엘레먼트는 가능) 다양한 값들을 포함할 수 없다.
  • attributes cannot contain tree structures (elements can)
  • 속성은 (엘레먼트는 가능) 트리 구조를 포함할 수 없다.
  • attributes are not easily expandable (for future changes)
  • 속성은 (미래 변화를 대비하여) 쉽게 확장 가능하지 않다.

Attributes are difficult to read and maintain. Use elements for data. Use attributes for information that is not relevant to the data.
속성은 읽고 유지하기 어렵다. 자료를 위해서 엘레먼트를 사용해라. 자료와 관련 없는 정보는 속성을 사용하라.

Don't end up like this:
다음처럼 끝내지 말아라.


<note day="10" month="01" year="2008"
to="Tove" from="Jani" heading="Reminder"
body="Don't forget me this weekend!">
</note>


XML Attributes for Metadata
메타데이터를 위한 XML 속성

Sometimes ID references are assigned to elements. These IDs can be used to identify XML elements in much the same way as the ID attribute in HTML. This example demonstrates this:
가끔 ID 참조는 엘레먼트에 대응된다. 이러한 ID들은 HTML에서 ID 속성과 같은 방법으로 XML 요소들을 확인하기 위해 사용될 수 있다. 다음은 이를 보여주고 있다.


<messages>
<note id="501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
<note id="502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not</body>
</note>
</messages>


The ID above is just an identifier, to identify the different notes. It is not a part of the note itself.
위의 ID는 다른 note를 확인하기 위한 식별자이다. note 그 자체의 부분이 아니다.

What I'm trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

내가 여기에서 말하고 싶은 점은 메타데이터(데이터에 관한 데이터)는 속성으로서 저장되어야 하고 자료 그 자체는 엘레먼트로 저장되어야 한다는 점이다.


Reference : http://www.w3schools.com/xml/xml_attributes.asp